Mir
Loading...
Searching...
No Matches
synchronised.h
Go to the documentation of this file.
1/*
2 * Copyright © Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 2 or 3,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#ifndef MIR_SYNCHRONISED_H_
18#define MIR_SYNCHRONISED_H_
19
20#include <mutex>
21
22namespace mir
23{
24
33template<typename T>
35{
36public:
37 Synchronised() = default;
38 Synchronised(T&& initial_value)
39 : value{std::move(initial_value)}
40 {
41 }
42
43 Synchronised(Synchronised const&) = delete;
45
54 template<typename U>
56 {
57 public:
58 LockedImpl(LockedImpl&& from) noexcept
59 : value{from.value},
60 lock{std::move(from.lock)}
61 {
62 from.value = nullptr;
63 }
64
65 ~LockedImpl() = default;
66
67 auto operator*() const -> U&
68 {
69 return *value;
70 }
71
72 auto operator->() const -> U*
73 {
74 return value;
75 }
76
83 void drop()
84 {
85 value = nullptr;
86 lock.unlock();
87 }
88
94 template<typename Cv, typename Predicate>
95 void wait(Cv& cv, Predicate stop_waiting)
96 {
97 cv.wait(lock, stop_waiting);
98 }
99
100 private:
101 friend class Synchronised;
102 LockedImpl(std::unique_lock<std::mutex>&& lock, U& value)
103 : value{&value},
104 lock{std::move(lock)}
105 {
106 }
107
108 U* value;
109 std::unique_lock<std::mutex> lock;
110 };
111
128 auto lock() -> Locked
129 {
130 return LockedImpl<T>{std::unique_lock{mutex}, value};
131 }
132
151 auto lock() const -> LockedView
152 {
153 return LockedImpl<T const>{std::unique_lock{mutex}, value};
154 }
155private:
156 std::mutex mutable mutex;
157 T value;
158};
159
160}
161
162#endif //MIR_SYNCHRONISED_H_
Smart-pointer-esque accessor for the protected data.
Definition synchronised.h:56
void wait(Cv &cv, Predicate stop_waiting)
Allows waiting for a condition variable.
Definition synchronised.h:95
LockedImpl(LockedImpl &&from) noexcept
Definition synchronised.h:58
void drop()
Relinquish access to the data.
Definition synchronised.h:83
auto operator->() const -> U *
Definition synchronised.h:72
auto operator*() const -> U &
Definition synchronised.h:67
An object that enforces unique access to the data it contains.
Definition synchronised.h:35
auto lock() const -> LockedView
Lock the data and return an accessor.
Definition synchronised.h:151
Synchronised()=default
Synchronised(T &&initial_value)
Definition synchronised.h:38
Synchronised & operator=(Synchronised const &)=delete
auto lock() -> Locked
Lock the data and return an accessor.
Definition synchronised.h:128
Synchronised(Synchronised const &)=delete
Definition splash_session.h:22
STL namespace.

Copyright © 2012-2025 Canonical Ltd.
Generated on Mon Jan 20 23:24:07 UTC 2025
This documentation is licensed under the GPL version 2 or 3.