When srv_sleep is used in preemptive mode using a mutex, the corresponding srv_wakeup routine must be preceded by a request for the same mutex. This process ensures that the sleeping thread is ready for srv_wakeup to be executed. The following code fragment shows how srv_wakeup must be preceded by a request for the mutex when it is used in preemptive mode:
/*
** Loop forever, logging language text. srv_getmsg will cause
** this thread to be suspended until a message is available on
** the log_request message queue.
*/
while((get_status = srv_getmsgq(msgqid, &log_request,
SRV_M_WAIT, &info)) == CS_SUCCEED)
{
/*
** Do the logging here.
*/
/*
** Request the mutex to make sure the sender
** has called srv_sleep.
*/
if (WaitForSingleObject(Mutex,INFINITE) != WAIT_OBJECT_0)
return(CS_FAIL);
/*
** Wake up the thread that is waiting for the language
** text to be logged.
*/
srv_wakeup(log_request, SRV_M_WAKE_FIRST, (CS_VOID*)0, (CS_VOID*)0);
/*
** Release the mutex.
*/
if (!ReleaseMutex(Mutex))
return(CS_FAIL);
}