OFFIS DCMTK  Version 3.6.0
syncppth.h
Go to the documentation of this file.
1 // Copyright (C) 2009, Vaclav Haisman. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without modifica-
4 // tion, are permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 //
9 // 2. Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 //
13 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
14 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
16 // APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
18 // DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
19 // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 
29 
30 namespace log4cplus { namespace thread {
31 
32 
33 #define LOG4CPLUS_THROW_RTE(msg) \
34  do { detail::syncprims_throw_exception (msg, __FILE__, __LINE__); } while (0)
35 
36 //
37 //
38 //
39 
40 inline
41 Mutex::Mutex ()
42 {
43  int ret = pthread_mutex_init (&mtx, 0);
44  if (ret != 0)
45  LOG4CPLUS_THROW_RTE ("Mutex::Mutex");
46 }
47 
48 
49 inline
50 Mutex::~Mutex ()
51 {
52  int ret = pthread_mutex_destroy (&mtx);
53  if (ret != 0)
54  LOG4CPLUS_THROW_RTE ("Mutex::~Mutex");
55 }
56 
57 
58 inline
59 void
60 Mutex::lock () const
61 {
62  int ret = pthread_mutex_lock (&mtx);
63  if (ret != 0)
64  LOG4CPLUS_THROW_RTE ("Mutex::lock");
65 }
66 
67 
68 inline
69 void
70 Mutex::unlock () const
71 {
72  int ret = pthread_mutex_unlock (&mtx);
73  if (ret != 0)
74  LOG4CPLUS_THROW_RTE ("Mutex::unlock");
75 }
76 
77 
78 //
79 //
80 //
81 
82 inline
83 Semaphore::Semaphore (unsigned max, unsigned initial)
84 {
85  int ret = sem_init (&sem, max, initial);
86  if (ret != 0)
87  LOG4CPLUS_THROW_RTE ("Semaphore::Semaphore");
88 }
89 
90 
91 inline
92 Semaphore::~Semaphore ()
93 try
94 {
95  int ret = sem_destroy (&sem);
96  if (ret != 0)
97  LOG4CPLUS_THROW_RTE ("Semaphore::~Semaphore");
98 }
99 catch (...)
100 { }
101 
102 
103 inline
104 void
105 Semaphore::unlock () const
106 {
107  int ret = sem_post (&sem);
108  if (ret != 0)
109  LOG4CPLUS_THROW_RTE ("Semaphore::unlock");
110 }
111 
112 
113 inline
114 void
115 Semaphore::lock () const
116 {
117  int ret = sem_wait (&sem);
118  if (ret != 0)
119  LOG4CPLUS_THROW_RTE ("Semaphore::lock");
120 }
121 
122 
123 //
124 //
125 //
126 
127 inline
128 ManualResetEvent::ManualResetEvent (bool sig)
129  : sigcount (0)
130  , signaled (sig)
131 {
132  int ret = pthread_cond_init (&cv, 0);
133  if (ret != 0)
134  LOG4CPLUS_THROW_RTE ("ManualResetEvent::ManualResetEvent");
135 }
136 
137 
138 inline
139 ManualResetEvent::~ManualResetEvent ()
140 try
141 {
142  int ret = pthread_cond_destroy (&cv);
143  if (ret != 0)
144  LOG4CPLUS_THROW_RTE ("ManualResetEvent::~ManualResetEvent");
145 }
146 catch (...)
147 { }
148 
149 
150 inline
151 void
152 ManualResetEvent::signal () const
153 {
154  MutexGuard mguard (mtx);
155 
156  signaled = true;
157  sigcount += 1;
158  int ret = pthread_cond_broadcast (&cv);
159  if (ret != 0)
160  LOG4CPLUS_THROW_RTE ("ManualResetEVent::signal");
161 
162 }
163 
164 
165 inline
166 void
167 ManualResetEvent::wait () const
168 {
169  MutexGuard mguard (mtx);
170 
171  if (! signaled)
172  {
173  unsigned prev_count = sigcount;
174  do
175  {
176  int ret = pthread_cond_wait (&cv, &mtx.mtx);
177  if (ret != 0)
178  {
179  mguard.unlock ();
180  mguard.detach ();
181  LOG4CPLUS_THROW_RTE ("ManualResetEvent::wait");
182  }
183  }
184  while (prev_count == sigcount);
185  }
186 }
187 
188 
189 inline
190 bool
191 ManualResetEvent::timed_wait (unsigned long msec) const
192 {
193  MutexGuard mguard (mtx);
194 
195  if (! signaled)
196  {
197  helpers::Time const wakeup_time (helpers::Time::gettimeofday ()
198  + helpers::Time (msec / 1000, (msec % 1000) * 1000));
199  struct timespec const ts = {wakeup_time.sec (),
200  wakeup_time.usec () * 1000};
201  unsigned prev_count = sigcount;
202  do
203  {
204  int ret = pthread_cond_timedwait (&cv, &mtx.mtx, &ts);
205  switch (ret)
206  {
207  case 0:
208  break;
209 
210  case ETIMEDOUT:
211  return false;
212 
213  default:
214  mguard.unlock ();
215  mguard.detach ();
216  LOG4CPLUS_THROW_RTE ("ManualResetEvent::timed_wait");
217  }
218  }
219  while (prev_count == sigcount);
220  }
221 
222  return true;
223 }
224 
225 
226 inline
227 void
228 ManualResetEvent::reset () const
229 {
230  MutexGuard mguard (mtx);
231 
232  signaled = false;
233 }
234 
235 
236 #undef LOG4CPLUS_THROW_RTE
237 
238 
239 } } // namespace log4cplus { namespace thread {
static Time gettimeofday()
Returns the current time using the gettimeofday() method if it is available on the current platform...


Generated on Thu Aug 27 2015 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.9.1