Modified APIs

The modified Open Server APIs are:

srv_sleep

srv_sleep is used to suspend the currently executing thread. srv_sleep now uses its final two parameters which had been reserved for future use. If you do not want to take advantage of the new functionality of srv_sleep no changes are required and your code will work as before.

The first reserved parameter is now an Open Server mutex, and the second is a time-out in milliseconds. Both of these parameters are optional, and should be set to (CS_VOID*)0 when not being used.

When a mutex is passed to srv_sleep, it is released before the thread suspends, but after it is marked suspended. The mutex is reacquired before srv_sleep returns. The behavior is modeled on condition variables (posix threads).

This pseduo-code fragment demonstrates how a multithreaded application may use the new sleep functionality to prevent race conditions where wakeups may be missed:

Sleep side:srv_lockmutex(mutex_id)
status = NOT_YET_DONE;
while (status != DONE)
{
srv_sleep(..., mutex_id, ...);
}
srv_unlockmutex(mutex_id)
Wakeup side:srv_lockmutex(mutex_id)
status = DONE;
srv_wakeup(...)
srv_unlockmutex(mutex_id)

If a srv_sleep call returns because of a time-out, the location pointed to by the infop parameter will be set to SRV_I_TIMEOUT. In this case, as in all others, the mutex is reacquired before the srv_sleep call returns. The normal usage of srv_sleep in the native threads version should be within a loop checking the predicate. For example:

mutex_lock()
        while (work != DONE)
        {
                srv_sleep(...);
        }
	mutex_unlock()

In this usage, the time-out is not useful because it is reset each time.

srv_props

The property SRV_S_NATIVEMUTEX has been added to srv_props(). You can set SRV_S_NATIVEMUTEX to CS_TRUE or CS_FALSE. The default is CS_FALSE. If set to CS_TRUE, the srv_createmutex, srv_lockmutex, srv_unlockmutex, and srv_deletemutex APIs use native (operating system) mutexes. If set to CS_FALSE, these APIs use Open Server mutexes.

Mutex operations will be faster if you set the SRV_S_NATIVEMUTEX property to CS_TRUE because mutex operations will map almost directly to operating system mutex operations. Currently, Open Server allows mutexes to be referred to by name. This requires that mutexes and their names be stored in tables. When you set SRV_S_NATIVEMUTEX property to CS_FALSE, mutex operations require table lookups; these table lookups must be synchronized with other threads to ensure that the mutex table does not become corrupt.

Mutexes created when SRV_S_NATIVEMUTEX is set to CS_FALSE support recursive locking.

Mutexes created when SRV_S_NATIVEMUTEX is set to CS_TRUE do not support recursive locking. If a mutex is locked, any attempt to lock it a second time, even by the same thread that originally locked it, will block the thread.

srv_deletemutex

srv_deletemutex() has been modified so that only an unlocked mutex can be deleted.

srv_dropproc

srv_dropproc() has been modified so that asynchronous or involuntary thread terminations are not allowed.