Debian Codegraph @

modules/lsp-runtime-lib/include/lsp-plug.in/ipc/Condition.h

raw
1 /*
2 * Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2026 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 4 янв. 2026 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #ifndef LSP_PLUG_IN_IPC_CONDITION_H_
23 #define LSP_PLUG_IN_IPC_CONDITION_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26 #include <lsp-plug.in/runtime/system.h>
27 #include <lsp-plug.in/common/types.h>
28 #include <lsp-plug.in/common/atomic.h>
29
30 #if defined(PLATFORM_WINDOWS)
31 // Nothing
32 #else
33 #include <pthread.h>
34 #endif
35
36 namespace lsp
37 {
38 namespace ipc
39 {
40 #if defined(PLATFORM_WINDOWS)
41
42 namespace detail
43 {
44 struct condition_t;
45 } /* namespace detail */
46
47 /**
48 * Condition variable implementation for Windows platform
49 */
50 class Condition
51 {
52 private:
53 mutable detail::condition_t *pState; // Condition variable state
54
55 public:
56 explicit Condition();
57 Condition(const Condition &) = delete;
58 Condition(Condition &&) = delete;
59 ~Condition();
60 Condition & operator = (const Condition &) = delete;
61 Condition & operator = (Condition &&) = delete;
62
63 public:
64 /**
65 * Wait until condition mutex is unlocked and lock it
66 */
67 bool lock() const;
68
69 /**
70 * Try to lock condition mutex and return status of operation
71 *
72 * @return true if condition has been locked
73 */
74 bool try_lock() const;
75
76 /**
77 * Unlock condition mutex
78 * @return true on success
79 */
80 bool unlock() const;
81
82 /**
83 * Notify single thread about condition change
84 * @return true on success
85 */
86 bool notify() const;
87
88 /**
89 * Notify all waiting threads about condition change
90 * @return true on success
91 */
92 bool notify_all() const;
93
94 /**
95 * Wait for condition variable within specified time interval
96 * @param millis time interval to perform the wait
97 * @return status of operation
98 */
99 status_t wait(system::time_millis_t millis);
100
101 /**
102 * Perform infinite wait for condition variable
103 * @return status of operation
104 */
105 status_t wait();
106 };
107 #else
108 /**
109 * Condition variable implementation using pthread
110 */
111 class Condition
112 {
113 private:
114 mutable pthread_mutex_t sMutex;
115 mutable pthread_cond_t sCondition;
116
117 public:
118 explicit Condition();
119 Condition(const Condition &) = delete;
120 Condition(Condition &&) = delete;
121 ~Condition();
122 Condition & operator = (const Condition &) = delete;
123 Condition & operator = (Condition &&) = delete;
124
125 public:
126 /**
127 * Wait until condition mutex is unlocked and lock it
128 */
129 bool lock() const;
130
131 /**
132 * Try to lock condition mutex and return status of operation
133 *
134 * @return true if condition has been locked
135 */
136 inline bool try_lock() const
137 {
138 return pthread_mutex_trylock(&sMutex) == 0;
139 }
140
141 /**
142 * Unlock condition mutex
143 * @return true on success
144 */
145 inline bool unlock() const
146 {
147 return pthread_mutex_unlock(&sMutex) == 0;
148 }
149
150 /**
151 * Notify single thread about condition change
152 * @return true on success
153 */
154 inline bool notify() const
155 {
156 return pthread_cond_signal(&sCondition) == 0;
157 }
158
159 /**
160 * Notify all waiting threads about condition change
161 * @return true on success
162 */
163 inline bool notify_all() const
164 {
165 return pthread_cond_broadcast(&sCondition) == 0;
166 }
167
168 /**
169 * Wait for condition variable within specified time interval
170 * @param millis time interval to perform the wait
171 * @return status of operation
172 */
173 status_t wait(system::time_millis_t millis);
174
175 /**
176 * Perform infinite wait for condition variable
177 * @return status of operation
178 */
179 status_t wait();
180 };
181 #endif
182
183 } /* namespace ipc */
184 } /* namespace lsp */
185
186
187
188 #endif /* LSP_PLUG_IN_IPC_CONDITION_H_ */