Debian Codegraph @

core/os/safe_binary_mutex.h

raw symbols SCIP
1 /**************************************************************************/
2 /* safe_binary_mutex.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/error/error_macros.h"
34 #include "core/os/mutex.h"
35 #include "core/typedefs.h"
36
37 #ifdef THREADS_ENABLED
38
39 GODOT_CLANG_WARNING_PUSH_AND_IGNORE("-Wundefined-var-template")
40
41 // A very special kind of mutex, used in scenarios where these
42 // requirements hold at the same time:
43 // - Must be used with a condition variable (only binary mutexes are suitable).
44 // - Must have recursive semnantics (or simulate, as this one does).
45 // The implementation keeps the lock count in TS. Therefore, only
46 // one object of each version of the template can exists; hence the Tag argument.
47 // Tags must be unique across the Godot codebase.
48 // Also, don't forget to declare the thread_local variable on each use.
49 template <int Tag>
50 class SafeBinaryMutex {
51 friend class MutexLock<SafeBinaryMutex<Tag>>;
52
53 using StdMutexType = THREADING_NAMESPACE::mutex;
54
55 mutable THREADING_NAMESPACE::mutex mutex;
56
57 struct TLSData {
58 mutable THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> lock;
59 uint32_t count = 0;
60
61 TLSData(SafeBinaryMutex<Tag> &p_mutex) :
62 lock(p_mutex.mutex, THREADING_NAMESPACE::defer_lock) {}
63 };
64 static thread_local TLSData tls_data;
65
66 public:
67 _ALWAYS_INLINE_ void lock() const {
68 if (++tls_data.count == 1) {
69 tls_data.lock.lock();
70 }
71 }
72
73 _ALWAYS_INLINE_ void unlock() const {
74 DEV_ASSERT(tls_data.count);
75 if (--tls_data.count == 0) {
76 tls_data.lock.unlock();
77 }
78 }
79
80 _ALWAYS_INLINE_ THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &_get_lock() const {
81 return const_cast<THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &>(tls_data.lock);
82 }
83
84 _ALWAYS_INLINE_ SafeBinaryMutex() {
85 }
86
87 _ALWAYS_INLINE_ ~SafeBinaryMutex() {
88 DEV_ASSERT(!tls_data.count);
89 }
90 };
91
92 template <int Tag>
93 class MutexLock<SafeBinaryMutex<Tag>> {
94 friend class ConditionVariable;
95
96 const SafeBinaryMutex<Tag> &mutex;
97
98 public:
99 explicit MutexLock(const SafeBinaryMutex<Tag> &p_mutex) :
100 mutex(p_mutex) {
101 mutex.lock();
102 }
103
104 ~MutexLock() {
105 mutex.unlock();
106 }
107
108 _ALWAYS_INLINE_ void temp_relock() const {
109 mutex.lock();
110 }
111
112 _ALWAYS_INLINE_ void temp_unlock() const {
113 mutex.unlock();
114 }
115
116 // TODO: Implement a `try_temp_relock` if needed (will also need a dummy method below).
117 };
118
119 GODOT_CLANG_WARNING_POP
120
121 #else // No threads.
122
123 template <int Tag>
124 class SafeBinaryMutex {
125 struct TLSData {
126 TLSData(SafeBinaryMutex<Tag> &p_mutex) {}
127 };
128 static thread_local TLSData tls_data;
129
130 public:
131 void lock() const {}
132 void unlock() const {}
133 };
134
135 template <int Tag>
136 class MutexLock<SafeBinaryMutex<Tag>> {
137 public:
138 MutexLock(const SafeBinaryMutex<Tag> &p_mutex) {}
139
140 void temp_relock() const {}
141 void temp_unlock() const {}
142 };
143
144 #endif // THREADS_ENABLED