|
RtkCreateMutex creates an RTSS mutex. A handle is returned to the newly created mutex object.
Syntax
HANDLE RtkCreateMutex( RTSSINST RtssInst, PULONG pErrorCode, LPSECURITY_ATTRIBUTES pSecurity, BOOL bInitialOwner, PUNICODE_STRING lpName );
Parameters
RtssInst
An RTSSINST value returned from a call to RtkRtssAttach.
pErrorCode
A pointer to a location where additional error information may be returned. This location need not be defined; the user may pass a NULL value. If defined, this location is set to NULL if no error occurred.
pSecurity (ignored)
A pointer to a SECURITY_ATTRIBUTES structure.
bInitialOwner
The initial ownership state of the mutex object. If TRUE, the calling thread requests immediate ownership of the mutex object. Otherwise, the mutex is not owned.
lpName
A pointer to a PUNICODE_STRING specifying the name of the mutex object. The name is limited to RTX_MAX_PATH characters and can contain any character except the backslash path-separator character (\). Name comparison is case-sensitive.
If lpName matches the name of an existing named mutex object, this function requests MUTEX_ALL_ACCESS access to the existing object. In this case, the bInitialOwner parameter is ignored because it has already been set by the creating process.
If lpName matches the name of an existing semaphore, the function fails; ERROR_INVALID_HANDLE is returned in the ErrorCode location, if defined, provided by the caller. This occurs because mutex and semaphore objects share the same name space.
Return Values
A handle to the mutex object if the function succeeds
NOTE: If the named mutex object existed before the function call, pErrorCode is set to ERROR_ALREADY_EXISTS.
NULL if the function fails
Remarks
The handle returned by RtkCreateMutex has MUTEX_ALL_ACCESS access to the new mutex object and can be used in any function that requires a handle to a mutex object.
Any thread of the calling process can specify the mutex-object handle in a call to RtkWaitForSingleObject. This wait function returns when the state of the specified object is signaled.
The state of a mutex object is signaled when it is not owned by any thread. The creating thread can use the bInitialOwner flag to request immediate ownership of the mutex. Otherwise, a thread must use the wait function to request ownership. When the mutex's state is signaled, the highest priority waiting thread is granted ownership (if more than one thread is waiting at the same priority, they receive ownership of the mutex in the order they waited); the mutex's state changes to non-signaled; and the wait function returns. Only one thread can own a mutex at any given time. The owning thread uses RtkReleaseMutex to release its ownership.
The thread that owns a mutex can specify the same mutex in repeated wait function calls without blocking its execution. Typically, you would not wait repeatedly for the same mutex, but this mechanism prevents a thread from deadlocking itself while waiting for a mutex that it already owns. However, to release its ownership, the thread must call RtkReleaseMutex once for each time that the mutex satisfied a wait.
Two or more processes can call RtkCreateMutex to create the same named mutex. The first process actually creates the mutex, and subsequent processes open a handle to the existing mutex. This enables multiple processes to get handles of the same mutex, while relieving the user of the responsibility of ensuring that the creating process is started first. When using this technique, you should set the bInitialOwner flag to FALSE; otherwise, it can be difficult to be certain which process has initial ownership.
Multiple processes can have handles of the same mutex object, enabling use of the object for process synchronization. The available object-sharing mechanism is: A process can specify the name of a mutex object in a call to RtkOpenMutex or RtkCreateMutex.
RtkCloseHandle closes a mutex-object handle. The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.
Requirements
Header | Rtkapi.h |
Library | rtx_rtk.lib |
See Also: