File: Semaphore.h

package info (click to toggle)
primrose 6%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,304 kB
  • sloc: cpp: 27,318; php: 765; ansic: 636; objc: 272; sh: 136; makefile: 92; perl: 67
file content (201 lines) | stat: -rw-r--r-- 4,251 bytes parent folder | download | duplicates (30)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Modification History
 *
 * 2001-January-11		Jason Rohrer
 * Created.
 *
 * 2001-January-11		Jason Rohrer
 * Added a willBlock() function. 
 *
 * 2001-February-24		Jason Rohrer
 * Fixed incorrect delete usage.
 *
 * 2002-February-11		Jason Rohrer
 * Fixed a mistake in the signal() comment.
 *
 * 2003-August-26   Jason Rohrer
 * Added support for timeouts on wait.
 *
 * 2003-December-28   Jason Rohrer
 * Fixed a bug in semaphore value when we timeout on wait.
 *
 * 2004-January-9   Jason Rohrer
 * Fixed a preprocessor error.
 */

#include "minorGems/common.h" 



#ifndef SEMAPHORE_CLASS_INCLUDED
#define SEMAPHORE_CLASS_INCLUDED

#include "MutexLock.h"
#include "BinarySemaphore.h"


/**
 * General semaphore with an unbounded value.
 *
 * This class uses BinarySemaphores to implement general semaphores,
 * so it relies on platform-specific BinarySemaphore implementations, 
 * but this class itself is platform-independent.
 *
 * @author Jason Rohrer
 */
class Semaphore {

	public:
		
		/**
		 * Constructs a semaphore.
		 *
		 * @param inStartingValue the starting value for this semaphore.
		 *   Defaults to 0 if unspecified.
		 */
		Semaphore( int inStartingValue = 0 );
		
		~Semaphore();
		
		
		/**
		 * If this semaphore's current value is 0, then this call blocks 
		 * on this semaphore until signal() is called by another thread.
		 * If this semaphore's value is >0, then it is decremented by this
         * call.
         *
         * @param inTimeoutInMilliseconds the maximum time to wait in
         *   milliseconds, or -1 to wait forever.  Defaults to -1.
         *
         * @return 1 if the semaphore was signaled, or 0 if it timed out.
		 */
		int wait( int inTimeoutInMilliseconds = -1 );
        
		
		
		/**
		 * If a thread is waiting on this semaphore, then the thread
		 * becomes unblocked.
		 * If no thread is waiting, then the semaphore is incremented.
		 */
		void signal();

		
		/**
		 * Returns true if a call to wait would have blocked.
		 */
		char willBlock();


	private:
		
		// starts at 0
		int mSemaphoreValue;
		
		// mutex semaphore starts at 1
		BinarySemaphore *mMutexSemaphore;
		// blocking semaphore starts at 0
		BinarySemaphore *mBlockingSemaphore;
	};



inline Semaphore::Semaphore( int inStartingValue )
	: mSemaphoreValue( inStartingValue ), 
	mMutexSemaphore( new BinarySemaphore() ),
	mBlockingSemaphore(  new BinarySemaphore() ) {
	
	// increment the mutex semaphore to 1
	mMutexSemaphore->signal();
	}
	
	
	
inline Semaphore::~Semaphore() {
	delete mMutexSemaphore;
	delete mBlockingSemaphore;
	}
	
	
	
inline int Semaphore::wait( int inTimeoutInMilliseconds ) {
    int returnValue;
    // this implementation copied from _Operating System Concepts_, p. 172
	
	// lock the mutex
	mMutexSemaphore->wait();
	// decrement the semaphore
	mSemaphoreValue--;
	if( mSemaphoreValue < 0 ) {
		// we should block
		
		// release the mutex
		mMutexSemaphore->signal();
		
		// block
		returnValue = mBlockingSemaphore->wait( inTimeoutInMilliseconds );

        if( returnValue != 1 ) {
            // timed out

            // increment the semaphore, since we never got signaled
            // lock the mutex
            mMutexSemaphore->wait();
            mSemaphoreValue++;

            // we will unlock the mutex below
            }
		}
    else {
        returnValue = 1;
        }
	
	// release the mutex
	// ( if we were signaled, then the signaller left the mutex locked )
    // ( if we timed out, then we re-locked the mutex above )
	mMutexSemaphore->signal();

    return returnValue;
	}
	
	
	
inline char Semaphore::willBlock() {
	char returnValue = false;
	
	// lock the mutex
	mMutexSemaphore->wait();

	// check if we will block
	if( mSemaphoreValue <= 0 ) {
		returnValue = true;
		}
	
	// release the mutex
	mMutexSemaphore->signal();
	
	return returnValue;
	}
	
	
	
inline void Semaphore::signal() {
	// lock the mutex
	mMutexSemaphore->wait();
	// increment the semaphore
	mSemaphoreValue++;
	if( mSemaphoreValue <= 0 ) {
		// we need to wake up a waiting thread
		mBlockingSemaphore->signal();
		// let the waiting thread unlock the mutex
		}
	else {
		// no threads are waiting, so we need to unlock the mutex
		mMutexSemaphore->signal();
		}
	}
	
	
	

#endif