* Start the thread. */
| 93 | * Start the thread. |
| 94 | */ |
| 95 | bool startThread(const bool withRealtimePriority = false) noexcept |
| 96 | { |
| 97 | // check if already running |
| 98 | DISTRHO_SAFE_ASSERT_RETURN(! isThreadRunning(), true); |
| 99 | |
| 100 | pthread_t handle; |
| 101 | |
| 102 | pthread_attr_t attr; |
| 103 | pthread_attr_init(&attr); |
| 104 | |
| 105 | struct sched_param sched_param = {}; |
| 106 | |
| 107 | if (withRealtimePriority) |
| 108 | { |
| 109 | #ifdef __MOD_DEVICES__ |
| 110 | int rtprio; |
| 111 | const char* const srtprio = std::getenv("MOD_PLUGIN_THREAD_PRIORITY"); |
| 112 | if (srtprio != nullptr && (rtprio = std::atoi(srtprio)) > 0) |
| 113 | sched_param.sched_priority = rtprio - 1; |
| 114 | else |
| 115 | #endif |
| 116 | sched_param.sched_priority = 80; |
| 117 | |
| 118 | #ifndef DISTRHO_OS_HAIKU |
| 119 | if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) == 0 && |
| 120 | pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) == 0 && |
| 121 | #ifndef DISTRHO_OS_WINDOWS |
| 122 | (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) == 0 || |
| 123 | pthread_attr_setschedpolicy(&attr, SCHED_RR) == 0) && |
| 124 | #endif |
| 125 | pthread_attr_setschedparam(&attr, &sched_param) == 0) |
| 126 | { |
| 127 | d_stdout("Thread setup with realtime priority successful"); |
| 128 | } |
| 129 | else |
| 130 | #endif |
| 131 | { |
| 132 | d_stdout("Thread setup with realtime priority failed, going with normal priority instead"); |
| 133 | pthread_attr_destroy(&attr); |
| 134 | pthread_attr_init(&attr); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | const MutexLocker ml(fLock); |
| 139 | |
| 140 | fShouldExit = false; |
| 141 | |
| 142 | bool ok = pthread_create(&handle, &attr, _entryPoint, this) == 0; |
| 143 | pthread_attr_destroy(&attr); |
| 144 | |
| 145 | if (withRealtimePriority && !ok) |
| 146 | { |
| 147 | d_stdout("Thread with realtime priority failed on creation, going with normal priority instead"); |
| 148 | pthread_attr_init(&attr); |
| 149 | ok = pthread_create(&handle, &attr, _entryPoint, this) == 0; |
| 150 | pthread_attr_destroy(&attr); |
| 151 | } |
| 152 |