annotate stk/src/Mutex.cpp @ 0:4606bd505630 tip

first import
author Fiore Martin <f.martin@qmul.ac.uk>
date Sat, 13 Jun 2015 15:08:10 +0100
parents
children
rev   line source
f@0 1 /***************************************************/
f@0 2 /*! \class Mutex
f@0 3 \brief STK mutex class.
f@0 4
f@0 5 This class provides a uniform interface for
f@0 6 cross-platform mutex use. On Linux and IRIX
f@0 7 systems, the pthread library is used. Under
f@0 8 Windows, critical sections are used.
f@0 9
f@0 10 by Perry R. Cook and Gary P. Scavone, 1995--2014.
f@0 11 */
f@0 12 /***************************************************/
f@0 13 #pragma once
f@0 14
f@0 15 #include "../include/Mutex.h"
f@0 16
f@0 17 namespace stk {
f@0 18
f@0 19 Mutex :: Mutex()
f@0 20 {
f@0 21
f@0 22 #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
f@0 23
f@0 24 pthread_mutex_init(&mutex_, NULL);
f@0 25 pthread_cond_init(&condition_, NULL);
f@0 26
f@0 27 #elif defined(__OS_WINDOWS__)
f@0 28
f@0 29 InitializeCriticalSection(&mutex_);
f@0 30 condition_ = CreateEvent(NULL, // no security
f@0 31 true, // manual-reset
f@0 32 false, // non-signaled initially
f@0 33 NULL); // unnamed
f@0 34
f@0 35 #endif
f@0 36 }
f@0 37
f@0 38 Mutex :: ~Mutex()
f@0 39 {
f@0 40 #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
f@0 41
f@0 42 pthread_mutex_destroy(&mutex_);
f@0 43 pthread_cond_destroy(&condition_);
f@0 44
f@0 45 #elif defined(__OS_WINDOWS__)
f@0 46
f@0 47 DeleteCriticalSection(&mutex_);
f@0 48 CloseHandle( condition_ );
f@0 49
f@0 50 #endif
f@0 51 }
f@0 52
f@0 53 void Mutex :: lock()
f@0 54 {
f@0 55 #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
f@0 56
f@0 57 pthread_mutex_lock(&mutex_);
f@0 58
f@0 59 #elif defined(__OS_WINDOWS__)
f@0 60
f@0 61 EnterCriticalSection(&mutex_);
f@0 62
f@0 63 #endif
f@0 64 }
f@0 65
f@0 66 void Mutex :: unlock()
f@0 67 {
f@0 68 #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
f@0 69
f@0 70 pthread_mutex_unlock(&mutex_);
f@0 71
f@0 72 #elif defined(__OS_WINDOWS__)
f@0 73
f@0 74 LeaveCriticalSection(&mutex_);
f@0 75
f@0 76 #endif
f@0 77 }
f@0 78
f@0 79 void Mutex :: wait()
f@0 80 {
f@0 81 #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
f@0 82
f@0 83 pthread_cond_wait(&condition_, &mutex_);
f@0 84
f@0 85 #elif defined(__OS_WINDOWS__)
f@0 86
f@0 87 WaitForMultipleObjects(1, &condition_, false, INFINITE);
f@0 88
f@0 89 #endif
f@0 90 }
f@0 91
f@0 92 void Mutex :: signal()
f@0 93 {
f@0 94 #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
f@0 95
f@0 96 pthread_cond_signal(&condition_);
f@0 97
f@0 98 #elif defined(__OS_WINDOWS__)
f@0 99
f@0 100 SetEvent( condition_ );
f@0 101
f@0 102 #endif
f@0 103 }
f@0 104
f@0 105 } // stk namespace