Sunshine v2025.118.151840
Self-hosted game stream host for Moonlight.
sync.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <array>
8#include <mutex>
9#include <utility>
10
11namespace sync_util {
12
13 template <class T, class M = std::mutex>
14 class sync_t {
15 public:
16 using value_t = T;
17 using mutex_t = M;
18
19 std::lock_guard<mutex_t>
20 lock() {
21 return std::lock_guard { _lock };
22 }
23
24 template <class... Args>
25 sync_t(Args &&...args):
26 raw { std::forward<Args>(args)... } {}
27
28 sync_t &
29 operator=(sync_t &&other) noexcept {
30 std::lock(_lock, other._lock);
31
32 raw = std::move(other.raw);
33
34 _lock.unlock();
35 other._lock.unlock();
36
37 return *this;
38 }
39
40 sync_t &
41 operator=(sync_t &other) noexcept {
42 std::lock(_lock, other._lock);
43
44 raw = other.raw;
45
46 _lock.unlock();
47 other._lock.unlock();
48
49 return *this;
50 }
51
52 template <class V>
53 sync_t &
54 operator=(V &&val) {
55 auto lg = lock();
56
57 raw = val;
58
59 return *this;
60 }
61
62 sync_t &
63 operator=(const value_t &val) noexcept {
64 auto lg = lock();
65
66 raw = val;
67
68 return *this;
69 }
70
71 sync_t &
72 operator=(value_t &&val) noexcept {
73 auto lg = lock();
74
75 raw = std::move(val);
76
77 return *this;
78 }
79
80 value_t *
81 operator->() {
82 return &raw;
83 }
84
85 value_t &
86 operator*() {
87 return raw;
88 }
89
90 const value_t &
91 operator*() const {
92 return raw;
93 }
94
95 value_t raw;
96
97 private:
98 mutex_t _lock;
99 };
100
101} // namespace sync_util
Definition sync.h:14
Functions for handling command line arguments.
Definition entry_handler.cpp:41