srv_lockmutex

Description

Lock a mutex.

Syntax

CS_RETCODE srv_lockmutex(mutex_id, waitflag, infop)
SRV_OBJID       mutex_id;
CS_INT              waitflag;
CS_INT            *infop;

Parameters

mutex_id

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.

waitflag

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.

infop

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

Table 3-60: Return values (srv_lockmutex)

Returns

To indicate

CS_SUCCEED

The routine completed successfully.

CS_FAIL

The routine failed.

Examples

Example 1

#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;
}

Usage

See also

srv_createmutex, srv_deletemutex, srv_getobjid, srv_unlockmutex