RMutex Class Reference

#include <e32std.h>

class RMutex : public RHandleBase

Inherits from

Detailed Description

A handle to a mutex.

The mutex itself is a kernel side object.

Handles should be closed after use. RHandleBase provides the necessary Close() function which should be called when the handle is no longer required.

See also: RHandleBase::Close

Member Function Documentation

CreateGlobal ( const TDesC &, TOwnerType )

IMPORT_C TIntCreateGlobal(const TDesC &aName,
TOwnerTypeaType = EOwnerProcess
)

Creates a global mutex and opens this handle to the mutex.

The kernel side object representing the mutex is given the name contained in the specified descriptor, which makes it global. This means that any thread in any process can search for the mutex, using TFindMutex, and open a handle to it. If the specified name is empty the kernel side object representing the mutex is unnamed and so cannot be opened by name. It can however be passed to another process as a process parameter or via IPC.

By default, any thread in the process can use this instance of RMutex to access the mutex. However, specifying EOwnerThread as the second parameter to this function, means that only the creating thread can use this instance of RMutex to access the mutex; any other thread in this process that wants to access the mutex must either duplicate this handle or use OpenGlobal().

See also: OpenGlobal RHandleBase::Duplicate() TFindMutex

Parameters
aNameThe name to be assigned to this global mutex.
aTypeAn enumeration whose enumerators define the ownership of this mutex handle. If not explicitly specified, EOwnerProcess is taken as default.
Return Value
KErrNone if successful, otherwise one of the other system wide error codes.

CreateLocal ( TOwnerType )

IMPORT_C TIntCreateLocal(TOwnerTypeaType = EOwnerProcess)

Creates a mutex and opens this handle to the mutex.

The kernel side object representing the mutex is unnamed. This means that it is not possible to search for the mutex, which makes it local to the current process.

By default, any thread in the process can use this instance of RMutex to access the mutex. However, specifying EOwnerThread as the parameter to this function, means that only the creating thread can use this instance of RMutex to access the mutex; any other thread in this process that wants to access the mutex must duplicate this handle.

See also: RHandleBase::Duplicate()

Parameters
aTypeAn enumeration whose enumerators define the ownership of this mutex handle. If not explicitly specified, EOwnerProcess is taken as default.
Return Value
KErrNone if successful, otherwise one of the system wide error codes.

IsHeld ( )

IMPORT_C TBoolIsHeld()

Test if this mutex is held by the current thread.

Return Value
True if the current thread has waited on the mutex, false otherwise.

Open ( const TFindMutex &, TOwnerType )

TInt Open(const TFindMutex &aFind,
TOwnerTypeaType = EOwnerProcess
)[inline]

Opens a handle to the global mutex found using a TFindMutex object.

A TFindMutex object is used to find all global mutexes whose full names match a specified pattern.

By default, any thread in the process can use this instance of RMutex to access the mutex. However, specifying EOwnerThread as the second parameter to this function, means that only the opening thread can use this instance of RMutex to access the mutex; any other thread in this process that wants to access the mutex must either duplicate the handle or use OpenGlobal() again.

Parameters
aFindA reference to the object which is used to find the mutex.
aTypeAn enumeration whose enumerators define the ownership of this mutex handle. If not explicitly specified, EOwnerProcess is taken as default.
Return Value
KErrNone if successful, otherwise one of the other system wide error codes.

Open ( RMessagePtr2, TInt, TOwnerType )

IMPORT_C TIntOpen(RMessagePtr2aMessage,
TIntaParam,
TOwnerTypeaType = EOwnerProcess
)

Opens a handle to a mutex using a handle number sent by a client to a server.

This function is called by the server.

Parameters
aMessageThe message pointer.
aParamAn index specifying which of the four message arguments contains the handle number.
aTypeAn enumeration whose enumerators define the ownership of this mutex handle. If not explicitly specified, EOwnerProcess is taken as default.
Return Value
KErrNone, if successful; KErrArgument, if the value of aParam is outside the range 0-3; KErrBadHandle, if not a valid handle; otherwise one of the other system-wide error codes.

