summaryrefslogtreecommitdiff
path: root/src/mongo/util/synchronized_value.h
blob: 75bc29c4101f8764f2e872265f34284940005328 (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
/**
 *    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/platform/mutex.h"
#include "mongo/util/hierarchical_acquisition.h"

namespace mongo {

template <int level = 0>
struct LeveledSynchronizedValueMutexPolicy {
    using mutex_type = Mutex;
    static mutex_type construct() {
        return MONGO_MAKE_LATCH(HierarchicalAcquisitionLevel(level), "synchronized_value::_mutex");
    }
};

struct RawSynchronizedValueMutexPolicy {
    using mutex_type = stdx::mutex;  // NOLINT
    static mutex_type construct() {
        return {};
    }
};


/**
 * Provides mutex guarded access to an object.
 *
 * The protected object can be accessed by either:
 * 1. auto tmp = sv.synchronize()
 *    This is useful if you need to do multiple operations on it
 * 2. operator* or operator->
 *    Creates a temporary that holds the lock for the duration of the complete expression.
 * 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, typename MutexPolicy = LeveledSynchronizedValueMutexPolicy<>>
class synchronized_value {
public:
    using value_type = T;
    using mutex_policy_type = MutexPolicy;

    using mutex_type = typename MutexPolicy::mutex_type;

    template <typename SV>
    class UpdateGuard {
        template <typename SV_>
        using RequiresMutable = std::enable_if_t<!std::is_const_v<SV_>>;

    public:
        explicit UpdateGuard(SV& sv) : _sv(sv), _lock(_sv._mutex) {}

        // Accessors, always available.
        const value_type& operator*() const {
            return _sv._value;
        }
        const value_type* operator->() const {
            return &_sv._value;
        }
        operator value_type() const {
            return **this;
        }

        // Mutators, only available if non-const.
        template <typename SV_ = SV, typename = RequiresMutable<SV_>>
        value_type& operator*() {
            return _sv._value;
        }

        template <typename SV_ = SV, typename = RequiresMutable<SV_>>
        value_type* operator->() {
            return &_sv._value;
        }

        template <typename SV_ = SV, typename = RequiresMutable<SV_>>
        UpdateGuard& operator=(const value_type& v) {
            _sv._value = v;
            return *this;
        }

        template <typename SV_ = SV, typename = RequiresMutable<SV_>>
        UpdateGuard& operator=(value_type&& v) {
            _sv._value = std::move(v);
            return *this;
        }

    private:
        SV& _sv;
        stdx::unique_lock<mutex_type> _lock;
    };

    synchronized_value() = default;
    explicit synchronized_value(value_type value) : _value(std::move(value)) {}

    synchronized_value(const synchronized_value&) = delete;
    synchronized_value& operator=(const synchronized_value&) = delete;

    auto synchronize() const {
        return UpdateGuard<std::remove_reference_t<decltype(*this)>>{*this};
    }

    auto synchronize() {
        return UpdateGuard<std::remove_reference_t<decltype(*this)>>{*this};
    }

    /** Lock and return a holder to the value and lock. Const or non-const. */
    auto operator-> () const {
        return synchronize();
    }
    auto operator*() const {
        return synchronize();
    }
    /** Return a copy of the protected object. */
    value_type get() const {
        return *synchronize();
    }

    /** Mutators */
    auto operator-> () {
        return synchronize();
    }
    auto operator*() {
        return synchronize();
    }

    /** Support assigning from the contained value */
    synchronized_value& operator=(const value_type& v) {
        *synchronize() = v;
        return *this;
    }
    synchronized_value& operator=(value_type&& v) {
        *synchronize() = std::move(v);
        return *this;
    }

    friend bool operator==(const synchronized_value& a, const synchronized_value& b) {
        std::scoped_lock lk(a._mutex, b._mutex);
        return a._value == b._value;
    }
    friend bool operator==(const synchronized_value& a, const value_type& b) {
        return **a == b;
    }
    friend bool operator==(const value_type& a, const synchronized_value& b) {
        return b == a;
    }

    friend bool operator!=(const synchronized_value& a, const synchronized_value& b) {
        return !(a == b);
    }
    friend bool operator!=(const synchronized_value& a, const value_type& b) {
        return !(a == b);
    }
    friend bool operator!=(const value_type& a, const synchronized_value& b) {
        return !(a == b);
    }

private:
    value_type _value;  ///< guarded by _mutex
    mutable mutex_type _mutex = mutex_policy_type::construct();
};

}  // namespace mongo