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.

540 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. namespace juce
  18. {
  19. //==============================================================================
  20. Thread::Thread (const String& name, size_t stackSize) : threadName (name),
  21. threadStackSize (stackSize)
  22. {
  23. }
  24. Thread::~Thread()
  25. {
  26. if (deleteOnThreadEnd)
  27. return;
  28. /* If your thread class's destructor has been called without first stopping the thread, that
  29. means that this partially destructed object is still performing some work - and that's
  30. probably a Bad Thing!
  31. To avoid this type of nastiness, always make sure you call stopThread() before or during
  32. your subclass's destructor.
  33. */
  34. jassert (! isThreadRunning());
  35. stopThread (-1);
  36. }
  37. //==============================================================================
  38. // Use a ref-counted object to hold this shared data, so that it can outlive its static
  39. // shared pointer when threads are still running during static shutdown.
  40. struct CurrentThreadHolder final : public ReferenceCountedObject
  41. {
  42. CurrentThreadHolder() noexcept {}
  43. using Ptr = ReferenceCountedObjectPtr<CurrentThreadHolder>;
  44. ThreadLocalValue<Thread*> value;
  45. JUCE_DECLARE_NON_COPYABLE (CurrentThreadHolder)
  46. };
  47. static char currentThreadHolderLock [sizeof (SpinLock)]; // (statically initialised to zeros).
  48. static SpinLock* castToSpinLockWithoutAliasingWarning (void* s)
  49. {
  50. return static_cast<SpinLock*> (s);
  51. }
  52. static CurrentThreadHolder::Ptr getCurrentThreadHolder()
  53. {
  54. static CurrentThreadHolder::Ptr currentThreadHolder;
  55. SpinLock::ScopedLockType lock (*castToSpinLockWithoutAliasingWarning (currentThreadHolderLock));
  56. if (currentThreadHolder == nullptr)
  57. currentThreadHolder = new CurrentThreadHolder();
  58. return currentThreadHolder;
  59. }
  60. void Thread::threadEntryPoint()
  61. {
  62. const CurrentThreadHolder::Ptr currentThreadHolder (getCurrentThreadHolder());
  63. currentThreadHolder->value = this;
  64. if (threadName.isNotEmpty())
  65. setCurrentThreadName (threadName);
  66. // This 'startSuspensionEvent' protects 'threadId' which is initialised after the platform's native 'CreateThread' method.
  67. // This ensures it has been initialised correctly before it reaches this point.
  68. if (startSuspensionEvent.wait (10000))
  69. {
  70. jassert (getCurrentThreadId() == threadId);
  71. if (affinityMask != 0)
  72. setCurrentThreadAffinityMask (affinityMask);
  73. try
  74. {
  75. run();
  76. }
  77. catch (...)
  78. {
  79. jassertfalse; // Your run() method mustn't throw any exceptions!
  80. }
  81. }
  82. currentThreadHolder->value.releaseCurrentThreadStorage();
  83. // Once closeThreadHandle is called this class may be deleted by a different
  84. // thread, so we need to store deleteOnThreadEnd in a local variable.
  85. auto shouldDeleteThis = deleteOnThreadEnd;
  86. closeThreadHandle();
  87. if (shouldDeleteThis)
  88. delete this;
  89. }
  90. // used to wrap the incoming call from the platform-specific code
  91. void JUCE_API juce_threadEntryPoint (void* userData)
  92. {
  93. static_cast<Thread*> (userData)->threadEntryPoint();
  94. }
  95. //==============================================================================
  96. bool Thread::startThreadInternal (Priority threadPriority)
  97. {
  98. shouldExit = false;
  99. // 'priority' is essentially useless on Linux as only realtime
  100. // has any options but we need to set this here to satisfy
  101. // later queries, otherwise we get inconsistent results across
  102. // platforms.
  103. #if JUCE_ANDROID || JUCE_LINUX || JUCE_BSD
  104. priority = threadPriority;
  105. #endif
  106. if (createNativeThread (threadPriority))
  107. {
  108. startSuspensionEvent.signal();
  109. return true;
  110. }
  111. return false;
  112. }
  113. bool Thread::startThread()
  114. {
  115. return startThread (Priority::normal);
  116. }
  117. bool Thread::startThread (Priority threadPriority)
  118. {
  119. const ScopedLock sl (startStopLock);
  120. if (threadHandle == nullptr)
  121. {
  122. realtimeOptions.reset();
  123. return startThreadInternal (threadPriority);
  124. }
  125. return false;
  126. }
  127. bool Thread::startRealtimeThread (const RealtimeOptions& options)
  128. {
  129. const ScopedLock sl (startStopLock);
  130. if (threadHandle == nullptr)
  131. {
  132. realtimeOptions = std::make_optional (options);
  133. if (startThreadInternal (Priority::normal))
  134. return true;
  135. realtimeOptions.reset();
  136. }
  137. return false;
  138. }
  139. bool Thread::isThreadRunning() const
  140. {
  141. return threadHandle != nullptr;
  142. }
  143. Thread* JUCE_CALLTYPE Thread::getCurrentThread()
  144. {
  145. return getCurrentThreadHolder()->value.get();
  146. }
  147. Thread::ThreadID Thread::getThreadId() const noexcept
  148. {
  149. return threadId;
  150. }
  151. //==============================================================================
  152. void Thread::signalThreadShouldExit()
  153. {
  154. shouldExit = true;
  155. listeners.call ([] (Listener& l) { l.exitSignalSent(); });
  156. }
  157. bool Thread::threadShouldExit() const
  158. {
  159. return shouldExit;
  160. }
  161. bool Thread::currentThreadShouldExit()
  162. {
  163. if (auto* currentThread = getCurrentThread())
  164. return currentThread->threadShouldExit();
  165. return false;
  166. }
  167. bool Thread::waitForThreadToExit (const int timeOutMilliseconds) const
  168. {
  169. // Doh! So how exactly do you expect this thread to wait for itself to stop??
  170. jassert (getThreadId() != getCurrentThreadId() || getCurrentThreadId() == ThreadID());
  171. auto timeoutEnd = Time::getMillisecondCounter() + (uint32) timeOutMilliseconds;
  172. while (isThreadRunning())
  173. {
  174. if (timeOutMilliseconds >= 0 && Time::getMillisecondCounter() > timeoutEnd)
  175. return false;
  176. sleep (2);
  177. }
  178. return true;
  179. }
  180. bool Thread::stopThread (const int timeOutMilliseconds)
  181. {
  182. // agh! You can't stop the thread that's calling this method! How on earth
  183. // would that work??
  184. jassert (getCurrentThreadId() != getThreadId());
  185. const ScopedLock sl (startStopLock);
  186. if (isThreadRunning())
  187. {
  188. signalThreadShouldExit();
  189. notify();
  190. if (timeOutMilliseconds != 0)
  191. waitForThreadToExit (timeOutMilliseconds);
  192. if (isThreadRunning())
  193. {
  194. // very bad karma if this point is reached, as there are bound to be
  195. // locks and events left in silly states when a thread is killed by force..
  196. jassertfalse;
  197. Logger::writeToLog ("!! killing thread by force !!");
  198. killThread();
  199. threadHandle = nullptr;
  200. threadId = {};
  201. return false;
  202. }
  203. }
  204. return true;
  205. }
  206. void Thread::addListener (Listener* listener)
  207. {
  208. listeners.add (listener);
  209. }
  210. void Thread::removeListener (Listener* listener)
  211. {
  212. listeners.remove (listener);
  213. }
  214. bool Thread::isRealtime() const
  215. {
  216. return realtimeOptions.has_value();
  217. }
  218. void Thread::setAffinityMask (const uint32 newAffinityMask)
  219. {
  220. affinityMask = newAffinityMask;
  221. }
  222. //==============================================================================
  223. bool Thread::wait (double timeOutMilliseconds) const
  224. {
  225. return defaultEvent.wait (timeOutMilliseconds);
  226. }
  227. void Thread::notify() const
  228. {
  229. defaultEvent.signal();
  230. }
  231. //==============================================================================
  232. struct LambdaThread final : public Thread
  233. {
  234. LambdaThread (std::function<void()>&& f) : Thread ("anonymous"), fn (std::move (f)) {}
  235. void run() override
  236. {
  237. fn();
  238. fn = nullptr; // free any objects that the lambda might contain while the thread is still active
  239. }
  240. std::function<void()> fn;
  241. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LambdaThread)
  242. };
  243. bool Thread::launch (std::function<void()> functionToRun)
  244. {
  245. return launch (Priority::normal, std::move (functionToRun));
  246. }
  247. bool Thread::launch (Priority priority, std::function<void()> functionToRun)
  248. {
  249. auto anon = std::make_unique<LambdaThread> (std::move (functionToRun));
  250. anon->deleteOnThreadEnd = true;
  251. if (anon->startThread (priority))
  252. {
  253. anon.release();
  254. return true;
  255. }
  256. return false;
  257. }
  258. //==============================================================================
  259. void SpinLock::enter() const noexcept
  260. {
  261. if (! tryEnter())
  262. {
  263. for (int i = 20; --i >= 0;)
  264. if (tryEnter())
  265. return;
  266. while (! tryEnter())
  267. Thread::yield();
  268. }
  269. }
  270. //==============================================================================
  271. bool JUCE_CALLTYPE Process::isRunningUnderDebugger() noexcept
  272. {
  273. return juce_isRunningUnderDebugger();
  274. }
  275. //==============================================================================
  276. //==============================================================================
  277. #if JUCE_UNIT_TESTS
  278. class AtomicTests final : public UnitTest
  279. {
  280. public:
  281. AtomicTests()
  282. : UnitTest ("Atomics", UnitTestCategories::threads)
  283. {}
  284. void runTest() override
  285. {
  286. beginTest ("Misc");
  287. char a1[7];
  288. expect (numElementsInArray (a1) == 7);
  289. int a2[3];
  290. expect (numElementsInArray (a2) == 3);
  291. expect (ByteOrder::swap ((uint16) 0x1122) == 0x2211);
  292. expect (ByteOrder::swap ((uint32) 0x11223344) == 0x44332211);
  293. expect (ByteOrder::swap ((uint64) 0x1122334455667788ULL) == (uint64) 0x8877665544332211LL);
  294. beginTest ("Atomic int");
  295. AtomicTester <int>::testInteger (*this);
  296. beginTest ("Atomic unsigned int");
  297. AtomicTester <unsigned int>::testInteger (*this);
  298. beginTest ("Atomic int32");
  299. AtomicTester <int32>::testInteger (*this);
  300. beginTest ("Atomic uint32");
  301. AtomicTester <uint32>::testInteger (*this);
  302. beginTest ("Atomic long");
  303. AtomicTester <long>::testInteger (*this);
  304. beginTest ("Atomic int*");
  305. AtomicTester <int*>::testInteger (*this);
  306. beginTest ("Atomic float");
  307. AtomicTester <float>::testFloat (*this);
  308. #if ! JUCE_64BIT_ATOMICS_UNAVAILABLE // 64-bit intrinsics aren't available on some old platforms
  309. beginTest ("Atomic int64");
  310. AtomicTester <int64>::testInteger (*this);
  311. beginTest ("Atomic uint64");
  312. AtomicTester <uint64>::testInteger (*this);
  313. beginTest ("Atomic double");
  314. AtomicTester <double>::testFloat (*this);
  315. #endif
  316. beginTest ("Atomic pointer increment/decrement");
  317. Atomic<int*> a (a2); int* b (a2);
  318. expect (++a == ++b);
  319. {
  320. beginTest ("Atomic void*");
  321. Atomic<void*> atomic;
  322. void* c;
  323. atomic.set ((void*) 10);
  324. c = (void*) 10;
  325. expect (atomic.value == c);
  326. expect (atomic.get() == c);
  327. }
  328. }
  329. template <typename Type>
  330. class AtomicTester
  331. {
  332. public:
  333. AtomicTester() = default;
  334. static void testInteger (UnitTest& test)
  335. {
  336. Atomic<Type> a, b;
  337. Type c;
  338. a.set ((Type) 10);
  339. c = (Type) 10;
  340. test.expect (a.value == c);
  341. test.expect (a.get() == c);
  342. a += 15;
  343. c += 15;
  344. test.expect (a.get() == c);
  345. a.memoryBarrier();
  346. a -= 5;
  347. c -= 5;
  348. test.expect (a.get() == c);
  349. test.expect (++a == ++c);
  350. ++a;
  351. ++c;
  352. test.expect (--a == --c);
  353. test.expect (a.get() == c);
  354. a.memoryBarrier();
  355. testFloat (test);
  356. }
  357. static void testFloat (UnitTest& test)
  358. {
  359. Atomic<Type> a, b;
  360. a = (Type) 101;
  361. a.memoryBarrier();
  362. /* These are some simple test cases to check the atomics - let me know
  363. if any of these assertions fail on your system!
  364. */
  365. test.expect (exactlyEqual (a.get(), (Type) 101));
  366. test.expect (! a.compareAndSetBool ((Type) 300, (Type) 200));
  367. test.expect (exactlyEqual (a.get(), (Type) 101));
  368. test.expect (a.compareAndSetBool ((Type) 200, a.get()));
  369. test.expect (exactlyEqual (a.get(), (Type) 200));
  370. test.expect (exactlyEqual (a.exchange ((Type) 300), (Type) 200));
  371. test.expect (exactlyEqual (a.get(), (Type) 300));
  372. b = a;
  373. test.expect (exactlyEqual (b.get(), a.get()));
  374. }
  375. };
  376. };
  377. static AtomicTests atomicUnitTests;
  378. //==============================================================================
  379. class ThreadLocalValueUnitTest final : public UnitTest,
  380. private Thread
  381. {
  382. public:
  383. ThreadLocalValueUnitTest()
  384. : UnitTest ("ThreadLocalValue", UnitTestCategories::threads),
  385. Thread ("ThreadLocalValue Thread")
  386. {}
  387. void runTest() override
  388. {
  389. beginTest ("values are thread local");
  390. {
  391. ThreadLocalValue<int> threadLocal;
  392. sharedThreadLocal = &threadLocal;
  393. sharedThreadLocal.get()->get() = 1;
  394. startThread();
  395. signalThreadShouldExit();
  396. waitForThreadToExit (-1);
  397. mainThreadResult = sharedThreadLocal.get()->get();
  398. expectEquals (mainThreadResult.get(), 1);
  399. expectEquals (auxThreadResult.get(), 2);
  400. }
  401. beginTest ("values are per-instance");
  402. {
  403. ThreadLocalValue<int> a, b;
  404. a.get() = 1;
  405. b.get() = 2;
  406. expectEquals (a.get(), 1);
  407. expectEquals (b.get(), 2);
  408. }
  409. }
  410. private:
  411. Atomic<int> mainThreadResult, auxThreadResult;
  412. Atomic<ThreadLocalValue<int>*> sharedThreadLocal;
  413. void run() override
  414. {
  415. sharedThreadLocal.get()->get() = 2;
  416. auxThreadResult = sharedThreadLocal.get()->get();
  417. }
  418. };
  419. ThreadLocalValueUnitTest threadLocalValueUnitTest;
  420. #endif
  421. } // namespace juce