17 #ifndef DISTRHO_THREAD_HPP_INCLUDED 
   18 #define DISTRHO_THREAD_HPP_INCLUDED 
   20 #include "d_mutex.hpp" 
   21 #include "d_sleep.hpp" 
   22 #include "d_string.hpp" 
   24 #if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012 
   26 #elif defined(DISTRHO_OS_LINUX) 
   27 # include <sys/prctl.h> 
   30 START_NAMESPACE_DISTRHO
 
   41     Thread(
const char* 
const threadName = 
nullptr) noexcept
 
   45           fHandle({
nullptr, 0}),
 
   56         DISTRHO_SAFE_ASSERT(! isThreadRunning());
 
   64     virtual void run() = 0;
 
   72     bool isThreadRunning() 
const noexcept
 
   75         return (fHandle.p != 
nullptr);
 
   77         return (fHandle != 0);
 
   84     bool shouldThreadExit() 
const noexcept
 
   92     bool startThread() noexcept
 
   95         DISTRHO_SAFE_ASSERT_RETURN(! isThreadRunning(), 
true);
 
  103         if (pthread_create(&handle, 
nullptr, _entryPoint, 
this) == 0)
 
  106             DISTRHO_SAFE_ASSERT_RETURN(handle.p != 
nullptr, 
false);
 
  108             DISTRHO_SAFE_ASSERT_RETURN(handle != 0, 
false);
 
  110             pthread_detach(handle);
 
  129     bool stopThread(
const int timeOutMilliseconds) noexcept
 
  133         if (isThreadRunning())
 
  135             signalThreadShouldExit();
 
  137             if (timeOutMilliseconds != 0)
 
  140                 int timeOutCheck = (timeOutMilliseconds == 1 || timeOutMilliseconds == -1) ? timeOutMilliseconds : timeOutMilliseconds/2;
 
  142                 for (; isThreadRunning();)
 
  146                     if (timeOutCheck < 0)
 
  149                     if (timeOutCheck > 0)
 
  156             if (isThreadRunning())
 
  159                 d_stderr2(
"Carla assertion failure: \"! isThreadRunning()\" in file %s, line %i", __FILE__, __LINE__);
 
  167                     pthread_cancel(threadId);
 
  168                 } DISTRHO_SAFE_EXCEPTION(
"pthread_cancel");
 
  180     void signalThreadShouldExit() noexcept
 
  191     const d_string& getThreadName() 
const noexcept
 
  199     static void setCurrentThreadName(
const char* 
const name) noexcept
 
  201         DISTRHO_SAFE_ASSERT_RETURN(name != 
nullptr && name[0] != 
'\0',);
 
  203 #if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012 
  204         pthread_setname_np(pthread_self(), name);
 
  205 #elif defined(DISTRHO_OS_LINUX) 
  206         prctl(PR_SET_NAME, name, 0, 0, 0);
 
  215     volatile pthread_t fHandle;     
 
  216     volatile bool      fShouldExit; 
 
  221     void _init() noexcept
 
  234     void _copyFrom(
const pthread_t& handle) noexcept
 
  237         fHandle.p = handle.p;
 
  238         fHandle.x = handle.x;
 
  247     void _copyTo(
volatile pthread_t& handle) 
const noexcept
 
  250         handle.p = fHandle.p;
 
  251         handle.x = fHandle.x;
 
  260     void _runEntryPoint() noexcept
 
  265         setCurrentThreadName(fName);
 
  278     static void* _entryPoint(
void* userData) noexcept
 
  280         static_cast<Thread*
>(userData)->_runEntryPoint();
 
  284     DISTRHO_DECLARE_NON_COPY_CLASS(
Thread)
 
  289 END_NAMESPACE_DISTRHO
 
  291 #endif // DISTRHO_THREAD_HPP_INCLUDED 
Definition: d_mutex.hpp:34
Definition: d_mutex.hpp:171
Definition: d_thread.hpp:35
Definition: d_string.hpp:27