mirror of
https://github.com/thestk/stk
synced 2026-01-15 05:51:52 +00:00
Version 4.2.0
This commit is contained in:
committed by
Stephen Sinclair
parent
cf06b7598b
commit
a6381b9d38
100
src/Mutex.cpp
Normal file
100
src/Mutex.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
/***************************************************/
|
||||
/*! \class Mutex
|
||||
\brief STK mutex class.
|
||||
|
||||
This class provides a uniform interface for
|
||||
cross-platform mutex use. On Linux and IRIX
|
||||
systems, the pthread library is used. Under
|
||||
Windows, critical sections are used.
|
||||
|
||||
by Perry R. Cook and Gary P. Scavone, 1995 - 2004.
|
||||
*/
|
||||
/***************************************************/
|
||||
|
||||
#include "Mutex.h"
|
||||
|
||||
Mutex :: Mutex()
|
||||
{
|
||||
|
||||
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
|
||||
|
||||
pthread_mutex_init(&mutex_, NULL);
|
||||
pthread_cond_init(&condition_, NULL);
|
||||
|
||||
#elif defined(__OS_WINDOWS__)
|
||||
|
||||
InitializeCriticalSection(&mutex_);
|
||||
condition_ = CreateEvent(NULL, // no security
|
||||
true, // manual-reset
|
||||
false, // non-signaled initially
|
||||
NULL); // unnamed
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
Mutex :: ~Mutex()
|
||||
{
|
||||
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
|
||||
|
||||
pthread_mutex_destroy(&mutex_);
|
||||
pthread_cond_destroy(&condition_);
|
||||
|
||||
#elif defined(__OS_WINDOWS__)
|
||||
|
||||
DeleteCriticalSection(&mutex_);
|
||||
CloseHandle( condition_ );
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
void Mutex :: lock()
|
||||
{
|
||||
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
|
||||
|
||||
pthread_mutex_lock(&mutex_);
|
||||
|
||||
#elif defined(__OS_WINDOWS__)
|
||||
|
||||
EnterCriticalSection(&mutex_);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
void Mutex :: unlock()
|
||||
{
|
||||
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
|
||||
|
||||
pthread_mutex_unlock(&mutex_);
|
||||
|
||||
#elif defined(__OS_WINDOWS__)
|
||||
|
||||
LeaveCriticalSection(&mutex_);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
void Mutex :: wait()
|
||||
{
|
||||
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
|
||||
|
||||
pthread_cond_wait(&condition_, &mutex_);
|
||||
|
||||
#elif defined(__OS_WINDOWS__)
|
||||
|
||||
WaitForMultipleObjects(1, &condition_, false, INFINITE);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
void Mutex :: signal()
|
||||
{
|
||||
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
|
||||
|
||||
pthread_cond_signal(&condition_);
|
||||
|
||||
#elif defined(__OS_WINDOWS__)
|
||||
|
||||
SetEvent( condition_ );
|
||||
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user