log4cplus 2.1.2
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
38namespace log4cplus { namespace thread {
39
40
41template <typename SyncPrim>
43{
44public:
46 SyncGuard (SyncPrim const &);
48 SyncGuard (SyncGuard const &) = delete;
49 SyncGuard & operator = (SyncGuard const &) = delete;
50
51 void lock ();
52 void unlock ();
53 void attach (SyncPrim const &);
54 void attach_and_lock (SyncPrim const &);
55 void detach ();
56
57private:
58 SyncPrim const * sp;
59};
60
61
63{
64public:
67 SimpleMutex (SimpleMutex const &) = delete;
68 SimpleMutex & operator = (SimpleMutex const &) = delete;
69
70 void lock () const;
71 bool try_lock () const;
72 void unlock () const;
73
74private:
75 LOG4CPLUS_THREADED (mutable std::mutex mtx;)
76};
77
78
80
81
83{
84public:
87 Mutex (Mutex const &) = delete;
88 Mutex & operator = (Mutex const &) = delete;
89
90 void lock () const;
91 void unlock () const;
92
93private:
94 LOG4CPLUS_THREADED (mutable std::recursive_mutex mtx;)
95};
96
97
99
100
102{
103public:
104 Semaphore (unsigned max, unsigned initial);
106 Semaphore (Semaphore const &) = delete;
107 Semaphore & operator = (Semaphore const &) = delete;
108
109 void lock () const;
110 void unlock () const;
111
112private:
113#if ! defined (LOG4CPLUS_SINGLE_THREADED)
114 mutable std::mutex mtx;
115 mutable std::condition_variable cv;
116 mutable unsigned maximum;
117 mutable unsigned val;
118#endif
119};
120
121
123
124
126{
127public:
128 explicit ManualResetEvent (bool = false);
131 ManualResetEvent & operator = (ManualResetEvent const &) = delete;
132
133 void signal () const;
134 void wait () const;
135 bool timed_wait (unsigned long msec) const;
136 void reset () const;
137
138private:
139#if ! defined (LOG4CPLUS_SINGLE_THREADED)
140 mutable std::mutex mtx;
141 mutable std::condition_variable cv;
142 mutable bool signaled;
143 mutable unsigned sigcount;
144#endif
145};
146
147
149{
150protected:
152};
153
154
155template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
156 void (SyncPrim:: * unlock_func) () const>
158{
159public:
160 SyncGuardFunc (SyncPrim const &);
162
163 void lock ();
164 void unlock ();
165 void attach (SyncPrim const &);
166 void detach ();
167
168private:
169 SyncPrim const * sp;
170
172 SyncGuardFunc & operator = (SyncGuardFunc const &);
173};
174
175
177{
178public:
179 SharedMutex ();
180 ~SharedMutex ();
181
182 void rdlock () const;
183 void rdunlock () const;
184
185 void wrlock () const;
186 void wrunlock () const;
187
188private:
190
191 SharedMutex (SharedMutex const &);
192 SharedMutex & operator = (SharedMutex const &);
193};
194
195
198
199
202
203
204//
205//
206//
207
208template <typename SyncPrim>
209inline
211 : sp (0)
212{ }
213
214
215template <typename SyncPrim>
216inline
218 : sp (&m)
219{
220 sp->lock ();
221}
222
223
224template <typename SyncPrim>
225inline
227{
228 if (sp)
229 sp->unlock ();
230}
231
232
233template <typename SyncPrim>
234inline
235void
237{
238 sp->lock ();
239}
240
241
242template <typename SyncPrim>
243inline
244void
246{
247 sp->unlock ();
248}
249
250
251template <typename SyncPrim>
252inline
253void
254SyncGuard<SyncPrim>::attach (SyncPrim const & m)
255{
256 sp = &m;
257}
258
259
260template <typename SyncPrim>
261inline
262void
264{
265 attach (m);
266 try
267 {
268 lock();
269 }
270 catch (...)
271 {
272 detach ();
273 throw;
274 }
275}
276
277
278template <typename SyncPrim>
279inline
280void
282{
283 sp = 0;
284}
285
286
287//
288//
289//
290
291template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
292 void (SyncPrim:: * unlock_func) () const>
293inline
295 : sp (&m)
296{
297 (sp->*lock_func) ();
298}
299
300
301template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
302 void (SyncPrim:: * unlock_func) () const>
303inline
305{
306 if (sp)
307 (sp->*unlock_func) ();
308}
309
310
311template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
312 void (SyncPrim:: * unlock_func) () const>
313inline
314void
316{
317 (sp->*lock_func) ();
318}
319
320
321template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
322 void (SyncPrim:: * unlock_func) () const>
323inline
324void
326{
327 (sp->*unlock_func) ();
328}
329
330
331template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
332 void (SyncPrim:: * unlock_func) () const>
333inline
334void
336{
337 sp = &m;
338}
339
340
341template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
342 void (SyncPrim:: * unlock_func) () const>
343inline
344void
349
350
351} } // namespace log4cplus { namespace thread {
352
353
354#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
SimpleMutex(SimpleMutex const &)=delete
SyncGuardFunc(SyncPrim const &)
Definition syncprims.h:294
void attach(SyncPrim const &)
Definition syncprims.h:335
SyncGuard & operator=(SyncGuard const &)=delete
void attach_and_lock(SyncPrim const &)
Definition syncprims.h:263
SyncGuard(SyncPrim const &)
Definition syncprims.h:217
SyncGuard(SyncGuard const &)=delete
void attach(SyncPrim const &)
Definition syncprims.h:254
#define LOG4CPLUS_THREADED(x)
Definition config.hxx:178
SyncGuardFunc< SharedMutex, &SharedMutex::rdlock, &SharedMutex::rdunlock > SharedMutexReaderGuard
Definition syncprims.h:197
SyncGuard< Mutex > MutexGuard
Definition syncprims.h:98
SyncGuard< SimpleMutex > SimpleMutexGuard
Definition syncprims.h:79
SyncGuardFunc< SharedMutex, &SharedMutex::wrlock, &SharedMutex::wrunlock > SharedMutexWriterGuard
Definition syncprims.h:201
SyncGuard< Semaphore > SemaphoreGuard
Definition syncprims.h:122
#define LOG4CPLUS_EXPORT
Definition win32.h:141