File: BinarySemaphore.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 (79 lines) | stat: -rw-r--r-- 1,739 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
/*
 * Modification History
 *
 * 2001-January-11		Jason Rohrer
 * Created.
 *
 * 2001-January-27		Jason Rohrer
 * Fixed a bug in the precompiler directives.
 *
 * 2003-August-26   Jason Rohrer
 * Added support for timeouts on wait.
 */

#include "minorGems/common.h" 



#ifndef BINARY_SEMAPHORE_CLASS_INCLUDED
#define BINARY_SEMAPHORE_CLASS_INCLUDED

#include "MutexLock.h"

/**
 * Binary semaphore class.  Semaphore starts out with a value of 0.
 *
 * Note:  Implementation for the functions defined here is provided
 *   separately for each platform (in the mac/ linux/ and win32/ 
 *   subdirectories).
 *
 * @author Jason Rohrer
 */
class BinarySemaphore {

	public:
		
		/**
		 * Constructs a binary semaphore.
		 */
		BinarySemaphore();
		
		~BinarySemaphore();
		
		
		/**
		 * Blocks on this semaphore until signal() is called by another thread.
		 * Note that if signal() has already been called before wait() is
		 * called, then this call will return immediately, though the semaphore
		 * is reset to 0 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 );
		
		
		/**
		 * Signals the semaphore, allowing a waiting thread to return from
		 * its call to wait(). (The semaphore is set to 1 by this call if
		 * no thread is waiting on the semaphore currently.)
		 */
		void signal();


	private:
		
		// starts at 0
		int mSemaphoreValue;
		
		/**
		 * Used by platform-specific implementations.
		 */		
		void *mNativeObjectPointerA;
		void *mNativeObjectPointerB;
	};


#endif