1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (C) 2020-2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef READWRITE_MUTEX_H
#define READWRITE_MUTEX_H

/// @file readwrite_mutex.h
///
/// Standard implementation of read-write mutexes with writer preference
/// using C++11 mutex and condition variable.
/// As we need only the RAII wrappers implement only used methods.

#include <exceptions/exceptions.h>
#include <boost/noncopyable.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <climits><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <condition_variable><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <mutex><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace util {

/// @brief Read-Write Mutex.
///
/// The code is based on Howard Hinnant's reference implementation
/// for C++17 shared_mutex.
class ReadWriteMutex : public boost::noncopyable {
public:

    /// Constants.

    /// @brief The write entered flag (higher bit so 2^31).
    static const unsigned WRITE_ENTERED =
        1U << (sizeof(unsigned) * CHAR_BIT - 1);

    /// @brief The maximum number of readers (flag complement so 2^31 - 1).
    static const unsigned MAX_READERS = ~WRITE_ENTERED;

    /// @brief Constructor.
    ReadWriteMutex() : state_(0) {
    }

    /// @brief Destructor.
    ///
    /// @note: do not check that state is 0 as there is nothing very
    /// useful to do in this case...
    virtual ~ReadWriteMutex() {
        std::lock_guard<std::mutex> lk(mutex_);
    }

    /// @brief Lock write.
    void writeLock() {
        std::unique_lock<std::mutex> lk(mutex_);
        // Wait until the write entered flag can be set.
        gate1_.wait(lk, [&]() { return (!writeEntered()); });
        state_ |= WRITE_ENTERED;
        // Wait until there are no more readers.
        gate2_.wait(lk, [&]() { return (readers() == 0); });
    }

    /// @brief Unlock write.
    ///
    /// @note: do not check that WRITE_ENTERED was set.
    void writeUnlock() {
        std::lock_guard<std::mutex> lk(mutex_);
        state_ = 0;
        // Wake-up waiting threads when exiting the guard.
        gate1_.notify_all();
    }

    /// @brief Lock read.
    void readLock() {
        std::unique_lock<std::mutex> lk(mutex_);
        // Wait if there is a writer or if readers overflow.
        gate1_.wait(lk, [&]() { return (state_ < MAX_READERS); });
        ++state_;
    }

    /// @brief Unlock read.
    ///
    /// @note: do not check that there is a least one reader.
    void readUnlock() {
        std::lock_guard<std::mutex> lk(mutex_);
        unsigned prev = state_--;
        if (writeEntered()) {
            if (readers() == 0) {
                // Last reader: wake up a waiting writer.
                gate2_.notify_one();
            }
        } else {
            if (prev == MAX_READERS) {
                // Reader overflow: wake up one waiting reader.
                gate1_.notify_one();
            }
        }
    }

private:

    /// Helpers.

    /// @brief Check if the write entered flag is set.
    bool writeEntered() const {
        return (state_ & WRITE_ENTERED);
    }

    /// @brief Return the number of readers.
    unsigned readers() const {
        return (state_ & MAX_READERS);
    }

    /// Members.

    /// @brief Mutex.
    ///
    /// Used to protect the state and in condition variables.
    std::mutex mutex_;

    /// @brief First condition variable.
    ///
    /// Used to block while the write entered flag is set or readers overflow.
    std::condition_variable gate1_;

    /// @brief Second condition variable.
    ///
    /// Used to block writers until the reader count decrements to zero.
    std::condition_variable gate2_;

    /// @brief State.
    ///
    /// Used to handle the write entered flag and the reader count.
    unsigned state_;
};

/// @brief Read mutex RAII handler.
///
/// The constructor acquires the lock, the destructor releases it.
class ReadLockGuard : public boost::noncopyable {
public:

    /// @brief Constructor.
    ///
    /// @param rw_mutex The read mutex.
    ReadLockGuard(ReadWriteMutex& rw_mutex) : rw_mutex_(rw_mutex) {
        rw_mutex_.readLock();
    }

    /// @brief Destructor.
    virtual ~ReadLockGuard() {
        rw_mutex_.readUnlock();
    }

private:
    /// @brief The read-write mutex.
    ReadWriteMutex& rw_mutex_;

};

/// @brief Write mutex RAII handler.
///
/// The constructor acquires the lock, the destructor releases it.
class WriteLockGuard : public boost::noncopyable {
public:

    /// @brief Constructor.
    ///
    /// @param rw_mutex The write mutex.
    WriteLockGuard(ReadWriteMutex& rw_mutex) : rw_mutex_(rw_mutex) {
        rw_mutex_.writeLock();
    }

    /// @brief Destructor.
    virtual ~WriteLockGuard() {
        rw_mutex_.writeUnlock();
    }

private:
    /// @brief The read-write mutex.
    ReadWriteMutex& rw_mutex_;
};

} // namespace util
} // namespace isc

#endif // READWRITE_MUTEX_H