Debian Codegraph @

modules/lsp-runtime-lib/src/main/ipc/Thread.cpp

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: 25 февр. 2019 г.
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 #include <lsp-plug.in/common/types.h>
23 #include <lsp-plug.in/common/atomic.h>
24 #include <lsp-plug.in/ipc/Thread.h>
25
26 #include <time.h>
27 #include <errno.h>
28 #include <unistd.h>
29
30 #if defined(PLATFORM_WINDOWS)
31 #include <windows.h>
32 #include <processthreadsapi.h>
33 #elif defined(PLATFORM_LINUX)
34 #include <sched.h>
35 #include <sys/syscall.h>
36 #elif defined(PLATFORM_SOLARIS)
37 #include <pthread.h>
38 #elif defined(PLATFORM_FREEBSD)
39 #include <sched.h>
40 #include <sys/thr.h>
41 #elif defined(PLATFORM_NETBSD)
42 #include <sched.h>
43 #include <lwp.h>
44 #elif defined(PLATFORM_MACOSX)
45 #include <mach/mach.h>
46 #include <mach/mach_error.h>
47 #include <sched.h>
48 #elif defined(PLATFORM_DRAGONFLYBSD)
49 #include <sched.h>
50 #include <lwp.h>
51 #elif defined(PLATFORM_HAIKU)
52 #include <kernel/OS.h>
53 #endif /* PLATFORM_WINDOWS */
54
55 namespace lsp
56 {
57 namespace ipc
58 {
59 __thread Thread *Thread::pThis = NULL;
60
61 #if defined(PLATFORM_WINDOWS)
62 #define CLR_HANDLE(hThread) hThread = INVALID_HANDLE_VALUE;
63 #else
64 #define CLR_HANDLE(hThread) hThread = 0;
65 #endif
66
67 Thread::Thread()
68 {
69 atomic_store(&enState, TS_CREATED);
70 nResult = STATUS_OK;
71 bCancelled = false;
72 CLR_HANDLE(hThread);
73 sBinding.proc = NULL;
74 sBinding.arg = NULL;
75 sBinding.runnable = NULL;
76 }
77
78 Thread::Thread(thread_proc_t proc)
79 {
80 atomic_store(&enState, TS_CREATED);
81 nResult = STATUS_OK;
82 bCancelled = false;
83 CLR_HANDLE(hThread);
84 sBinding.proc = proc;
85 sBinding.arg = NULL;
86 sBinding.runnable = NULL;
87 }
88
89 Thread::Thread(thread_proc_t proc, void *arg)
90 {
91 atomic_store(&enState, TS_CREATED);
92 nResult = STATUS_OK;
93 bCancelled = false;
94 CLR_HANDLE(hThread);
95 sBinding.proc = proc;
96 sBinding.runnable = NULL;
97 sBinding.arg = arg;
98 }
99
100 Thread::Thread(IRunnable *runnable)
101 {
102 atomic_store(&enState, TS_CREATED);
103 nResult = STATUS_OK;
104 bCancelled = false;
105 CLR_HANDLE(hThread);
106 sBinding.proc = NULL;
107 sBinding.arg = NULL;
108 sBinding.runnable = runnable;
109 }
110
111 Thread::~Thread()
112 {
113 #if defined(PLATFORM_WINDOWS)
114 if (hThread != INVALID_HANDLE_VALUE)
115 CloseHandle(hThread);
116 #endif /* PLATFORM_WINDOWS */
117
118 CLR_HANDLE(hThread);
119 }
120
121 status_t Thread::run()
122 {
123 if (sBinding.proc != NULL)
124 return sBinding.proc(sBinding.arg);
125 else if (sBinding.runnable != NULL)
126 return sBinding.runnable->run();
127 return STATUS_OK;
128 }
129
130 status_t Thread::cancel()
131 {
132 switch (atomic_load(&enState))
133 {
134 case TS_PENDING:
135 case TS_RUNNING:
136 break;
137 default:
138 return STATUS_BAD_STATE;
139 }
140
141 bCancelled = true;
142 return STATUS_OK;
143 }
144
145 #if defined(PLATFORM_WINDOWS)
146 DWORD WINAPI Thread::thread_launcher(_In_ LPVOID lpParameter)
147 {
148 Thread *_this = reinterpret_cast<Thread *>(lpParameter);
149 pThis = _this;
150
151 // Wait until we are ready to launch
152 while (!atomic_cas(&_this->enState, TS_PENDING, TS_RUNNING)) {}
153
154 // Execute the thread
155 status_t res = _this->run();
156 pThis = NULL;
157
158 // Commit the 'FINISHED' status
159 int state;
160 do
161 {
162 state = atomic_load(&_this->enState);
163 } while (!atomic_cas(&_this->enState, state, TS_FINISHED));
164
165 _this->nResult = res;
166 return 0;
167 }
168
169 status_t Thread::start()
170 {
171 DWORD tid;
172 HANDLE thandle = CreateThread(NULL, 0, thread_launcher, this, 0, &tid);
173 if (thandle == INVALID_HANDLE_VALUE)
174 return STATUS_UNKNOWN_ERR;
175
176 hThread = thandle;
177 atomic_store(&enState, TS_PENDING);
178 return STATUS_OK;
179 }
180
181 status_t Thread::join()
182 {
183 switch (atomic_load(&enState))
184 {
185 case TS_CREATED:
186 return STATUS_BAD_STATE;
187 case TS_PENDING:
188 case TS_RUNNING:
189 {
190 DWORD res = WaitForSingleObject(hThread, INFINITE);
191 if ((res != WAIT_OBJECT_0) && (res != WAIT_ABANDONED))
192 return STATUS_UNKNOWN_ERR;
193 break;
194 }
195 case TS_FINISHED:
196 return STATUS_OK;
197 default:
198 return STATUS_BAD_STATE;
199 }
200 return STATUS_OK;
201 }
202
203 status_t Thread::sleep(wsize_t millis)
204 {
205 if (pThis == NULL)
206 {
207 while (millis > 0)
208 {
209 DWORD interval = (millis > 500) ? 500 : millis;
210 Sleep(interval);
211 millis -= interval;
212 }
213 }
214 else
215 {
216 if (pThis->bCancelled)
217 return STATUS_CANCELLED;
218
219 while (millis > 0)
220 {
221 if (pThis->bCancelled)
222 return STATUS_CANCELLED;
223
224 DWORD interval = (millis > 100) ? 100 : millis;
225 Sleep(interval);
226 millis -= interval;
227 }
228 }
229
230 return STATUS_OK;
231 }
232
233 void Thread::yield()
234 {
235 SwitchToThread();
236 }
237
238 #else
239 void *Thread::thread_launcher(void *arg)
240 {
241 Thread *_this = reinterpret_cast<Thread *>(arg);
242 pThis = _this;
243
244 // Cleanup cancellation state
245 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
246 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
247
248 // Wait until we are ready to launch
249 while (!atomic_cas(&_this->enState, TS_PENDING, TS_RUNNING)) {}
250
251 // Execute the thread
252 status_t res = _this->run();
253
254 // Commit the 'FINISHED' status
255 int state;
256 do
257 {
258 state = atomic_load(&_this->enState);
259 } while (!atomic_cas(&_this->enState, state, TS_FINISHED));
260
261 _this->nResult = res;
262 return NULL;
263 }
264
265 status_t Thread::start()
266 {
267 pthread_t tid;
268 if (pthread_create(&tid, NULL, &thread_launcher, this) != 0)
269 return STATUS_UNKNOWN_ERR;
270
271 hThread = tid;
272 atomic_store(&enState, TS_PENDING);
273 return STATUS_OK;
274 }
275
276 status_t Thread::join()
277 {
278 switch (atomic_load(&enState))
279 {
280 case TS_CREATED:
281 return STATUS_BAD_STATE;
282 case TS_PENDING:
283 case TS_RUNNING:
284 if (pthread_join(hThread, NULL) != 0)
285 return STATUS_UNKNOWN_ERR;
286 break;
287 case TS_FINISHED:
288 return STATUS_OK;
289 default:
290 return STATUS_BAD_STATE;
291 }
292 return STATUS_OK;
293 }
294
295 status_t Thread::sleep(wsize_t millis)
296 {
297 struct timespec req, rem;
298
299 if (pThis == NULL)
300 {
301 req.tv_sec = millis / 1000;
302 req.tv_nsec = (millis % 1000) * 1000000;
303
304 while (::nanosleep(&req, &rem) != 0)
305 {
306 int code = errno;
307 if (code != EINTR)
308 return STATUS_UNKNOWN_ERR;
309 req = rem;
310 }
311 }
312 else
313 {
314 if (pThis->bCancelled)
315 return STATUS_CANCELLED;
316
317 while (millis > 0)
318 {
319 if (pThis->bCancelled)
320 return STATUS_CANCELLED;
321
322 wsize_t interval = (millis > 100) ? 100 : millis;
323 req.tv_sec = 0;
324 req.tv_nsec = interval * 1000000;
325
326 while (::nanosleep(&req, &rem) != 0)
327 {
328 int code = errno;
329 if (code != EINTR)
330 return STATUS_UNKNOWN_ERR;
331
332 if (pThis->bCancelled)
333 return STATUS_CANCELLED;
334 req = rem;
335 }
336
337 millis -= interval;
338 }
339 }
340
341 return STATUS_OK;
342 }
343
344 void Thread::yield()
345 {
346 sched_yield();
347 }
348
349 #endif /* PLATFORM_WINDOWS */
350
351
352 thread_id_t Thread::current_thread_id()
353 {
354 thread_id_t result = INVALID_THREAD_ID;
355
356 #if defined(PLATFORM_WINDOWS)
357 return GetCurrentThreadId();
358 #elif defined(PLATFORM_LINUX)
359 result = syscall( __NR_gettid );
360 #elif defined(PLATFORM_SOLARIS)
361 result = pthread_self();
362 #elif defined(PLATFORM_MACOSX)
363 const mach_port_t port = mach_thread_self();
364 mach_port_deallocate(mach_task_self(), port);
365 result = port;
366 #elif defined(PLATFORM_NETBSD)
367 result = _lwp_self();
368 #elif defined(PLATFORM_FREEBSD)
369 long lwpid;
370 thr_self( &lwpid );
371 result = lwpid;
372 #elif defined(PLATFORM_DRAGONFLYBSD)
373 result = lwp_gettid();
374 #elif defined (PLATFORM_HAIKU)
375 result = find_thread(NULL);
376 #else
377 #error "need to implement Thread::current_thread_id"
378 #endif
379 return result;
380 }
381
382 } /* namespace ipc */
383 } /* namespace lsp */