juce_Thread.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. Thread::Thread (const String& threadName_)
  22. : threadName (threadName_),
  23. threadHandle (nullptr),
  24. threadId (0),
  25. threadPriority (5),
  26. affinityMask (0),
  27. shouldExit (false)
  28. {
  29. }
  30. Thread::~Thread()
  31. {
  32. /* If your thread class's destructor has been called without first stopping the thread, that
  33. means that this partially destructed object is still performing some work - and that's
  34. probably a Bad Thing!
  35. To avoid this type of nastiness, always make sure you call stopThread() before or during
  36. your subclass's destructor.
  37. */
  38. jassert (! isThreadRunning());
  39. stopThread (-1);
  40. }
  41. //==============================================================================
  42. // Use a ref-counted object to hold this shared data, so that it can outlive its static
  43. // shared pointer when threads are still running during static shutdown.
  44. struct CurrentThreadHolder : public ReferenceCountedObject
  45. {
  46. CurrentThreadHolder() noexcept {}
  47. typedef ReferenceCountedObjectPtr <CurrentThreadHolder> Ptr;
  48. ThreadLocalValue<Thread*> value;
  49. JUCE_DECLARE_NON_COPYABLE (CurrentThreadHolder)
  50. };
  51. static char currentThreadHolderLock [sizeof (SpinLock)]; // (statically initialised to zeros).
  52. static SpinLock* castToSpinLockWithoutAliasingWarning (void* s)
  53. {
  54. return static_cast<SpinLock*> (s);
  55. }
  56. static CurrentThreadHolder::Ptr getCurrentThreadHolder()
  57. {
  58. static CurrentThreadHolder::Ptr currentThreadHolder;
  59. SpinLock::ScopedLockType lock (*castToSpinLockWithoutAliasingWarning (currentThreadHolderLock));
  60. if (currentThreadHolder == nullptr)
  61. currentThreadHolder = new CurrentThreadHolder();
  62. return currentThreadHolder;
  63. }
  64. void Thread::threadEntryPoint()
  65. {
  66. const CurrentThreadHolder::Ptr currentThreadHolder (getCurrentThreadHolder());
  67. currentThreadHolder->value = this;
  68. JUCE_TRY
  69. {
  70. if (threadName.isNotEmpty())
  71. setCurrentThreadName (threadName);
  72. if (startSuspensionEvent.wait (10000))
  73. {
  74. jassert (getCurrentThreadId() == threadId);
  75. if (affinityMask != 0)
  76. setCurrentThreadAffinityMask (affinityMask);
  77. run();
  78. }
  79. }
  80. JUCE_CATCH_ALL_ASSERT
  81. currentThreadHolder->value.releaseCurrentThreadStorage();
  82. closeThreadHandle();
  83. }
  84. // used to wrap the incoming call from the platform-specific code
  85. void JUCE_API juce_threadEntryPoint (void* userData)
  86. {
  87. static_cast <Thread*> (userData)->threadEntryPoint();
  88. }
  89. //==============================================================================
  90. void Thread::startThread()
  91. {
  92. const ScopedLock sl (startStopLock);
  93. shouldExit = false;
  94. if (threadHandle == nullptr)
  95. {
  96. launchThread();
  97. setThreadPriority (threadHandle, threadPriority);
  98. startSuspensionEvent.signal();
  99. }
  100. }
  101. void Thread::startThread (const int priority)
  102. {
  103. const ScopedLock sl (startStopLock);
  104. if (threadHandle == nullptr)
  105. {
  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::waitForThreadToExit (const int timeOutMilliseconds) const
  128. {
  129. // Doh! So how exactly do you expect this thread to wait for itself to stop??
  130. jassert (getThreadId() != getCurrentThreadId() || getCurrentThreadId() == 0);
  131. const uint32 timeoutEnd = Time::getMillisecondCounter() + (uint32) timeOutMilliseconds;
  132. while (isThreadRunning())
  133. {
  134. if (timeOutMilliseconds >= 0 && Time::getMillisecondCounter() > timeoutEnd)
  135. return false;
  136. sleep (2);
  137. }
  138. return true;
  139. }
  140. bool Thread::stopThread (const int timeOutMilliseconds)
  141. {
  142. // agh! You can't stop the thread that's calling this method! How on earth
  143. // would that work??
  144. jassert (getCurrentThreadId() != getThreadId());
  145. const ScopedLock sl (startStopLock);
  146. if (isThreadRunning())
  147. {
  148. signalThreadShouldExit();
  149. notify();
  150. if (timeOutMilliseconds != 0)
  151. waitForThreadToExit (timeOutMilliseconds);
  152. if (isThreadRunning())
  153. {
  154. // very bad karma if this point is reached, as there are bound to be
  155. // locks and events left in silly states when a thread is killed by force..
  156. jassertfalse;
  157. Logger::writeToLog ("!! killing thread by force !!");
  158. killThread();
  159. threadHandle = nullptr;
  160. threadId = 0;
  161. return false;
  162. }
  163. }
  164. return true;
  165. }
  166. //==============================================================================
  167. bool Thread::setPriority (const int newPriority)
  168. {
  169. // NB: deadlock possible if you try to set the thread prio from the thread itself,
  170. // so using setCurrentThreadPriority instead in that case.
  171. if (getCurrentThreadId() == getThreadId())
  172. return setCurrentThreadPriority (newPriority);
  173. const ScopedLock sl (startStopLock);
  174. if ((! isThreadRunning()) || setThreadPriority (threadHandle, newPriority))
  175. {
  176. threadPriority = newPriority;
  177. return true;
  178. }
  179. return false;
  180. }
  181. bool Thread::setCurrentThreadPriority (const int newPriority)
  182. {
  183. return setThreadPriority (0, newPriority);
  184. }
  185. void Thread::setAffinityMask (const uint32 newAffinityMask)
  186. {
  187. affinityMask = newAffinityMask;
  188. }
  189. //==============================================================================
  190. bool Thread::wait (const int timeOutMilliseconds) const
  191. {
  192. return defaultEvent.wait (timeOutMilliseconds);
  193. }
  194. void Thread::notify() const
  195. {
  196. defaultEvent.signal();
  197. }
  198. //==============================================================================
  199. void SpinLock::enter() const noexcept
  200. {
  201. if (! tryEnter())
  202. {
  203. for (int i = 20; --i >= 0;)
  204. if (tryEnter())
  205. return;
  206. while (! tryEnter())
  207. Thread::yield();
  208. }
  209. }
  210. //==============================================================================
  211. #if JUCE_UNIT_TESTS
  212. class AtomicTests : public UnitTest
  213. {
  214. public:
  215. AtomicTests() : UnitTest ("Atomics") {}
  216. void runTest()
  217. {
  218. beginTest ("Misc");
  219. char a1[7];
  220. expect (numElementsInArray(a1) == 7);
  221. int a2[3];
  222. expect (numElementsInArray(a2) == 3);
  223. expect (ByteOrder::swap ((uint16) 0x1122) == 0x2211);
  224. expect (ByteOrder::swap ((uint32) 0x11223344) == 0x44332211);
  225. expect (ByteOrder::swap ((uint64) 0x1122334455667788ULL) == 0x8877665544332211LL);
  226. beginTest ("Atomic int");
  227. AtomicTester <int>::testInteger (*this);
  228. beginTest ("Atomic unsigned int");
  229. AtomicTester <unsigned int>::testInteger (*this);
  230. beginTest ("Atomic int32");
  231. AtomicTester <int32>::testInteger (*this);
  232. beginTest ("Atomic uint32");
  233. AtomicTester <uint32>::testInteger (*this);
  234. beginTest ("Atomic long");
  235. AtomicTester <long>::testInteger (*this);
  236. beginTest ("Atomic void*");
  237. AtomicTester <void*>::testInteger (*this);
  238. beginTest ("Atomic int*");
  239. AtomicTester <int*>::testInteger (*this);
  240. beginTest ("Atomic float");
  241. AtomicTester <float>::testFloat (*this);
  242. #if ! JUCE_64BIT_ATOMICS_UNAVAILABLE // 64-bit intrinsics aren't available on some old platforms
  243. beginTest ("Atomic int64");
  244. AtomicTester <int64>::testInteger (*this);
  245. beginTest ("Atomic uint64");
  246. AtomicTester <uint64>::testInteger (*this);
  247. beginTest ("Atomic double");
  248. AtomicTester <double>::testFloat (*this);
  249. #endif
  250. }
  251. template <typename Type>
  252. class AtomicTester
  253. {
  254. public:
  255. AtomicTester() {}
  256. static void testInteger (UnitTest& test)
  257. {
  258. Atomic<Type> a, b;
  259. a.set ((Type) 10);
  260. test.expect (a.value == (Type) 10);
  261. test.expect (a.get() == (Type) 10);
  262. a += (Type) 15;
  263. test.expect (a.get() == (Type) 25);
  264. a.memoryBarrier();
  265. a -= (Type) 5;
  266. test.expect (a.get() == (Type) 20);
  267. test.expect (++a == (Type) 21);
  268. ++a;
  269. test.expect (--a == (Type) 21);
  270. test.expect (a.get() == (Type) 21);
  271. a.memoryBarrier();
  272. testFloat (test);
  273. }
  274. static void testFloat (UnitTest& test)
  275. {
  276. Atomic<Type> a, b;
  277. a = (Type) 21;
  278. a.memoryBarrier();
  279. /* These are some simple test cases to check the atomics - let me know
  280. if any of these assertions fail on your system!
  281. */
  282. test.expect (a.get() == (Type) 21);
  283. test.expect (a.compareAndSetValue ((Type) 100, (Type) 50) == (Type) 21);
  284. test.expect (a.get() == (Type) 21);
  285. test.expect (a.compareAndSetValue ((Type) 101, a.get()) == (Type) 21);
  286. test.expect (a.get() == (Type) 101);
  287. test.expect (! a.compareAndSetBool ((Type) 300, (Type) 200));
  288. test.expect (a.get() == (Type) 101);
  289. test.expect (a.compareAndSetBool ((Type) 200, a.get()));
  290. test.expect (a.get() == (Type) 200);
  291. test.expect (a.exchange ((Type) 300) == (Type) 200);
  292. test.expect (a.get() == (Type) 300);
  293. b = a;
  294. test.expect (b.get() == a.get());
  295. }
  296. };
  297. };
  298. static AtomicTests atomicUnitTests;
  299. #endif