PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/synch/semaphore.d

http://github.com/wilkie/djehuty
D | 58 lines | 33 code | 14 blank | 11 comment | 1 complexity | d539934510f8c6368b74b71072f0a033 MD5 | raw file
 1module synch.semaphore;
 2
 3import scaffold.thread;
 4
 5import platform.vars.semaphore;
 6
 7import core.definitions;
 8
 9// Section: Core/Synchronization
10
11// Description: This class abstracts a counting semaphore.
12class Semaphore {
13
14	~this() {
15		if (_inited)
16		{
17			SemaphoreUninit(_pfvars);
18		}
19	}
20
21	// Description: Creates an uninitialized semaphore.
22	this() {
23	}
24
25	// Description: Creates and initializes a semaphore.
26	// initialValue: The initial count for the semaphore.
27	this(uint initialValue) {
28		init(initialValue);
29	}
30
31	// Description: This function will initialize a semaphore and set it to an initial count.
32	// initialValue: The initial count for the semaphore.
33	void init(uint initialValue) {
34		SemaphoreInit(_pfvars, initialValue);
35		_inited = true;
36	}
37
38	// Description: This function will increment the count of the semaphore.
39	void up() {
40		SemaphoreUp(_pfvars);
41	}
42
43	// Description: This function will decrement the count of the semaphore as long as the count is greater than 0.  If not, it will yield the thread until the count is incremented via an up() call.  This is the blocking call of the semaphore.
44	void down() {
45		SemaphoreDown(_pfvars);
46	}
47
48	// Description: This function will decrement the count of the semaphore as long as the count is greater than 0.  If not, it will yield the thread until the count is incremented via an up() call.  This is the blocking call of the semaphore, but it will only block for as long as it is specified and will continue once there is a timeout.
49	// milliseconds: The amount of time to block before continuing if the semaphore should have a count of zero.
50	void down(uint milliseconds) {
51		SemaphoreDown(_pfvars, milliseconds);
52	}
53
54protected:
55
56	SemaphorePlatformVars _pfvars;
57	bool _inited = false;
58}