summaryrefslogtreecommitdiff
path: root/src/mongo/util/synchronized_value.h
blob: a49585b942643f29fe435df99542d057bee8f6f7 (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#pragma once

#include "mongo/stdx/mutex.h"

namespace mongo {

namespace details {
/**
 * Complimentary class to synchronized_value. This class is an implementation detail of
 * synchronized_value.
 *
 * Holds a lock and a reference to the value protected by synchronized_value.
 */
template <class T>
class update_guard {
public:
    /**
     * Take lock on construction to guard value.
     */
    explicit update_guard(T& value, stdx::mutex& mtx) : _lock(mtx), _value(value) {}
    ~update_guard() = default;

    // Only move construction is permitted so that synchronized_value may return update_guard
    update_guard(update_guard&&) = default;
    update_guard& operator=(update_guard&&) = default;

    // Disallow any copy
    update_guard(update_guard const&) = delete;
    update_guard& operator=(update_guard const&) = delete;

    T& operator*() noexcept {
        return _value;
    }

    T* operator->() noexcept {
        return &_value;
    }

    update_guard& operator=(const T& value) {
        _value = value;
        return *this;
    }

    update_guard& operator=(T&& value) {
        _value = std::move(value);
        return *this;
    }

    operator T() const {
        return _value;
    }

private:
    // Held lock from synchronized_value
    stdx::unique_lock<stdx::mutex> _lock;

    // Reference to the value from synchronized_value
    T& _value;
};

/**
 * Const version of update_guard
 */
template <class T>
class const_update_guard {
public:
    /**
     * Take lock on construction to guard value.
     */
    explicit const_update_guard(const T& value, stdx::mutex& mtx) : _lock(mtx), _value(value) {}
    ~const_update_guard() = default;

    // Only move construction is permitted so that synchronized_value may return const_update_guard
    const_update_guard(const_update_guard&&) = default;
    const_update_guard& operator=(const_update_guard&&) = default;

    // Disallow any copy
    const_update_guard(const_update_guard const&) = delete;
    const_update_guard& operator=(const_update_guard const&) = delete;

    T& operator*() const noexcept {
        return _value;
    }

    T* operator->() const noexcept {
        return &_value;
    }

    operator T() const {
        return _value;
    }

private:
    // Held lock from synchronized_value
    stdx::unique_lock<stdx::mutex> _lock;

    // Reference to the value from synchronized_value
    const T& _value;
};

}  // namespace details

/**
 * Provides mutex guarded access to an object.
 *
 * The protect object can be accessed by either:
 * 1. auto tmp = sv.synchronize()
 *    This is useful if you need to do multiple operators
 * 2. operator* or operator->
 *    Holds the lock for the duration of the call.
 * 3. get() - makes a copy of the object while the lock is held
 *
 * Inspired by https://isocpp.org/files/papers/n4033.html and boost::synchronized_value
 */
template <typename T>
class synchronized_value {
public:
    synchronized_value() = default;
    explicit synchronized_value(T value) : _value(std::move(value)) {}

    ~synchronized_value() = default;

    // Disallow copy and copy assignment
    synchronized_value(synchronized_value const&) = delete;
    synchronized_value& operator=(synchronized_value const&) = delete;

    // Support assigning from the contained value
    synchronized_value& operator=(const T& value) {
        {
            stdx::lock_guard<stdx::mutex> lock(_mutex);
            _value = value;
        }
        return *this;
    }

    synchronized_value& operator=(T&& value) {
        {
            stdx::lock_guard<stdx::mutex> lock(_mutex);
            _value = std::move(value);
        }
        return *this;
    }

    /**
     * Return a copy of the protected object.
     */
    T get() {
        stdx::lock_guard<stdx::mutex> lock(_mutex);
        return _value;
    }

    details::update_guard<T> operator->() {
        return details::update_guard<T>(_value, _mutex);
    }

    const details::const_update_guard<T> operator->() const {
        return details::update_guard<T>(_value, _mutex);
    }

    details::update_guard<T> operator*() {
        return details::update_guard<T>(_value, _mutex);
    }

    const details::const_update_guard<T> operator*() const {
        return details::const_update_guard<T>(_value, _mutex);
    }

    /**
     * Lock the synchronized_value and return a holder to the value and lock.
     */
    details::update_guard<T> synchronize() {
        return details::update_guard<T>(_value, _mutex);
    }

    bool operator==(synchronized_value const& rhs) const {
        // TODO: C++17 - move from std::lock to std::scoped_lock
        std::lock(_mutex, rhs._mutex);
        stdx::lock_guard<stdx::mutex> lk1(_mutex, stdx::adopt_lock);
        stdx::lock_guard<stdx::mutex> lk2(rhs._mutex, stdx::adopt_lock);
        return _value == rhs._value;
    }

    bool operator!=(synchronized_value const& rhs) const {
        // TODO: C++17 - move from std::lock to std::scoped_lock
        std::lock(_mutex, rhs._mutex);
        stdx::lock_guard<stdx::mutex> lk1(_mutex, stdx::adopt_lock);
        stdx::lock_guard<stdx::mutex> lk2(rhs._mutex, stdx::adopt_lock);
        return _value != rhs._value;
    }

    bool operator==(T const& rhs) const {
        stdx::lock_guard<stdx::mutex> lock1(_mutex);
        return _value == rhs;
    }

    bool operator!=(T const& rhs) const {
        stdx::lock_guard<stdx::mutex> lock1(_mutex);
        return _value != rhs;
    }

    template <class U>
    friend bool operator==(const synchronized_value<U>& lhs, const U& rhs);

    template <class U>
    friend bool operator!=(const synchronized_value<U>& lhs, const U& rhs);

    template <class U>
    friend bool operator==(const U& lhs, const synchronized_value<U>& rhs);

    template <class U>
    friend bool operator!=(const U& lhs, const synchronized_value<U>& rhs);

    template <class U>
    friend bool operator==(const synchronized_value<U>& lhs, const synchronized_value<U>& rhs);

    template <class U>
    friend bool operator!=(const synchronized_value<U>& lhs, const synchronized_value<U>& rhs);

private:
    // Value guarded by mutex
    T _value;

    // Mutex to guard value
    mutable stdx::mutex _mutex;
};

template <class T>
bool operator==(const synchronized_value<T>& lhs, const T& rhs) {
    stdx::lock_guard<stdx::mutex> lock(lhs._mutex);

    return lhs._value == rhs;
}

template <class T>
bool operator!=(const synchronized_value<T>& lhs, const T& rhs) {
    return !(lhs == rhs);
}

template <class T>
bool operator==(const T& lhs, const synchronized_value<T>& rhs) {
    stdx::lock_guard<stdx::mutex> lock(rhs._mutex);

    return lhs == rhs._value;
}

template <class T>
bool operator!=(const T& lhs, const synchronized_value<T>& rhs) {
    return !(lhs == rhs);
}

template <class T>
bool operator==(const synchronized_value<T>& lhs, const synchronized_value<T>& rhs) {
    // TODO: C++17 - move from std::lock to std::scoped_lock
    std::lock(lhs._mutex, rhs._mutex);
    stdx::lock_guard<stdx::mutex> lk1(lhs._mutex, stdx::adopt_lock);
    stdx::lock_guard<stdx::mutex> lk2(rhs._mutex, stdx::adopt_lock);

    return lhs._value == rhs._value;
}

template <class T>
bool operator!=(const synchronized_value<T>& lhs, const synchronized_value<T>& rhs) {
    return !(lhs == rhs);
}

}  // namespace mongo