17 #ifndef DISTRHO_THREAD_HPP_INCLUDED
18 #define DISTRHO_THREAD_HPP_INCLUDED
24 #ifdef DISTRHO_OS_LINUX
25 # include <sys/prctl.h>
28 START_NAMESPACE_DISTRHO
39 Thread(
const char*
const threadName =
nullptr) noexcept
43 fHandle({
nullptr, 0}),
54 DISTRHO_SAFE_ASSERT(! isThreadRunning());
62 virtual void run() = 0;
70 bool isThreadRunning()
const noexcept
73 return (fHandle.p !=
nullptr);
75 return (fHandle != 0);
82 bool shouldThreadExit()
const noexcept
90 bool startThread() noexcept
93 DISTRHO_SAFE_ASSERT_RETURN(! isThreadRunning(),
true);
101 if (pthread_create(&handle,
nullptr, _entryPoint,
this) == 0)
104 DISTRHO_SAFE_ASSERT_RETURN(handle.p !=
nullptr,
false);
106 DISTRHO_SAFE_ASSERT_RETURN(handle != 0,
false);
108 pthread_detach(handle);
127 bool stopThread(
const int timeOutMilliseconds) noexcept
131 if (isThreadRunning())
133 signalThreadShouldExit();
135 if (timeOutMilliseconds != 0)
138 int timeOutCheck = (timeOutMilliseconds == 1 || timeOutMilliseconds == -1) ? timeOutMilliseconds : timeOutMilliseconds/2;
140 for (; isThreadRunning();)
144 if (timeOutCheck < 0)
147 if (timeOutCheck > 0)
154 if (isThreadRunning())
157 d_stderr2(
"assertion failure: \"! isThreadRunning()\" in file %s, line %i", __FILE__, __LINE__);
165 pthread_cancel(threadId);
166 } DISTRHO_SAFE_EXCEPTION(
"pthread_cancel");
178 void signalThreadShouldExit() noexcept
189 const String& getThreadName()
const noexcept
197 static void setCurrentThreadName(
const char*
const name) noexcept
199 DISTRHO_SAFE_ASSERT_RETURN(name !=
nullptr && name[0] !=
'\0',);
201 #ifdef DISTRHO_OS_LINUX
202 prctl(PR_SET_NAME, name, 0, 0, 0);
204 #if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012
205 pthread_setname_np(pthread_self(), name);
214 volatile pthread_t fHandle;
215 volatile bool fShouldExit;
220 void _init() noexcept
233 void _copyFrom(
const pthread_t& handle) noexcept
236 fHandle.p = handle.p;
237 fHandle.x = handle.x;
246 void _copyTo(
volatile pthread_t& handle)
const noexcept
249 handle.p = fHandle.p;
250 handle.x = fHandle.x;
259 void _runEntryPoint() noexcept
264 setCurrentThreadName(fName);
277 static void* _entryPoint(
void* userData) noexcept
279 static_cast<Thread*
>(userData)->_runEntryPoint();
283 DISTRHO_DECLARE_NON_COPY_CLASS(
Thread)
288 END_NAMESPACE_DISTRHO
290 #endif // DISTRHO_THREAD_HPP_INCLUDED
Definition: Mutex.hpp:177
Definition: String.hpp:27
Definition: Thread.hpp:33