log4cplus 2.0.8
syncprims-pmsm.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
30
31
32#if ! defined (INSIDE_LOG4CPLUS)
33# error "This header must not be be used outside log4cplus' implementation files."
34#endif
35
36
37// This implements algorithm described in "Concurrent Control with "Readers"
38// and "Writers"; P.J. Courtois, F. Heymans, and D.L. Parnas;
39// MBLE Research Laboratory; Brussels, Belgium"
40
41
42inline
43SharedMutex::SharedMutex ()
44 : m1 ()
45 , m2 ()
46 , m3 ()
47 , w (1, 1)
48 , writer_count (0)
49 , r (1, 1)
50 , reader_count (0)
51{ }
52
53
54inline
56{ }
57
58
59inline
60void
62{
63 MutexGuard m3_guard (m3);
64 SemaphoreGuard r_guard (r);
65 MutexGuard m1_guard (m1);
66 if (reader_count + 1 == 1)
67 w.lock ();
68
69 reader_count += 1;
70}
71
72
73inline
74void
76{
77 MutexGuard m1_guard (m1);
78 if (reader_count - 1 == 0)
79 w.unlock ();
80
81 reader_count -= 1;
82}
83
84
85inline
86void
88{
89 {
90 MutexGuard m2_guard (m2);
91 if (writer_count + 1 == 1)
92 r.lock ();
93
94 writer_count += 1;
95 }
96 try
97 {
98 w.lock ();
99 }
100 catch (...)
101 {
102 MutexGuard m2_guard (m2);
103 writer_count -= 1;
104 throw;
105 }
106}
107
108
109inline
110void
112{
113 w.unlock ();
114 MutexGuard m2_guard (m2);
115 if (writer_count - 1 == 0)
116 r.unlock ();
117
118 writer_count -= 1;
119}