Lock a mutex.
CS_RETCODE srv_lockmutex(mutex_id, waitflag, infop)
SRV_OBJID mutex_id; CS_INT waitflag; CS_INT *infop;
The unique mutex identifier that was returned by the call to srv_createmutex. Given the name of the mutex, the mutex_id can be obtained by calling srv_getobjid.
Specifies whether the thread requesting the mutex lock should wait or just return if the mutex cannot be granted immediately. The value in *indp indicates whether the lock was granted. The two valid values for waitflag are SRV_M_WAIT, which indicates that the thread should wait if the lock cannot be granted immediately, and SRV_M_NOWAIT, which indicates that the thread should return without waiting if the lock cannot be granted.
A pointer to a CS_INT that is set to one of the following values:
SRV_I_SYNC – The lock was granted synchronously—the thread requesting the lock was not suspended to wait for the lock. srv_lockmutex returned CS_SUCCEED.
SRV_I_GRANTED – The lock was granted after the requesting thread was suspended to wait for another thread to release a lock on the mutex. srv_lockmutex returned CS_SUCCEED.
SRV_I_INTERRUPTED – The thread received an attention while waiting for the lock. The lock was not granted, and srv_lockmutex returned CS_FAIL.
SRV_I_WOULDWAIT – The waitflag parameter was set to SRV_M_NOWAIT and the thread would have had to wait for the lock. The lock was not granted, and srv_lockmutex returned CS_FAIL.
SRV_I_UNKNOWN – Some other error occurred, for example, the mutex does not exist. srv_lockmutex returned CS_FAIL.
Returns |
To indicate |
---|---|
CS_SUCCEED |
The routine completed successfully. |
CS_FAIL |
The routine failed. |
#include <ospublic.h>
/*
** Local Prototype
*/
CS_RETCODE ex_srv_lockmutex PROTOTYPE((
SRV_OBJID mid
));
/*
** EX_SRV_LOCKMUTEX
**
** Example routine to illustrate the use of srv_lockmutex.
**
** Arguments:
** mid - The id of the mutex to lock.
**
** Returns:
**
** CS_SUCCEED Mutex successfully locked.
** CS_FAIL An error was detected.
*/
CS_RETCODE ex_srv_lockmutex(mid)
SRV_OBJID mid; /* The mutex id. */
{
CS_INT info; /* Information output variable. */
/*
** Request the mutex lock - sleep until we get it.
*/
if( srv_lockmutex(mid, SRV_M_WAIT, &info) == CS_FAIL )
{
/*
** An error was al&ready raised.
*/
return CS_FAIL;
}
/*
** All done.
*/
return CS_SUCCEED;
}
Mutexes are associated with data objects and program resources that must be protected from simultaneous access by multiple threads.
Mutex locks are granted to threads on a first-come, first-served basis.
The lock is granted only if no other thread has al&ready obtained a lock on the mutex.
srv_lockmutex cannot be used in a SRV_START or SRV_ATTENTION handler.
A thread can lock a mutex more than once, but must call srv_unlockmutex once for each call to srv_lockmutex before another thread can lock the mutex.
If the mutex was waiting for is deleted, srv_lockmutex returns CS_FAIL.
srv_createmutex, srv_deletemutex, srv_getobjid, srv_unlockmutex