Open ( TInt, TOwnerType )

IMPORT_C TIntOpen(TIntaArgumentIndex,
TOwnerTypeaType = EOwnerProcess
)

Opens a handle to a mutex using a handle number passed as an environment data item to the child process during the creation of that child process.

Note that this function can only be called successfully once.

See also: RProcess::SetParameter()

Parameters
aArgumentIndexAn index that identifies the slot in the process environment data that contains the handle number. This is a value relative to zero, i.e. 0 is the first item/slot. This can range from 0 to 15.
aTypeAn enumeration whose enumerators define the ownership of this mutex handle. If not explicitly specified, EOwnerProcess is taken as default.
Return Value
KErrNone, if successful; KErrNotFound, if the slot indicated by aArgumentIndex is empty; KErrArgument, if the slot does not contain a mutex handle; otherwise one of the other system-wide error codes.

OpenGlobal ( const TDesC &, TOwnerType )

IMPORT_C TIntOpenGlobal(const TDesC &aName,
TOwnerTypeaType = EOwnerProcess
)

Opens a handle to a global mutex.

Global mutexes are identified by name.

By default, any thread in the process can use this instance of RMutex to access the mutex. However, specifying EOwnerThread as the second parameter to this function, means that only the opening thread can use this instance of RMutex to access the mutex; any other thread in this process that wants to access the mutex must either duplicate the handle or use OpenGlobal() again.

See also: RHandleBase::Duplicate()

Parameters
aNameThe name of the global mutex which is to be opened.
aTypeAn enumeration whose enumerators define the ownership of this mutex handle. If not explicitly specified, EOwnerProcess is taken as default.
Return Value
KErrNone if successful, otherwise another of the system wide error codes.

Poll ( )

IMPORT_C TIntPoll()

Acquire the mutex if it is currently free, but don't wait for it.

This function checks if the mutex is currently held. If not the mutex is marked as held by the current thread and the call returns immediately indicating success. If the mutex is held by another thread the call returns immediately indicating failure. If the mutex is already held by the current thread a count is maintained of how many times the thread has acquired the mutex.

Return Value
KErrNone if the mutex was acquired KErrTimedOut if the mutex could not be acquired KErrGeneral if the semaphore is being reset, i.e the semaphore is about to be deleted.

Signal ( )

IMPORT_C voidSignal()

Release the mutex.

This function decrements the count of how many times the current thread has acquired this mutex. If the count is now zero the mutex is marked as free and, if any other threads are waiting for the mutex to become free, the highest priority among those is made ready to run. However the mutex is not marked as held by any thread - the thread which has just been awakened must actually run in order to acquire the mutex.

Pre-condition
The mutex must previously have been acquired by the current thread calling Wait().
Panic Codes
KERN-EXEC1 If the mutex has not previously been acquired by the current thread calling

Wait ( )

IMPORT_C voidWait()

Acquire the mutex, waiting for it to become free if necessary.

This function checks if the mutex is currently held. If not the mutex is marked as held by the current thread and the call returns immediately. If the mutex is held by another thread the current thread will suspend until the mutex becomes free. If the mutex is already held by the current thread a count is maintained of how many times the thread has acquired the mutex.

Wait ( TInt )

IMPORT_C TIntWait(TIntaTimeout)

Acquire the mutex, if necessary waiting up to a specified maximum amount of time for it to become free.

This function checks if the mutex is currently held. If not the mutex is marked as held by the current thread and the call returns immediately. If the mutex is held by another thread the current thread will suspend until the mutex becomes free or until the specified timeout period has elapsed. If the mutex is already held by the current thread a count is maintained of how many times the thread has acquired the mutex.

Parameters
aTimeoutThe timeout value in microseconds
Return Value
KErrNone if the mutex was acquired successfully. KErrTimedOut if the timeout has expired. KErrGeneral if the mutex is being reset, i.e the mutex is about to be deleted. KErrArgument if aTimeout is negative; otherwise one of the other system wide error codes.