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