OFFIS DCMTK  Version 3.6.0
syncprims.h
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 
24 #ifndef LOG4CPLUS_THREAD_SYNCPRIMS_H
25 #define LOG4CPLUS_THREAD_SYNCPRIMS_H
26 
27 //#include <stdexcept>
28 #include "dcmtk/oflog/config.h"
29 #if defined (LOG4CPLUS_USE_PTHREADS)
30 # define INCLUDE_CERRNO
31 # include "dcmtk/ofstd/ofstdinc.h"
32 
33 # include <pthread.h>
34 # include <semaphore.h>
36 
37 #elif defined (LOG4CPLUS_USE_WIN32_THREADS)
38 # undef WIN32_LEAN_AND_MEAN
39 # define WIN32_LEAN_AND_MEAN
40 # include <windows.h>
41 
42 #endif
43 
44 
45 namespace log4cplus { namespace thread {
46 
47 
48 namespace detail
49 {
50 
51 LOG4CPLUS_EXPORT void syncprims_throw_exception (char const * const msg,
52  char const * const file, int line);
53 
54 } // namespace detail
55 
56 
57 template <typename SP>
58 class SyncGuard
59 {
60 public:
61  SyncGuard (SP const &);
62  ~SyncGuard ();
63 
64  void lock ();
65  void unlock ();
66  void attach (SP const &);
67  void detach ();
68 
69 private:
70  SP const * sp;
71 
72  SyncGuard (SyncGuard const &);
73  SyncGuard & operator = (SyncGuard const &);
74 };
75 
76 
77 class ManualResetEvent;
78 
79 
80 class Mutex
81 {
82 public:
83  Mutex ();
84  ~Mutex ();
85 
86  void lock () const;
87  void unlock () const;
88 
89 private:
90 #if defined (LOG4CPLUS_USE_PTHREADS)
91  mutable pthread_mutex_t mtx;
92  friend class ManualResetEvent;
93 #elif defined (LOG4CPLUS_USE_WIN32_THREADS)
94  mutable CRITICAL_SECTION cs;
95 #endif
96 
97  Mutex (Mutex const &);
98  Mutex & operator = (Mutex &);
99 };
100 
101 
103 
104 
106 {
107 public:
108  Semaphore (unsigned max, unsigned initial);
109  ~Semaphore ();
110 
111  void lock () const;
112  void unlock () const;
113 
114 private:
115 #if defined (LOG4CPLUS_USE_PTHREADS)
116  mutable sem_t sem;
117 #elif defined (LOG4CPLUS_USE_WIN32_THREADS)
118  HANDLE sem;
119 #endif
120 
121  Semaphore (Semaphore const &);
122  Semaphore & operator = (Semaphore const &);
123 };
124 
125 
127 
128 
130 {
131 public:
132  ManualResetEvent (bool = false);
133  ~ManualResetEvent ();
134 
135  void signal () const;
136  void wait () const;
137  bool timed_wait (unsigned long msec) const;
138  void reset () const;
139 
140 private:
141 #if defined (LOG4CPLUS_USE_PTHREADS)
142  mutable pthread_cond_t cv;
143  mutable Mutex mtx;
144  mutable volatile unsigned sigcount;
145  mutable volatile bool signaled;
146 #elif defined (LOG4CPLUS_USE_WIN32_THREADS)
147  HANDLE ev;
148 #endif
149 
151  ManualResetEvent & operator = (ManualResetEvent const &);
152 };
153 
154 
155 } } // namespace log4cplus { namespace thread {
156 
157 
158 // Include the appropriate implementations of the classes declared
159 // above.
160 
161 #if defined (LOG4CPLUS_USE_PTHREADS)
163 #elif defined (LOG4CPLUS_USE_WIN32_THREADS)
165 #endif
166 
167 
168 namespace log4cplus { namespace thread {
169 
170 
171 //
172 //
173 //
174 
175 template <typename SP>
176 inline
177 SyncGuard<SP>::SyncGuard (SP const & m)
178  : sp (&m)
179 {
180  sp->lock ();
181 }
182 
183 
184 template <typename SP>
185 inline
186 SyncGuard<SP>::~SyncGuard ()
187 {
188  if (sp)
189  sp->unlock ();
190 }
191 
192 
193 template <typename SP>
194 inline
195 void
196 SyncGuard<SP>::lock ()
197 {
198  sp->lock ();
199 }
200 
201 
202 template <typename SP>
203 inline
204 void
205 SyncGuard<SP>::unlock ()
206 {
207  sp->unlock ();
208 }
209 
210 
211 template <typename SP>
212 inline
213 void
214 SyncGuard<SP>::attach (SP const & m)
215 {
216  sp = &m;
217 }
218 
219 
220 template <typename SP>
221 inline
222 void
223 SyncGuard<SP>::detach ()
224 {
225  sp = 0;
226 }
227 
228 
229 } } // namespace log4cplus { namespace thread {
230 
231 
232 
233 #endif // LOG4CPLUS_THREAD_SYNCPRIMS_H
This file contains implementations of synchronization primitives using the POSIX threads.
This file contains implementations of synchronization primitives using the Win32 API.


Generated on Sun Aug 23 2015 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.9.1