RSemaphore Class Reference

#include <e32cmn.h>

class RSemaphore : public RHandleBase

Inherits from

Detailed Description

A handle to a semaphore.

The semaphore itself is a Kernel side object.

As with all handles, they 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 &, TInt, TOwnerType )

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

Creates a global semaphore, setting its initial count, and opens this handle to the semaphore.

The kernel side object representing the semaphore 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 semaphore, using TFindSemaphore, and open a handle to it. If the specified name is empty the kernel side object representing the semaphore 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 RSemaphore to access the semaphore. However, specifying EOwnerThread as the third parameter to this function, means that only the creating thread can use this instance of RSemaphore to access the semaphore; any other thread in this process that wants to access the semaphore must either duplicate this handle or use OpenGlobal().

See also: RSemaphore::OpenGlobal() RHandleBase::Duplicate() TFindSemaphore

Parameters
aNameA reference to the descriptor containing the name to be assigned to this global semaphore.
aCountThe initial value of the semaphore count.
aTypeAn enumeration whose enumerators define the ownership of this semaphore handle. If not explicitly specified, EOwnerProcess is taken as default.
Return Value
KErrNone if successful otherwise another of the system wide error codes.
Panic Codes
USER105 if aCount is negative.

CreateLocal ( TInt, TOwnerType )

IMPORT_C TIntCreateLocal(TIntaCount,
TOwnerTypeaType = EOwnerProcess
)

Creates a semaphore, setting its initial count, and opens this handle to the semaphore.

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

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

See also: RHandleBase::Duplicate()

Parameters
aCountThe initial value of the semaphore count.
aTypeAn enumeration whose enumerators define the ownership of this semaphore handle. If not explicitly specified, EOwnerProcess is taken as default.
Return Value
KErrNone if successful, otherwise another of the system wide error codes.
Panic Codes
USER105 if aCount is negative.

Open ( const TFindSemaphore &, TOwnerType )

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

Opens a handle to the global semaphore found using a TFindSemaphore object.

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

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

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

Open ( RMessagePtr2, TInt, TOwnerType )

IMPORT_C TIntOpen(RMessagePtr2aMessage,
TIntaParam,
TOwnerTypeaType = EOwnerProcess
)

Opens a handle to a semaphore 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 semaphore 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 semaphore 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 semaphore 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 Semaphore 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 semaphore.

Global semaphores are identified by name.

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

See also: RHandleBase::Duplicate()

Parameters
aNameA reference to the descriptor containing the name of the global semaphore to be opened.
aTypeAn enumeration whose enumerators define the ownership of this semaphore 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()

Acquires the semaphore if that is possible without waiting.

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

Signal ( )

IMPORT_C voidSignal()

Signals the semaphore once.

The function increments the semaphore count by one. If the count was negative before being incremented, the highest priority thread waiting on the semaphore's queue of threads is removed from that queue and, provided that it is not suspended for any other reason, is marked as ready to run.

Signal ( TInt )

IMPORT_C voidSignal(TIntaCount)

Signals the semaphore one or more times.

The function increments the semaphore count. If the count was negative before being incremented, the highest priority thread waiting on the semaphore's queue of threads is removed from that queue and, provided that it is not suspended for any other reason, is marked as ready to run.

Parameters
aCountThe number of times the semaphore is to be signalled.

Wait ( )

IMPORT_C voidWait()

Waits for a signal on the semaphore.

The function decrements the semaphore count by one and returns immediately if it is zero or positive.

If the semaphore count is negative after being decremented, the calling thread is added to a queue of threads maintained by this semaphore.

The thread waits until the semaphore is signalled. More than one thread can be waiting on a particular semaphore at a time. When there are multiple threads waiting on a semaphore, they are released in priority order.

If the semaphore is deleted, all threads waiting on that semaphore are released.

Wait ( TInt )

IMPORT_C TIntWait(TIntaTimeout)

Waits for a signal on the semaphore, or a timeout.

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