log4cplus  2.1.0
syncprims.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // Copyright (C) 2010-2017, Vaclav Haisman. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without modifica-
5 // tion, are permitted provided that the following conditions are met:
6 //
7 // 1. Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
15 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
17 // APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
19 // DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
20 // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 #ifndef LOG4CPLUS_THREAD_SYNCPRIMS_H
26 #define LOG4CPLUS_THREAD_SYNCPRIMS_H
27 
28 #include <log4cplus/config.hxx>
29 
30 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
31 #pragma once
32 #endif
33 
34 #include <mutex>
35 #include <condition_variable>
36 
37 
38 namespace log4cplus { namespace thread {
39 
40 
41 template <typename SyncPrim>
42 class SyncGuard
43 {
44 public:
45  SyncGuard ();
46  SyncGuard (SyncPrim const &);
47  ~SyncGuard ();
48  SyncGuard (SyncGuard const &) = delete;
49  SyncGuard & operator = (SyncGuard const &) = delete;
50 
51 
52  void lock ();
53  void unlock ();
54  void attach (SyncPrim const &);
55  void attach_and_lock (SyncPrim const &);
56  void detach ();
57 
58 private:
59  SyncPrim const * sp;
60 };
61 
62 
64 {
65 public:
66  Mutex ();
67  ~Mutex ();
68  Mutex (Mutex const &) = delete;
69  Mutex & operator = (Mutex const &) = delete;
70 
71  void lock () const;
72  void unlock () const;
73 
74 private:
75  LOG4CPLUS_THREADED (mutable std::recursive_mutex mtx;)
76 };
77 
78 
80 
81 
83 {
84 public:
85  Semaphore (unsigned max, unsigned initial);
87  Semaphore (Semaphore const &) = delete;
88  Semaphore & operator = (Semaphore const &) = delete;
89 
90  void lock () const;
91  void unlock () const;
92 
93 private:
94 #if ! defined (LOG4CPLUS_SINGLE_THREADED)
95  mutable std::mutex mtx;
96  mutable std::condition_variable cv;
97  mutable unsigned maximum;
98  mutable unsigned val;
99 #endif
100 };
101 
102 
104 
105 
107 {
108 public:
109  explicit ManualResetEvent (bool = false);
111  ManualResetEvent (ManualResetEvent const &) = delete;
112  ManualResetEvent & operator = (ManualResetEvent const &) = delete;
113 
114  void signal () const;
115  void wait () const;
116  bool timed_wait (unsigned long msec) const;
117  void reset () const;
118 
119 private:
120 #if ! defined (LOG4CPLUS_SINGLE_THREADED)
121  mutable std::mutex mtx;
122  mutable std::condition_variable cv;
123  mutable bool signaled;
124  mutable unsigned sigcount;
125 #endif
126 };
127 
128 
130 {
131 protected:
133 };
134 
135 
136 template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
137  void (SyncPrim:: * unlock_func) () const>
139 {
140 public:
141  SyncGuardFunc (SyncPrim const &);
142  ~SyncGuardFunc ();
143 
144  void lock ();
145  void unlock ();
146  void attach (SyncPrim const &);
147  void detach ();
148 
149 private:
150  SyncPrim const * sp;
151 
152  SyncGuardFunc (SyncGuardFunc const &);
153  SyncGuardFunc & operator = (SyncGuardFunc const &);
154 };
155 
156 
158 {
159 public:
160  SharedMutex ();
161  ~SharedMutex ();
162 
163  void rdlock () const;
164  void rdunlock () const;
165 
166  void wrlock () const;
167  void wrunlock () const;
168 
169 private:
170  SharedMutexImplBase * sm;
171 
172  SharedMutex (SharedMutex const &);
173  SharedMutex & operator = (SharedMutex const &);
174 };
175 
176 
179 
180 
183 
184 
185 //
186 //
187 //
188 
189 template <typename SyncPrim>
190 inline
192  : sp (0)
193 { }
194 
195 
196 template <typename SyncPrim>
197 inline
198 SyncGuard<SyncPrim>::SyncGuard (SyncPrim const & m)
199  : sp (&m)
200 {
201  sp->lock ();
202 }
203 
204 
205 template <typename SyncPrim>
206 inline
208 {
209  if (sp)
210  sp->unlock ();
211 }
212 
213 
214 template <typename SyncPrim>
215 inline
216 void
218 {
219  sp->lock ();
220 }
221 
222 
223 template <typename SyncPrim>
224 inline
225 void
227 {
228  sp->unlock ();
229 }
230 
231 
232 template <typename SyncPrim>
233 inline
234 void
235 SyncGuard<SyncPrim>::attach (SyncPrim const & m)
236 {
237  sp = &m;
238 }
239 
240 
241 template <typename SyncPrim>
242 inline
243 void
245 {
246  attach (m);
247  try
248  {
249  lock();
250  }
251  catch (...)
252  {
253  detach ();
254  throw;
255  }
256 }
257 
258 
259 template <typename SyncPrim>
260 inline
261 void
263 {
264  sp = 0;
265 }
266 
267 
268 //
269 //
270 //
271 
272 template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
273  void (SyncPrim:: * unlock_func) () const>
274 inline
276  : sp (&m)
277 {
278  (sp->*lock_func) ();
279 }
280 
281 
282 template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
283  void (SyncPrim:: * unlock_func) () const>
284 inline
286 {
287  if (sp)
288  (sp->*unlock_func) ();
289 }
290 
291 
292 template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
293  void (SyncPrim:: * unlock_func) () const>
294 inline
295 void
297 {
298  (sp->*lock_func) ();
299 }
300 
301 
302 template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
303  void (SyncPrim:: * unlock_func) () const>
304 inline
305 void
307 {
308  (sp->*unlock_func) ();
309 }
310 
311 
312 template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
313  void (SyncPrim:: * unlock_func) () const>
314 inline
315 void
317 {
318  sp = &m;
319 }
320 
321 
322 template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
323  void (SyncPrim:: * unlock_func) () const>
324 inline
325 void
327 {
328  sp = 0;
329 }
330 
331 
332 } } // namespace log4cplus { namespace thread {
333 
334 
335 #endif // LOG4CPLUS_THREAD_SYNCPRIMS_H
ManualResetEvent(ManualResetEvent const &)=delete
bool timed_wait(unsigned long msec) const
Mutex(Mutex const &)=delete
Semaphore(unsigned max, unsigned initial)
Semaphore(Semaphore const &)=delete
SyncGuard & operator=(SyncGuard const &)=delete
void attach_and_lock(SyncPrim const &)
Definition: syncprims.h:244
SyncGuard(SyncGuard const &)=delete
void attach(SyncPrim const &)
Definition: syncprims.h:235
#define LOG4CPLUS_THREADED(x)
Definition: config.hxx:178
SyncGuardFunc< SharedMutex, &SharedMutex::rdlock, &SharedMutex::rdunlock > SharedMutexReaderGuard
Definition: syncprims.h:178
SyncGuard< Mutex > MutexGuard
Definition: syncprims.h:79
SyncGuardFunc< SharedMutex, &SharedMutex::wrlock, &SharedMutex::wrunlock > SharedMutexWriterGuard
Definition: syncprims.h:182
SyncGuard< Semaphore > SemaphoreGuard
Definition: syncprims.h:103
#define LOG4CPLUS_EXPORT
Definition: win32.h:141