1 | /* |
---|
2 | * Copyright (c) 2005, Eric Crahen |
---|
3 | * |
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
5 | * of this software and associated documentation files (the "Software"), to deal |
---|
6 | * in the Software without restriction, including without limitation the rights |
---|
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
8 | * copies of the Software, and to permit persons to whom the Software is furnished |
---|
9 | * to do so, subject to the following conditions: |
---|
10 | * |
---|
11 | * The above copyright notice and this permission notice shall be included in all |
---|
12 | * copies or substantial portions of the Software. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
---|
18 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
---|
19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | #include "Debug.h" |
---|
24 | #include "zthread/PrioritySemaphore.h" |
---|
25 | #include "SemaphoreImpl.h" |
---|
26 | |
---|
27 | namespace ZThread { |
---|
28 | |
---|
29 | class PrioritySemaphoreImpl : public SemaphoreImpl<priority_list> { |
---|
30 | public: |
---|
31 | |
---|
32 | PrioritySemaphoreImpl(int count, unsigned int maxCount) |
---|
33 | : SemaphoreImpl<priority_list>(count, maxCount, true) { } |
---|
34 | |
---|
35 | }; |
---|
36 | |
---|
37 | /** |
---|
38 | * Create a new semaphore of a given size with a given count |
---|
39 | * |
---|
40 | * @param initialCount initial count to assign this semaphore |
---|
41 | * @param maxCount maximum size of the semaphore count |
---|
42 | */ |
---|
43 | PrioritySemaphore::PrioritySemaphore(int count, unsigned int maxCount) { |
---|
44 | |
---|
45 | _impl = new PrioritySemaphoreImpl(count, maxCount); |
---|
46 | |
---|
47 | } |
---|
48 | |
---|
49 | PrioritySemaphore::~PrioritySemaphore() { |
---|
50 | |
---|
51 | if(_impl != 0) |
---|
52 | delete _impl; |
---|
53 | |
---|
54 | } |
---|
55 | |
---|
56 | void PrioritySemaphore::wait() { |
---|
57 | |
---|
58 | _impl->acquire(); |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | bool PrioritySemaphore::tryWait(unsigned long ms) { |
---|
64 | |
---|
65 | return _impl->tryAcquire(ms); |
---|
66 | |
---|
67 | } |
---|
68 | |
---|
69 | void PrioritySemaphore::post() { |
---|
70 | |
---|
71 | _impl->release(); |
---|
72 | |
---|
73 | } |
---|
74 | |
---|
75 | int PrioritySemaphore::count() { |
---|
76 | |
---|
77 | return _impl->count(); |
---|
78 | |
---|
79 | } |
---|
80 | |
---|
81 | /////////////////////////////////////////////////////////////////////////////// |
---|
82 | // Locakable compatibility |
---|
83 | // |
---|
84 | |
---|
85 | void PrioritySemaphore::acquire() { |
---|
86 | |
---|
87 | _impl->acquire(); |
---|
88 | |
---|
89 | } |
---|
90 | |
---|
91 | bool PrioritySemaphore::tryAcquire(unsigned long ms) { |
---|
92 | |
---|
93 | return _impl->tryAcquire(ms); |
---|
94 | |
---|
95 | |
---|
96 | } |
---|
97 | |
---|
98 | void PrioritySemaphore::release() { |
---|
99 | |
---|
100 | _impl->release(); |
---|
101 | |
---|
102 | } |
---|
103 | |
---|
104 | } // namespace ZThread |
---|