Sunshine master
Self-hosted game stream host for Moonlight.
sync.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <array>
9#include <mutex>
10#include <utility>
11
12namespace sync_util {
13
17 template<class T, class M = std::mutex>
18 class sync_t {
19 public:
23 using value_t = T;
27 using mutex_t = M;
28
34 std::lock_guard<mutex_t> lock() {
35 return std::lock_guard {_lock};
36 }
37
43 template<class... Args>
44 sync_t(Args &&...args):
45 raw {std::forward<Args>(args)...} {
46 }
47
54 sync_t &operator=(sync_t &&other) noexcept {
55 std::lock(_lock, other._lock);
56
57 raw = std::move(other.raw);
58
59 _lock.unlock();
60 other._lock.unlock();
61
62 return *this;
63 }
64
71 sync_t &operator=(sync_t &other) noexcept {
72 std::lock(_lock, other._lock);
73
74 raw = other.raw;
75
76 _lock.unlock();
77 other._lock.unlock();
78
79 return *this;
80 }
81
88 template<class V>
89 sync_t &operator=(V &&val) {
90 auto lg = lock();
91
92 raw = val;
93
94 return *this;
95 }
96
103 sync_t &operator=(const value_t &val) noexcept {
104 auto lg = lock();
105
106 raw = val;
107
108 return *this;
109 }
110
117 sync_t &operator=(value_t &&val) noexcept {
118 auto lg = lock();
119
120 raw = std::move(val);
121
122 return *this;
123 }
124
131 return &raw;
132 }
133
140 return raw;
141 }
142
148 const value_t &operator*() const {
149 return raw;
150 }
151
153
154 private:
155 mutex_t _lock;
156 };
157
158} // namespace sync_util
Value wrapper that pairs an object with the mutex protecting it.
Definition sync.h:18
sync_t & operator=(sync_t &other) noexcept
Copy another synchronized value into this instance while locking both wrappers.
Definition sync.h:71
sync_t & operator=(sync_t &&other) noexcept
Move another synchronized value into this instance while locking both wrappers.
Definition sync.h:54
sync_t & operator=(value_t &&val) noexcept
Move-assign the protected value while holding this wrapper's lock.
Definition sync.h:117
T value_t
Type stored behind the synchronization wrapper.
Definition sync.h:23
const value_t & operator*() const
Dereference the protected value.
Definition sync.h:148
M mutex_t
Mutex type used to protect the stored value.
Definition sync.h:27
value_t raw
Protected value accessed while this helper's lock is held.
Definition sync.h:152
value_t & operator*()
Dereference the protected value.
Definition sync.h:139
std::lock_guard< mutex_t > lock()
Acquire the underlying lock or keyed mutex.
Definition sync.h:34
sync_t(Args &&...args)
Initialize the protected object from forwarded arguments.
Definition sync.h:44
sync_t & operator=(V &&val)
Assign a new value while holding this wrapper's lock.
Definition sync.h:89
value_t * operator->()
Access the protected value directly.
Definition sync.h:130
sync_t & operator=(const value_t &val) noexcept
Copy-assign the protected value while holding this wrapper's lock.
Definition sync.h:103
Functions for handling command line arguments.
Definition entry_handler.cpp:37