2 * Copyright (c) 2005, Eric Crahen
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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.
23 #ifndef __ZTCOUNTINGSEMAPHORE_H__
24 #define __ZTCOUNTINGSEMAPHORE_H__
26 #include "zthread/Lockable.h"
27 #include "zthread/NonCopyable.h"
31 class FifoSemaphoreImpl
;
34 * @class CountingSemaphore
35 * @author Eric Crahen <http://www.code-foo.com>
36 * @date <2003-07-16T15:26:18-0400>
39 * A CountingSemaphore is an owner-less Lockable object.
41 * It differs from a normal Semaphore in that there is no upper bound on the count
42 * and it will not throw an exception because a maximum value has been exceeded.
46 * Threads blocked on a CountingSemaphore are resumed in FIFO order.
48 class ZTHREAD_API CountingSemaphore
: public Lockable
, private NonCopyable
{
50 FifoSemaphoreImpl
* _impl
;
55 * Create a new CountingSemaphore.
57 * @param count - initial count
59 CountingSemaphore(int initialCount
= 0);
61 //! Destroy the CountingSemaphore
62 virtual ~CountingSemaphore();
65 * <i>Provided to reflect the traditional Semaphore semantics</i>
73 * <i>Provided to reflect the traditional Semaphore semantics</i>
75 * @see tryAcquire(unsigned long timeout)
77 bool tryWait(unsigned long timeout
);
80 * <i>Provided to reflect the traditional Semaphore semantics</i>
88 * Get the current count of the semaphore.
90 * This value may change immediately after this function returns to the calling thread.
92 * @return <em>int</em> count
97 * Decrement the count, blocking that calling thread if the count becomes 0 or
98 * less than 0. The calling thread will remain blocked until the count is
99 * raised above 0, an exception is thrown or the given amount of time expires.
101 * @param timeout maximum amount of time (milliseconds) this method could block
104 * - <em>true</em> if the Semaphore was acquired before <i>timeout</i> milliseconds elapse.
105 * - <em>false</em> otherwise.
107 * @exception Interrupted_Exception thrown when the calling thread is interrupted.
108 * A thread may be interrupted at any time, prematurely ending any wait.
110 * @see Lockable::tryAcquire(unsigned long timeout)
112 virtual bool tryAcquire(unsigned long timeout
);
115 * Decrement the count, blocking that calling thread if the count becomes 0 or
116 * less than 0. The calling thread will remain blocked until the count is
117 * raised above 0 or if an exception is thrown.
119 * @exception Interrupted_Exception thrown when the calling thread is interrupted.
120 * A thread may be interrupted at any time, prematurely ending any wait.
122 * @see Lockable::acquire()
124 virtual void acquire();
127 * Increment the count, unblocking one thread if count is positive.
129 * @see Lockable::release()
131 virtual void release();
136 } // namespace ZThread
138 #endif // __ZTCOUNTINGSEMAPHORE_H__