log4cplus 2.0.8
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:
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
58private:
59 SyncPrim const * sp;
60};
61
62
64{
65public:
68 Mutex (Mutex const &) = delete;
69 Mutex & operator = (Mutex const &) = delete;
70
71 void lock () const;
72 void unlock () const;
73
74private:
75 LOG4CPLUS_THREADED (mutable std::recursive_mutex mtx;)
76};
77
78
80
81
83{
84public:
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
93private:
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{
108public:
109 explicit ManualResetEvent (bool = false);
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
119private:
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{
131protected:
133};
134
135
136template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
137 void (SyncPrim:: * unlock_func) () const>
139{
140public:
141 SyncGuardFunc (SyncPrim const &);
143
144 void lock ();
145 void unlock ();
146 void attach (SyncPrim const &);
147 void detach ();
148
149private:
150 SyncPrim const * sp;
151
153 SyncGuardFunc & operator = (SyncGuardFunc const &);
154};
155
156
158{
159public:
160 SharedMutex ();
161 ~SharedMutex ();
162
163 void rdlock () const;
164 void rdunlock () const;
165
166 void wrlock () const;
167 void wrunlock () const;
168
169private:
171
172 SharedMutex (SharedMutex const &);
173 SharedMutex & operator = (SharedMutex const &);
174};
175
176
179
180
183
184
185//
186//
187//
188
189template <typename SyncPrim>
190inline
192 : sp (0)
193{ }
194
195
196template <typename SyncPrim>
197inline
199 : sp (&m)
200{
201 sp->lock ();
202}
203
204
205template <typename SyncPrim>
206inline
208{
209 if (sp)
210 sp->unlock ();
211}
212
213
214template <typename SyncPrim>
215inline
216void
218{
219 sp->lock ();
220}
221
222
223template <typename SyncPrim>
224inline
225void
227{
228 sp->unlock ();
229}
230
231
232template <typename SyncPrim>
233inline
234void
235SyncGuard<SyncPrim>::attach (SyncPrim const & m)
236{
237 sp = &m;
238}
239
240
241template <typename SyncPrim>
242inline
243void
245{
246 attach (m);
247 try
248 {
249 lock();
250 }
251 catch (...)
252 {
253 detach ();
254 throw;
255 }
256}
257
258
259template <typename SyncPrim>
260inline
261void
263{
264 sp = 0;
265}
266
267
268//
269//
270//
271
272template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
273 void (SyncPrim:: * unlock_func) () const>
274inline
275SyncGuardFunc<SyncPrim, lock_func, unlock_func>::SyncGuardFunc (SyncPrim const & m)
276 : sp (&m)
277{
278 (sp->*lock_func) ();
279}
280
281
282template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
283 void (SyncPrim:: * unlock_func) () const>
284inline
285SyncGuardFunc<SyncPrim, lock_func, unlock_func>::~SyncGuardFunc ()
286{
287 if (sp)
288 (sp->*unlock_func) ();
289}
290
291
292template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
293 void (SyncPrim:: * unlock_func) () const>
294inline
295void
296SyncGuardFunc<SyncPrim, lock_func, unlock_func>::lock ()
297{
298 (sp->*lock_func) ();
299}
300
301
302template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
303 void (SyncPrim:: * unlock_func) () const>
304inline
305void
306SyncGuardFunc<SyncPrim, lock_func, unlock_func>::unlock ()
307{
308 (sp->*unlock_func) ();
309}
310
311
312template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
313 void (SyncPrim:: * unlock_func) () const>
314inline
315void
316SyncGuardFunc<SyncPrim, lock_func, unlock_func>::attach (SyncPrim const & m)
317{
318 sp = &m;
319}
320
321
322template <typename SyncPrim, void (SyncPrim:: * lock_func) () const,
323 void (SyncPrim:: * unlock_func) () const>
324inline
325void
326SyncGuardFunc<SyncPrim, lock_func, unlock_func>::detach ()
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