The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

477 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. Thread::Thread (const String& name, const size_t stackSize)
  18. : threadName (name), threadStackSize (stackSize)
  19. {
  20. }
  21. Thread::~Thread()
  22. {
  23. /* If your thread class's destructor has been called without first stopping the thread, that
  24. means that this partially destructed object is still performing some work - and that's
  25. probably a Bad Thing!
  26. To avoid this type of nastiness, always make sure you call stopThread() before or during
  27. your subclass's destructor.
  28. */
  29. jassert (! isThreadRunning());
  30. stopThread (-1);
  31. }
  32. //==============================================================================
  33. // Use a ref-counted object to hold this shared data, so that it can outlive its static
  34. // shared pointer when threads are still running during static shutdown.
  35. struct CurrentThreadHolder : public ReferenceCountedObject
  36. {
  37. CurrentThreadHolder() noexcept {}
  38. typedef ReferenceCountedObjectPtr<CurrentThreadHolder> Ptr;
  39. ThreadLocalValue<Thread*> value;
  40. JUCE_DECLARE_NON_COPYABLE (CurrentThreadHolder)
  41. };
  42. static char currentThreadHolderLock [sizeof (SpinLock)]; // (statically initialised to zeros).
  43. static SpinLock* castToSpinLockWithoutAliasingWarning (void* s)
  44. {
  45. return static_cast<SpinLock*> (s);
  46. }
  47. static CurrentThreadHolder::Ptr getCurrentThreadHolder()
  48. {
  49. static CurrentThreadHolder::Ptr currentThreadHolder;
  50. SpinLock::ScopedLockType lock (*castToSpinLockWithoutAliasingWarning (currentThreadHolderLock));
  51. if (currentThreadHolder == nullptr)
  52. currentThreadHolder = new CurrentThreadHolder();
  53. return currentThreadHolder;
  54. }
  55. void Thread::threadEntryPoint()
  56. {
  57. const CurrentThreadHolder::Ptr currentThreadHolder (getCurrentThreadHolder());
  58. currentThreadHolder->value = this;
  59. if (threadName.isNotEmpty())
  60. setCurrentThreadName (threadName);
  61. if (startSuspensionEvent.wait (10000))
  62. {
  63. jassert (getCurrentThreadId() == threadId);
  64. if (affinityMask != 0)
  65. setCurrentThreadAffinityMask (affinityMask);
  66. try
  67. {
  68. run();
  69. }
  70. catch (...)
  71. {
  72. jassertfalse; // Your run() method mustn't throw any exceptions!
  73. }
  74. }
  75. currentThreadHolder->value.releaseCurrentThreadStorage();
  76. closeThreadHandle();
  77. }
  78. // used to wrap the incoming call from the platform-specific code
  79. void JUCE_API juce_threadEntryPoint (void* userData)
  80. {
  81. static_cast<Thread*> (userData)->threadEntryPoint();
  82. }
  83. //==============================================================================
  84. void Thread::startThread()
  85. {
  86. const ScopedLock sl (startStopLock);
  87. shouldExit = false;
  88. if (threadHandle == nullptr)
  89. {
  90. launchThread();
  91. setThreadPriority (threadHandle, threadPriority);
  92. startSuspensionEvent.signal();
  93. }
  94. }
  95. void Thread::startThread (int priority)
  96. {
  97. const ScopedLock sl (startStopLock);
  98. if (threadHandle == nullptr)
  99. {
  100. auto isRealtime = (priority == realtimeAudioPriority);
  101. #if JUCE_ANDROID
  102. isAndroidRealtimeThread = isRealtime;
  103. #endif
  104. if (isRealtime)
  105. priority = 9;
  106. threadPriority = priority;
  107. startThread();
  108. }
  109. else
  110. {
  111. setPriority (priority);
  112. }
  113. }
  114. bool Thread::isThreadRunning() const
  115. {
  116. return threadHandle != nullptr;
  117. }
  118. Thread* JUCE_CALLTYPE Thread::getCurrentThread()
  119. {
  120. return getCurrentThreadHolder()->value.get();
  121. }
  122. //==============================================================================
  123. void Thread::signalThreadShouldExit()
  124. {
  125. shouldExit = true;
  126. }
  127. bool Thread::currentThreadShouldExit()
  128. {
  129. if (Thread* currentThread = getCurrentThread())
  130. return currentThread->threadShouldExit();
  131. return false;
  132. }
  133. bool Thread::waitForThreadToExit (const int timeOutMilliseconds) const
  134. {
  135. // Doh! So how exactly do you expect this thread to wait for itself to stop??
  136. jassert (getThreadId() != getCurrentThreadId() || getCurrentThreadId() == 0);
  137. const uint32 timeoutEnd = Time::getMillisecondCounter() + (uint32) timeOutMilliseconds;
  138. while (isThreadRunning())
  139. {
  140. if (timeOutMilliseconds >= 0 && Time::getMillisecondCounter() > timeoutEnd)
  141. return false;
  142. sleep (2);
  143. }
  144. return true;
  145. }
  146. bool Thread::stopThread (const int timeOutMilliseconds)
  147. {
  148. // agh! You can't stop the thread that's calling this method! How on earth
  149. // would that work??
  150. jassert (getCurrentThreadId() != getThreadId());
  151. const ScopedLock sl (startStopLock);
  152. if (isThreadRunning())
  153. {
  154. signalThreadShouldExit();
  155. notify();
  156. if (timeOutMilliseconds != 0)
  157. waitForThreadToExit (timeOutMilliseconds);
  158. if (isThreadRunning())
  159. {
  160. // very bad karma if this point is reached, as there are bound to be
  161. // locks and events left in silly states when a thread is killed by force..
  162. jassertfalse;
  163. Logger::writeToLog ("!! killing thread by force !!");
  164. killThread();
  165. threadHandle = nullptr;
  166. threadId = 0;
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. //==============================================================================
  173. bool Thread::setPriority (int newPriority)
  174. {
  175. bool isRealtime = (newPriority == realtimeAudioPriority);
  176. if (isRealtime)
  177. newPriority = 9;
  178. // NB: deadlock possible if you try to set the thread prio from the thread itself,
  179. // so using setCurrentThreadPriority instead in that case.
  180. if (getCurrentThreadId() == getThreadId())
  181. return setCurrentThreadPriority (newPriority);
  182. const ScopedLock sl (startStopLock);
  183. #if JUCE_ANDROID
  184. // you cannot switch from or to an Android realtime thread once the
  185. // thread is already running!
  186. jassert (isThreadRunning() && (isRealtime == isAndroidRealtimeThread));
  187. isAndroidRealtimeThread = isRealtime;
  188. #endif
  189. if ((! isThreadRunning()) || setThreadPriority (threadHandle, newPriority))
  190. {
  191. threadPriority = newPriority;
  192. return true;
  193. }
  194. return false;
  195. }
  196. bool Thread::setCurrentThreadPriority (const int newPriority)
  197. {
  198. return setThreadPriority (0, newPriority);
  199. }
  200. void Thread::setAffinityMask (const uint32 newAffinityMask)
  201. {
  202. affinityMask = newAffinityMask;
  203. }
  204. //==============================================================================
  205. bool Thread::wait (const int timeOutMilliseconds) const
  206. {
  207. return defaultEvent.wait (timeOutMilliseconds);
  208. }
  209. void Thread::notify() const
  210. {
  211. defaultEvent.signal();
  212. }
  213. //==============================================================================
  214. void SpinLock::enter() const noexcept
  215. {
  216. if (! tryEnter())
  217. {
  218. for (int i = 20; --i >= 0;)
  219. if (tryEnter())
  220. return;
  221. while (! tryEnter())
  222. Thread::yield();
  223. }
  224. }
  225. //==============================================================================
  226. bool JUCE_CALLTYPE Process::isRunningUnderDebugger() noexcept
  227. {
  228. return juce_isRunningUnderDebugger();
  229. }
  230. #if JUCE_UNIT_TESTS
  231. //==============================================================================
  232. class AtomicTests : public UnitTest
  233. {
  234. public:
  235. AtomicTests() : UnitTest ("Atomics") {}
  236. void runTest() override
  237. {
  238. beginTest ("Misc");
  239. char a1[7];
  240. expect (numElementsInArray(a1) == 7);
  241. int a2[3];
  242. expect (numElementsInArray(a2) == 3);
  243. expect (ByteOrder::swap ((uint16) 0x1122) == 0x2211);
  244. expect (ByteOrder::swap ((uint32) 0x11223344) == 0x44332211);
  245. expect (ByteOrder::swap ((uint64) 0x1122334455667788ULL) == 0x8877665544332211LL);
  246. beginTest ("Atomic int");
  247. AtomicTester <int>::testInteger (*this);
  248. beginTest ("Atomic unsigned int");
  249. AtomicTester <unsigned int>::testInteger (*this);
  250. beginTest ("Atomic int32");
  251. AtomicTester <int32>::testInteger (*this);
  252. beginTest ("Atomic uint32");
  253. AtomicTester <uint32>::testInteger (*this);
  254. beginTest ("Atomic long");
  255. AtomicTester <long>::testInteger (*this);
  256. beginTest ("Atomic int*");
  257. AtomicTester <int*>::testInteger (*this);
  258. beginTest ("Atomic float");
  259. AtomicTester <float>::testFloat (*this);
  260. #if ! JUCE_64BIT_ATOMICS_UNAVAILABLE // 64-bit intrinsics aren't available on some old platforms
  261. beginTest ("Atomic int64");
  262. AtomicTester <int64>::testInteger (*this);
  263. beginTest ("Atomic uint64");
  264. AtomicTester <uint64>::testInteger (*this);
  265. beginTest ("Atomic double");
  266. AtomicTester <double>::testFloat (*this);
  267. #endif
  268. beginTest ("Atomic pointer increment/decrement");
  269. Atomic<int*> a (a2); int* b (a2);
  270. expect (++a == ++b);
  271. {
  272. beginTest ("Atomic void*");
  273. Atomic<void*> atomic;
  274. void* c;
  275. atomic.set ((void*) 10);
  276. c = (void*) 10;
  277. expect (atomic.value == c);
  278. expect (atomic.get() == c);
  279. }
  280. }
  281. template <typename Type>
  282. class AtomicTester
  283. {
  284. public:
  285. AtomicTester() {}
  286. static void testInteger (UnitTest& test)
  287. {
  288. Atomic<Type> a, b;
  289. Type c;
  290. a.set ((Type) 10);
  291. c = (Type) 10;
  292. test.expect (a.value == c);
  293. test.expect (a.get() == c);
  294. a += 15;
  295. c += 15;
  296. test.expect (a.get() == c);
  297. a.memoryBarrier();
  298. a -= 5;
  299. c -= 5;
  300. test.expect (a.get() == c);
  301. test.expect (++a == ++c);
  302. ++a;
  303. ++c;
  304. test.expect (--a == --c);
  305. test.expect (a.get() == c);
  306. a.memoryBarrier();
  307. testFloat (test);
  308. }
  309. static void testFloat (UnitTest& test)
  310. {
  311. Atomic<Type> a, b;
  312. a = (Type) 101;
  313. a.memoryBarrier();
  314. /* These are some simple test cases to check the atomics - let me know
  315. if any of these assertions fail on your system!
  316. */
  317. test.expect (a.get() == (Type) 101);
  318. test.expect (! a.compareAndSetBool ((Type) 300, (Type) 200));
  319. test.expect (a.get() == (Type) 101);
  320. test.expect (a.compareAndSetBool ((Type) 200, a.get()));
  321. test.expect (a.get() == (Type) 200);
  322. test.expect (a.exchange ((Type) 300) == (Type) 200);
  323. test.expect (a.get() == (Type) 300);
  324. b = a;
  325. test.expect (b.get() == a.get());
  326. }
  327. };
  328. };
  329. static AtomicTests atomicUnitTests;
  330. //==============================================================================
  331. class ThreadLocalValueUnitTest : public UnitTest, private Thread
  332. {
  333. public:
  334. ThreadLocalValueUnitTest()
  335. : UnitTest ("ThreadLocalValue"),
  336. Thread ("ThreadLocalValue Thread")
  337. {}
  338. void runTest() override
  339. {
  340. beginTest ("values are thread local");
  341. {
  342. ThreadLocalValue<int> threadLocal;
  343. sharedThreadLocal = &threadLocal;
  344. sharedThreadLocal.get()->get() = 1;
  345. startThread();
  346. signalThreadShouldExit();
  347. waitForThreadToExit (-1);
  348. mainThreadResult = sharedThreadLocal.get()->get();
  349. expectEquals (mainThreadResult.get(), 1);
  350. expectEquals (auxThreadResult.get(), 2);
  351. }
  352. beginTest ("values are per-instance");
  353. {
  354. ThreadLocalValue<int> a, b;
  355. a.get() = 1;
  356. b.get() = 2;
  357. expectEquals (a.get(), 1);
  358. expectEquals (b.get(), 2);
  359. }
  360. }
  361. private:
  362. Atomic<int> mainThreadResult, auxThreadResult;
  363. Atomic<ThreadLocalValue<int>*> sharedThreadLocal;
  364. void run() override
  365. {
  366. sharedThreadLocal.get()->get() = 2;
  367. auxThreadResult = sharedThreadLocal.get()->get();
  368. }
  369. };
  370. ThreadLocalValueUnitTest threadLocalValueUnitTest;
  371. #endif