root/trunk/dep/src/zthread/macos/Monitor.h

Revision 2, 4.3 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 __ZTMONITOR_H__
24#define __ZTMONITOR_H__
25
26#include "../Status.h"
27#include "../FastLock.h"
28
29namespace ZThread {
30
31/**
32 * @class Monitor
33 * @author Eric Crahen <http://www.code-foo.com/>
34 * @date <2003-07-29T11:24:58-0400>
35 * @version 2.2.1
36 */
37class Monitor : public Status, private NonCopyable {
38
39  //! Serialize access to external objects
40  FastLock _lock;
41
42  //! Serialize access to internal state
43  FastLock _waitLock;
44
45  //! Semaphore to control the owning thread
46  MPSemaphoreID _sema;
47
48  //! Owning thread
49  MPTaskID _owner;
50
51  //! Waiting flag, to avoid uneccessary signals
52  volatile bool _waiting; 
53
54  //! Waiting flag, to avoid too many signals
55  volatile bool _pending; 
56
57  //! State of the monitor
58  volatile int _state;
59 
60 public:
61 
62  //! Create a new monitor.
63  Monitor();
64
65  //! Destroy the monitor.
66  ~Monitor() throw();
67
68  //! Acquire the external lock for this monitor.
69  inline void acquire() {
70    _lock.acquire();
71  }
72
73  //! Try to acquire the external lock for this monitor.
74  inline bool tryAcquire() {
75    return _lock.tryAcquire();
76  }
77
78  //! Release the external lock for this monitor.
79  inline void release() {
80    _lock.release();
81  }
82 
83  /**
84   * Wait for a state change and atomically unlock the external lock.
85   * Blocks for an indefinent amount of time.
86   *
87   * @return INTERRUPTED if the wait was ended by a interrupt()
88   *         or SIGNALED if the wait was ended by a notify()
89   *
90   * @post the external lock is always acquired before this function returns
91   */
92  inline STATE wait() {
93    return wait(0);
94  }
95
96  /**
97   * Wait for a state change and atomically unlock the external lock.
98   * May blocks for an indefinent amount of time.
99   *
100   * @param timeout - maximum time to block (milliseconds) or 0 to
101   * block indefinently
102   *
103   * @return INTERRUPTED if the wait was ended by a interrupt()
104   *         or TIMEDOUT if the maximum wait time expired.
105   *         or SIGNALED if the wait was ended by a notify()
106   *
107   * @post the external lock is always acquired before this function returns
108   */
109  STATE wait(unsigned long timeout);
110
111  /**
112   * Interrupt this monitor. If there is a thread blocked on this monitor object
113   * it will be signaled and released. If there is no waiter, a flag is set and
114   * the next attempt to wait() will return INTERRUPTED w/o blocking.
115   *
116   * @return false if the thread was previously INTERRUPTED.
117   */
118  bool interrupt();
119
120  /**
121   * Notify this monitor. If there is a thread blocked on this monitor object
122   * it will be signaled and released. If there is no waiter, a flag is set and
123   * the next attempt to wait() will return SIGNALED w/o blocking, if no other
124   * flag is set.
125   *
126   * @return false if the thread was previously INTERRUPTED.
127   */
128  bool notify();
129
130  /**
131   * Check the state of this monitor, clearing the INTERRUPTED status if set.
132   *
133   * @return bool true if the monitor was INTERRUPTED.
134   * @post INTERRUPTED flag cleared if the calling thread owns the monitor.
135   */
136  bool isInterrupted();
137
138  /**
139   * Mark the Status CANCELED, and INTERRUPT the montor.
140   *
141   * @see interrupt()
142   */
143  bool cancel();
144
145  /**
146   * Test the CANCELED Status, clearing the INTERRUPTED status if set.
147   *
148   * @return bool
149   */
150  bool isCanceled();
151
152};
153
154};
155
156#endif
Note: See TracBrowser for help on using the browser.