f@0: /***************************************************/ f@0: /*! \class Mutex f@0: \brief STK mutex class. f@0: f@0: This class provides a uniform interface for f@0: cross-platform mutex use. On Linux and IRIX f@0: systems, the pthread library is used. Under f@0: Windows, critical sections are used. f@0: f@0: by Perry R. Cook and Gary P. Scavone, 1995--2014. f@0: */ f@0: /***************************************************/ f@0: #pragma once f@0: f@0: #include "../include/Mutex.h" f@0: f@0: namespace stk { f@0: f@0: Mutex :: Mutex() f@0: { f@0: f@0: #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__)) f@0: f@0: pthread_mutex_init(&mutex_, NULL); f@0: pthread_cond_init(&condition_, NULL); f@0: f@0: #elif defined(__OS_WINDOWS__) f@0: f@0: InitializeCriticalSection(&mutex_); f@0: condition_ = CreateEvent(NULL, // no security f@0: true, // manual-reset f@0: false, // non-signaled initially f@0: NULL); // unnamed f@0: f@0: #endif f@0: } f@0: f@0: Mutex :: ~Mutex() f@0: { f@0: #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__)) f@0: f@0: pthread_mutex_destroy(&mutex_); f@0: pthread_cond_destroy(&condition_); f@0: f@0: #elif defined(__OS_WINDOWS__) f@0: f@0: DeleteCriticalSection(&mutex_); f@0: CloseHandle( condition_ ); f@0: f@0: #endif f@0: } f@0: f@0: void Mutex :: lock() f@0: { f@0: #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__)) f@0: f@0: pthread_mutex_lock(&mutex_); f@0: f@0: #elif defined(__OS_WINDOWS__) f@0: f@0: EnterCriticalSection(&mutex_); f@0: f@0: #endif f@0: } f@0: f@0: void Mutex :: unlock() f@0: { f@0: #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__)) f@0: f@0: pthread_mutex_unlock(&mutex_); f@0: f@0: #elif defined(__OS_WINDOWS__) f@0: f@0: LeaveCriticalSection(&mutex_); f@0: f@0: #endif f@0: } f@0: f@0: void Mutex :: wait() f@0: { f@0: #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__)) f@0: f@0: pthread_cond_wait(&condition_, &mutex_); f@0: f@0: #elif defined(__OS_WINDOWS__) f@0: f@0: WaitForMultipleObjects(1, &condition_, false, INFINITE); f@0: f@0: #endif f@0: } f@0: f@0: void Mutex :: signal() f@0: { f@0: #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__)) f@0: f@0: pthread_cond_signal(&condition_); f@0: f@0: #elif defined(__OS_WINDOWS__) f@0: f@0: SetEvent( condition_ ); f@0: f@0: #endif f@0: } f@0: f@0: } // stk namespace