Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 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 #ifndef LSP_PLUG_IN_IPC_THREAD_H_
23 #define LSP_PLUG_IN_IPC_THREAD_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26 #include <lsp-plug.in/common/atomic.h>
27 #include <lsp-plug.in/common/types.h>
28 #include <lsp-plug.in/common/status.h>
29
30 #if defined(PLATFORM_WINDOWS)
31 #include <processthreadsapi.h>
32 #else
33 #include <pthread.h>
34 #endif /* PLATFORM_WINDOWS */
35
36 #include <lsp-plug.in/ipc/IRunnable.h>
37 #include <lsp-plug.in/runtime/system.h>
38
39 namespace lsp
40 {
41 namespace ipc
42 {
43 enum thread_state_t
44 {
45 TS_CREATED,
46 TS_PENDING,
47 TS_RUNNING,
48 TS_FINISHED
49 };
50
51 /**
52 * Thread procedure that can be launched
53 * @param arg additional argument passed to a thread procedure
54 * @return status of thread execution
55 */
56 typedef status_t (* thread_proc_t)(void *arg);
57
58 typedef umword_t thread_id_t;
59
60 constexpr thread_id_t INVALID_THREAD_ID = 0;
61
62 /**
63 * Thread class
64 */
65 class Thread: public IRunnable
66 {
67 protected:
68 typedef struct binding_t
69 {
70 thread_proc_t proc;
71 union
72 {
73 IRunnable *runnable;
74 void *arg;
75 };
76 } binding_t;
77
78 private:
79 static __thread Thread *pThis;
80 mutable int enState;
81 volatile bool bCancelled;
82 volatile status_t nResult;
83
84 #if defined(PLATFORM_WINDOWS)
85 HANDLE hThread; // Windows threads
86 #else
87 pthread_t hThread; // POSIX threads
88 #endif /* PLATFORM_WINDOWS */
89
90 protected:
91 binding_t sBinding;
92
93 private:
94 #if defined(PLATFORM_WINDOWS)
95 static DWORD WINAPI thread_launcher(_In_ LPVOID lpParameter);
96 #else
97 static void *thread_launcher(void *arg);
98 #endif /* PLATFORM_WINDOWS */
99
100 Thread & operator = (const Thread &src); // Deny copying
101
102 public:
103 explicit Thread();
104 explicit Thread(thread_proc_t proc);
105 explicit Thread(thread_proc_t proc, void *arg);
106 explicit Thread(IRunnable *runnable);
107
108 virtual ~Thread();
109
110 public:
111 /**
112 * The thread's main execution method
113 * @return status of thread execution
114 */
115 virtual status_t run();
116
117 /**
118 * Launch the created thread
119 * @return status of operation
120 */
121 status_t start();
122
123 /**
124 * Send cancel request to the thread
125 * @return status of operation
126 */
127 status_t cancel();
128
129 /**
130 * Wait thread for completion
131 * @return status of operation
132 */
133 status_t join();
134
135 /**
136 * Force current thread to leep for amount of milliseconds
137 * @param millis interval in milliseconds to perform a sleep
138 */
139 static status_t sleep(wsize_t millis);
140
141 /**
142 * Causes the calling thread to yield execution to another thread that
143 * is ready to run on the current processor.
144 */
145 static void yield();
146
147 /**
148 * Return the current thread
149 * @return current thread or NULL if current thread is not an instance of ipc::Thread class
150 */
151 static inline Thread *current() { return pThis; }
152
153 /** Check that cancellation signal has been delivered to the current thread
154 * @return true if cancellation signal is pending
155 */
156 static inline bool is_cancelled() { return (pThis != NULL) ? pThis->bCancelled : false; };
157
158 /** Check that cancellation signal has been delivered to the thread
159 * @return true if thread has been cancelled
160 */
161 inline bool cancelled() const { return bCancelled; };
162
163 /**
164 * Check whether thread has finished
165 * @return true if thread has finished
166 */
167 inline bool finished() const { return atomic_load(&enState) == TS_FINISHED; }
168
169 /**
170 * Get thread state
171 * @return thread state
172 */
173 inline thread_state_t state() const { return thread_state_t(atomic_load(&enState)); };
174
175 /**
176 * Return the execution result of the thread
177 * @return execution result of the thread
178 */
179 status_t get_result() const { return (atomic_load(&enState) == TS_FINISHED) ? nResult : STATUS_BAD_STATE; };
180
181 /**
182 * Return number of execution cores supported by the system
183 * @return number of logical CPUs in the system available for processing
184 */
185 static inline size_t system_cores() { return system::system_cores(); }
186
187 /**
188 * Get current thread identifier.
189 * @return current thread identifier
190 */
191 static thread_id_t current_thread_id();
192 };
193
194 } /* namespace ipc */
195 } /* namespace lsp */
196
197 #endif /* LSP_PLUG_IN_IPC_THREAD_H_ */