root/trunk/whisperlib/common/sync/event.h

Revision 7, 3.1 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Cosmin Tudorache
31
32 #ifndef __COMMON_SYNC_EVENT_H__
33 #define __COMMON_SYNC_EVENT_H__
34
35 #include <whisperlib/common/base/types.h>
36 #include <whisperlib/common/sync/mutex.h>
37
38 namespace synch {
39
40 class Event {
41  protected:
42   pthread_mutex_t mutex_;
43   pthread_cond_t cond_;
44
45   bool is_signaled_;
46   const bool manual_reset_;
47
48  public:
49   // is_signaled - initial state
50   // manual_reset - when set, the event is reset on an explicit
51   //                Reset call; else we reset it implicitly when
52   //                releasing a thread from a Wait.
53   Event(bool is_signaled, bool manual_reset);
54
55   // All waiting threads are released.
56   virtual ~Event();
57
58   // Signals the event and releases all threads in manual_reset_ mode
59   // or only *one* thread in automatic mode (!manual_reset_)
60   void Signal();
61
62   // resets the signal - makes sense only in manual_reset_ mode
63   void Reset();
64
65   // Wait for is_signaled_ to be turned on, else waits for a Signal
66   // to happen
67   void Wait();
68
69   static const uint32 kInfiniteWait = 0xffffffff;
70
71   // Same as Wait but w/ a given timeout in miliseconds. For an infinite
72   // wait give Event::kInfiniteWait as timeout_ms.
73   // At timeout_ms == 0 no wait happens
74   // returns:
75   //   true - if the event was signaled in time.
76   //   false - if the event is not signaled, and the timeout
77   //           period expired.
78   bool Wait(uint32 timeout_in_ms);
79
80  private:
81   bool AutoResetLocked() {
82     const bool ret = is_signaled_;
83     if ( is_signaled_ && !manual_reset_ ) {
84       is_signaled_ = false;
85     }
86     return ret;
87   }
88
89  private:
90   DISALLOW_EVIL_CONSTRUCTORS(Event);
91 };
92 }
93
94 #endif  // __COMMON_SYNC_EVENT_H__
Note: See TracBrowser for help on using the browser.