RCondVar Class Reference

#include <e32std.h>

class RCondVar : public RHandleBase

Inherits from

Detailed Description

A handle to a condition variable.

The condition variable 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

Broadcast ( )

IMPORT_C voidBroadcast()

Broadcast to a condition variable

This unblocks all threads which are currently blocked on the condition variable. If there are no threads currently waiting this call does nothing.

It is not required that any mutex is held when calling this function but it is recommended that the mutex associated with the condition variable is held since otherwise a race condition can result from the condition variable being signalled just after the waiting thread testing the condition and before it calls Wait().

CreateGlobal ( const TDesC &, TOwnerType )

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

Creates a global condition variable and opens this handle to it.

If the specified name is a non-empty string the kernel side object representing the condition variable is given the specified name and is therefore global. It may subsequently be opened by name using the RCondVar::OpenGlobal function. If the specified name is empty the kernel side object representing the condition variable is unnamed and so cannot be opened by name. It can however be passed to another process as a process parameter or via IPC.

If the specified name is non-empty it must consist entirely of printable ASCII characters (codes 0x20 to 0x7e inclusive) and may not contain : * or ?.

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

See also: RCondVar::OpenGlobal() RHandleBase::Duplicate() RProcess::SetParameter(TInt, RHandleBase) TIpcArgs::Set(TInt, RHandleBase) RMessagePtr2::Complete(RHandleBase)

Parameters
aNameThe name to be assigned to this condition variable.
aTypeAn enumeration whose enumerators define the ownership of this condition variable 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 condition variable and opens this handle to it.

The kernel side object representing the condition variable is unnamed and so the condition variable cannot be found by name and hence it is local to the current process.

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

See also: RHandleBase::Duplicate()

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

Open ( RMessagePtr2, TInt, TOwnerType )

IMPORT_C TIntOpen(RMessagePtr2aMessage,
TIntaParam,
TOwnerTypeaType = EOwnerProcess
)

Opens a handle to a condition variable 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 condition variable 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 condition variable 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 condition variable 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 condition variable 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 condition variable.

Global condition variables are identified by name.

By default, any thread in the process can use this instance of RCondVar to access the condition variable. However, specifying EOwnerThread as the parameter to this function means that only the creating thread can use this instance of RCondVar to access the condition variable; any other thread in this process that wants to access the condition variable must either duplicate this handle or use OpenGlobal again.

See also: RHandleBase::Duplicate()

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

Signal ( )

IMPORT_C voidSignal()

Signal a condition variable

This unblocks a single thread which is currently blocked on the condition variable. The highest priority waiting thread which is not explicitly suspended will be the one unblocked. If there are no threads currently waiting this call does nothing.

It is not required that any mutex is held when calling this function but it is recommended that the mutex associated with the condition variable is held since otherwise a race condition can result from the condition variable being signalled just after the waiting thread testing the condition and before it calls Wait().

TimedWait ( RMutex &, TInt )

IMPORT_C TIntTimedWait(RMutex &aMutex,
TIntaTimeout
)

Wait on a condition variable with timeout

This is the same as RCondVar::Wait(RMutex) except that there is a time limit on how long the current thread will block while waiting for the condition variable.

Pre-condition
The specified mutex is held by the current thread.
Post-condition
The specified mutex is held by the current thread unless the return value is KErrGeneral in which case the condition variable no longer exists.

See also: RCondVar::Wait(RMutex)

Parameters
aMutexThe mutex to be released and reacquired.
aTimeoutThe maximum time to wait in microseconds. 0 means no maximum.
Return Value
KErrNone if the condition variable has been signalled. KErrInUse if another thread is already waiting on this condition variable in conjunction with a different mutex. KErrGeneral if the condition variable is deleted. KErrTimedOut if the timeout expired before the condition variable was signalled.
Panic Codes
KERN-EXEC0 if either the condition variable or mutex handles are not valid.
KERN-EXEC54 if the current thread does not hold the specified mutex.

Wait ( RMutex & )

IMPORT_C TIntWait(RMutex &aMutex)

Wait on a condition variable

This call releases the specified mutex then atomically blocks the current thread on this condition variable. The atomicity here is with respect to the condition variable and mutex concerned. Specifically if the condition variable is signalled at any time after the mutex is released then this thread will be awakened. Once the thread has awakened it will reacquire the specified mutex before this call returns (except in the case where the condition variable has been deleted).

The usage pattern for this is as follows:

	mutex.Wait();
	while(!CONDITION)
		condvar.Wait(mutex);
	STATEMENTS;
	mutex.Signal();

where CONDITION is an arbitrary condition involving any number of user-side variables whose integrity is protected by the mutex.

It is necessary to loop while testing the condition because there is **no** guarantee that the condition has been satisfied when the condition variable is signalled. Different threads may be waiting on different conditions or the condition may have already been absorbed by another thread. All that can be said is that the thread will awaken whenever something happens which **might** affect the condition.

It needs to be stressed that if:

condvar.Wait(mutex);

completes, it does not necessarily mean that the condition is yet satisfied, hence the necessity for the loop.

Pre-condition
The specified mutex is held by the current thread.
Post-condition
The specified mutex is held by the current thread unless the return value is KErrGeneral in which case the condition variable no longer exists.

See also: RCondVar::Signal() RCondVar::Broadcast()

Parameters
aMutexThe mutex to be released and reacquired.
Return Value
KErrNone if the condition variable has been signalled. KErrInUse if another thread is already waiting on this condition variable in conjunction with a different mutex. KErrGeneral if the condition variable is deleted.
Panic Codes
KERN-EXEC0 if either the condition variable or mutex handles are not valid.
KERN-EXEC54 if the current thread does not hold the specified mutex.