root/trunk/dep/include/zthread/Semaphore.h @ 2

Revision 2, 4.7 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
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#ifndef __ZTSEMAPHORE_H__
24#define __ZTSEMAPHORE_H__
25
26#include "zthread/Lockable.h"
27#include "zthread/NonCopyable.h"
28
29namespace ZThread {
30 
31  class FifoSemaphoreImpl;
32 
33  /**
34   * @class Semaphore
35   * @author Eric Crahen <http://www.code-foo.com>
36   * @date <2003-07-16T15:28:01-0400>
37   * @version 2.2.1
38   *
39   * A Semaphore is an owner-less Lockable object. Its probably best described as
40   * a set of 'permits'. A Semaphore is initialized with an initial count and
41   * a maximum count, these would correspond to the number of 'permits' currently
42   * available and the number of' permits' in total.
43   *
44   * - Acquiring the Semaphore means taking a permit, but if there are none
45   *   (the count is 0) the Semaphore will block the calling thread.
46   *
47   * - Releasing the Semaphore means returning a permit, unblocking a thread
48   *   waiting for one.
49   *
50   * A Semaphore with an initial value of 1 and maximum value of 1 will act as
51   * a Mutex.
52   *
53   * Threads blocked on a Semaphore are resumed in FIFO order.
54   *
55   */
56  class ZTHREAD_API Semaphore : public Lockable, private NonCopyable {
57 
58    FifoSemaphoreImpl* _impl;
59 
60  public:
61 
62    /**
63     * Create a new Semaphore.
64     *
65     * @param count initial count
66     * @param maxCount maximum count
67     */
68    Semaphore(int count = 1, unsigned int maxCount = 1); 
69
70    //! Destroy the Semaphore
71    virtual ~Semaphore();
72
73    /**
74     * <i>Provided to reflect the traditional Semaphore semantics</i>
75     *
76     * @see acquire()
77     */ 
78    void wait(); 
79
80
81    /**
82     * <i>Provided to reflect the traditional Semaphore semantics</i>
83     *
84     * @see tryAcquire(unsigned long timeout)
85     */
86    bool tryWait(unsigned long timeout); 
87
88    /**
89     * <i>Provided to reflect the traditional Semaphore semantics</i>
90     *
91     * @see release()
92     */
93    void post(); 
94
95 
96    /**
97     * Get the current count of the semaphore.
98     *
99     * This value may change immediately after this function returns to the calling thread.
100     *
101     * @return <em>int</em> count
102     */
103    virtual int count(); 
104
105    /**
106     * Decrement the count, blocking that calling thread if the count becomes 0 or
107     * less than 0. The calling thread will remain blocked until the count is
108     * raised above 0, an exception is thrown or the given amount of time expires.
109     *
110     * @param timeout maximum amount of time (milliseconds) this method could block
111     *
112     * @return
113     *   - <em>true</em> if the Semaphore was acquired before <i>timeout</i> milliseconds elapse.
114     *   - <em>false</em> otherwise.
115     *
116     * @exception Interrupted_Exception thrown when the calling thread is interrupted.
117     *            A thread may be interrupted at any time, prematurely ending any wait.
118     *
119     * @see Lockable::tryAcquire(unsigned long timeout)
120     */
121    virtual bool tryAcquire(unsigned long timeout); 
122 
123
124    /**
125     * Decrement the count, blocking that calling thread if the count becomes 0 or
126     * less than 0. The calling thread will remain blocked until the count is
127     * raised above 0 or if an exception is thrown.
128     *
129     * @exception Interrupted_Exception thrown when the calling thread is interrupted.
130     *            A thread may be interrupted at any time, prematurely ending any wait.
131     *
132     * @see Lockable::acquire()
133     */
134    virtual void acquire();
135
136    /**
137     * Increment the count, unblocking one thread if count is positive.
138     *
139     * @exception InvalidOp_Exception thrown if the maximum count would be exceeded.
140     *
141     * @see Lockable::release()
142     */
143    virtual void release();
144 
145  }; 
146
147
148} // namespace ZThread
149
150#endif // __ZTSEMAPHORE_H__
Note: See TracBrowser for help on using the browser.