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

Revision 7, 3.8 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 #include <errno.h>
33 #include "common/base/log.h"
34 #include "common/sync/event.h"
35 #include "common/base/timer.h"
36
37 namespace synch {
38
39 Event::Event(bool is_signaled, bool manual_reset)
40   : is_signaled_(is_signaled),
41     manual_reset_(manual_reset) {
42   CHECK_SYS_FUN(pthread_mutex_init(&mutex_, NULL), 0);
43   CHECK_SYS_FUN(pthread_cond_init(&cond_, NULL), 0);
44 }
45 Event::~Event() {
46   CHECK_SYS_FUN(pthread_cond_destroy(&cond_), 0);
47   CHECK_SYS_FUN(pthread_mutex_destroy(&mutex_), 0);
48 }
49
50 void Event::Signal() {
51   CHECK_SYS_FUN(pthread_mutex_lock(&mutex_), 0);
52   is_signaled_ = true;
53   if ( manual_reset_ ) {
54     // manual reset event => wakes up all waiting thread
55     CHECK_SYS_FUN(pthread_cond_broadcast(&cond_), 0);
56   } else {
57     // auto reset event => wakes up just 1 thread
58     CHECK_SYS_FUN(pthread_cond_signal(&cond_), 0);
59   }
60   CHECK_SYS_FUN(pthread_mutex_unlock(&mutex_), 0);
61 }
62
63 void Event::Reset() {
64   DCHECK(manual_reset_);
65   CHECK_SYS_FUN(pthread_mutex_lock(&mutex_), 0);
66   is_signaled_ = false;
67   CHECK_SYS_FUN(pthread_mutex_unlock(&mutex_), 0);
68 }
69
70 void Event::Wait() {
71   CHECK_SYS_FUN(pthread_mutex_lock(&mutex_), 0);
72   while ( !is_signaled_ ) {
73     CHECK_SYS_FUN(pthread_cond_wait(&cond_, &mutex_), 0);
74   }
75   CHECK(AutoResetLocked());
76   CHECK_SYS_FUN(pthread_mutex_unlock(&mutex_), 0);
77 }
78
79 bool Event::Wait(uint32 timeout_in_ms) {
80   if ( kInfiniteWait == timeout_in_ms ) {
81     Wait();
82     return true;
83   }
84   CHECK_SYS_FUN(pthread_mutex_lock(&mutex_), 0);
85   const struct timespec ts = timer::TimespecAbsoluteMsec(timeout_in_ms);
86   const int64 ms_start = timer::TicksMsec();
87   int64 ms_now = ms_start;
88   while ( ms_now - ms_start < timeout_in_ms && !is_signaled_ ) {
89     // NOTE: [COSMIN]: normally pthread_cond_timedwait returns ETIMEDOUT
90     // after the timestamp expired, NOT sooner.
91     // However, there is bug in GDB: a thread waiting in pthread_cond_timedwait
92     // immediately returns ETIMEDOUT if another thread is created.
93     // The workaround is to wait in a loop, util the timestamp expired
94     // or the event becomes signaled.
95     //
96     const int result = pthread_cond_timedwait(&cond_, &mutex_, &ts);
97     CHECK(result == 0 || result == ETIMEDOUT) << " Invalid result: " << result;
98     ms_now = timer::TicksMsec();
99   }
100   bool ret = AutoResetLocked();
101   CHECK_SYS_FUN(pthread_mutex_unlock(&mutex_), 0);
102   return ret;
103 }
104 }
Note: See TracBrowser for help on using the browser.