Coverage Report

Created: 2026-07-14 08:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/log4cplus/include/log4cplus/thread/impl/syncprims-pmsm.h
Line
Count
Source
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
//! @file
26
//! This file contains implementations of reader-writer locking
27
//! primitive using other primitives, IOW poor man's rwlock.
28
//! It does not contain any include guards because it is only a fragment
29
//! to be included by syncprims-{pthreads,win32}.h.
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
42
inline
43
SharedMutex::SharedMutex ()
44
0
    : m1 ()
45
0
    , m2 ()
46
0
    , m3 ()
47
0
    , w (1, 1)
48
0
    , writer_count (0)
49
0
    , r (1, 1)
50
0
    , reader_count (0)
51
0
{ }
52
53
54
inline
55
SharedMutex::~SharedMutex ()
56
0
{ }
57
58
59
inline
60
void
61
SharedMutex::rdlock () const
62
0
{
63
0
    MutexGuard m3_guard (m3);
64
0
    SemaphoreGuard r_guard (r);
65
0
    MutexGuard m1_guard (m1);
66
0
    if (reader_count + 1 == 1)
67
0
        w.lock ();
68
69
0
    reader_count += 1;
70
0
}
71
72
73
inline
74
void
75
SharedMutex::rdunlock () const
76
0
{
77
0
    MutexGuard m1_guard (m1);
78
0
    if (reader_count - 1 == 0)
79
0
        w.unlock ();
80
81
0
    reader_count -= 1;
82
0
}
83
84
85
inline
86
void
87
SharedMutex::wrlock () const
88
0
{
89
0
    {
90
0
        MutexGuard m2_guard (m2);
91
0
        if (writer_count + 1 == 1)
92
0
            r.lock ();
93
94
0
        writer_count += 1;
95
0
    }
96
0
    try
97
0
    {
98
0
        w.lock ();
99
0
    }
100
0
    catch (...)
101
0
    {
102
0
        MutexGuard m2_guard (m2);
103
0
        writer_count -= 1;
104
0
        throw;
105
0
    }
106
0
}
107
108
109
inline
110
void
111
SharedMutex::wrunlock () const
112
0
{
113
0
    w.unlock ();
114
0
    MutexGuard m2_guard (m2);
115
0
    if (writer_count - 1 == 0)
116
0
        r.unlock ();
117
118
0
    writer_count -= 1;
119
0
}