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.

249 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "../memory/juce_Atomic.h"
  21. #include "../maths/juce_Random.h"
  22. #include "juce_PlatformUtilities.h"
  23. #include "juce_SystemStats.h"
  24. #include "../text/juce_LocalisedStrings.h"
  25. #include "../io/streams/juce_MemoryOutputStream.h"
  26. #include "../io/streams/juce_MemoryInputStream.h"
  27. #include "../threads/juce_Thread.h"
  28. #if ! JUCE_ONLY_BUILD_CORE_LIBRARY
  29. #include "../events/juce_MessageManager.h"
  30. #include "../gui/components/buttons/juce_TextButton.h"
  31. #include "../gui/components/lookandfeel/juce_LookAndFeel.h"
  32. #endif
  33. #if JUCE_WINDOWS
  34. extern void juce_shutdownWin32Sockets(); // (defined in the sockets code)
  35. #endif
  36. #if JUCE_DEBUG
  37. extern void juce_CheckForDanglingStreams(); // (in juce_OutputStream.cpp)
  38. #endif
  39. //==============================================================================
  40. static bool juceInitialisedNonGUI = false;
  41. JUCE_API void JUCE_CALLTYPE initialiseJuce_NonGUI()
  42. {
  43. if (! juceInitialisedNonGUI)
  44. {
  45. juceInitialisedNonGUI = true;
  46. JUCE_AUTORELEASEPOOL
  47. DBG (SystemStats::getJUCEVersion());
  48. SystemStats::initialiseStats();
  49. Random::getSystemRandom().setSeedRandomly(); // (mustn't call this before initialiseStats() because it relies on the time being set up)
  50. }
  51. // Some basic tests, to keep an eye on things and make sure these types work ok
  52. // on all platforms. Let me know if any of these assertions fail on your system!
  53. static_jassert (sizeof (pointer_sized_int) == sizeof (void*));
  54. static_jassert (sizeof (int8) == 1);
  55. static_jassert (sizeof (uint8) == 1);
  56. static_jassert (sizeof (int16) == 2);
  57. static_jassert (sizeof (uint16) == 2);
  58. static_jassert (sizeof (int32) == 4);
  59. static_jassert (sizeof (uint32) == 4);
  60. static_jassert (sizeof (int64) == 8);
  61. static_jassert (sizeof (uint64) == 8);
  62. }
  63. JUCE_API void JUCE_CALLTYPE shutdownJuce_NonGUI()
  64. {
  65. if (juceInitialisedNonGUI)
  66. {
  67. juceInitialisedNonGUI = false;
  68. JUCE_AUTORELEASEPOOL
  69. LocalisedStrings::setCurrentMappings (0);
  70. Thread::stopAllThreads (3000);
  71. #if JUCE_WINDOWS
  72. juce_shutdownWin32Sockets();
  73. #endif
  74. #if JUCE_DEBUG
  75. juce_CheckForDanglingStreams();
  76. #endif
  77. }
  78. }
  79. //==============================================================================
  80. #if ! JUCE_ONLY_BUILD_CORE_LIBRARY
  81. static bool juceInitialisedGUI = false;
  82. JUCE_API void JUCE_CALLTYPE initialiseJuce_GUI()
  83. {
  84. if (! juceInitialisedGUI)
  85. {
  86. juceInitialisedGUI = true;
  87. JUCE_AUTORELEASEPOOL
  88. initialiseJuce_NonGUI();
  89. MessageManager::getInstance();
  90. LookAndFeel::setDefaultLookAndFeel (0);
  91. #if JUCE_DEBUG
  92. try // This section is just a safety-net for catching builds without RTTI enabled..
  93. {
  94. MemoryOutputStream mo;
  95. OutputStream* o = &mo;
  96. // Got an exception here? Then TURN ON RTTI in your compiler settings!!
  97. o = dynamic_cast <MemoryOutputStream*> (o);
  98. jassert (o != 0);
  99. }
  100. catch (...)
  101. {
  102. // Ended up here? If so, TURN ON RTTI in your compiler settings!!
  103. jassertfalse;
  104. }
  105. #endif
  106. }
  107. }
  108. JUCE_API void JUCE_CALLTYPE shutdownJuce_GUI()
  109. {
  110. if (juceInitialisedGUI)
  111. {
  112. juceInitialisedGUI = false;
  113. JUCE_AUTORELEASEPOOL
  114. DeletedAtShutdown::deleteAll();
  115. LookAndFeel::clearDefaultLookAndFeel();
  116. delete MessageManager::getInstance();
  117. shutdownJuce_NonGUI();
  118. }
  119. }
  120. #endif
  121. //==============================================================================
  122. #if JUCE_UNIT_TESTS
  123. #include "../utilities/juce_UnitTest.h"
  124. class AtomicTests : public UnitTest
  125. {
  126. public:
  127. AtomicTests() : UnitTest ("Atomics") {}
  128. void runTest()
  129. {
  130. beginTest ("Misc");
  131. char a1[7];
  132. expect (numElementsInArray(a1) == 7);
  133. int a2[3];
  134. expect (numElementsInArray(a2) == 3);
  135. expect (ByteOrder::swap ((uint16) 0x1122) == 0x2211);
  136. expect (ByteOrder::swap ((uint32) 0x11223344) == 0x44332211);
  137. expect (ByteOrder::swap ((uint64) literal64bit (0x1122334455667788)) == literal64bit (0x8877665544332211));
  138. beginTest ("Atomic types");
  139. AtomicTester <int>::testInteger (*this);
  140. AtomicTester <unsigned int>::testInteger (*this);
  141. AtomicTester <int32>::testInteger (*this);
  142. AtomicTester <uint32>::testInteger (*this);
  143. AtomicTester <long>::testInteger (*this);
  144. AtomicTester <void*>::testInteger (*this);
  145. AtomicTester <int*>::testInteger (*this);
  146. AtomicTester <float>::testFloat (*this);
  147. #if ! JUCE_64BIT_ATOMICS_UNAVAILABLE // 64-bit intrinsics aren't available on some old platforms
  148. AtomicTester <int64>::testInteger (*this);
  149. AtomicTester <uint64>::testInteger (*this);
  150. AtomicTester <double>::testFloat (*this);
  151. #endif
  152. }
  153. template <typename Type>
  154. class AtomicTester
  155. {
  156. public:
  157. AtomicTester() {}
  158. static void testInteger (UnitTest& test)
  159. {
  160. Atomic<Type> a, b;
  161. a.set ((Type) 10);
  162. a += (Type) 15;
  163. a.memoryBarrier();
  164. a -= (Type) 5;
  165. ++a; ++a; --a;
  166. a.memoryBarrier();
  167. testFloat (test);
  168. }
  169. static void testFloat (UnitTest& test)
  170. {
  171. Atomic<Type> a, b;
  172. a = (Type) 21;
  173. a.memoryBarrier();
  174. /* These are some simple test cases to check the atomics - let me know
  175. if any of these assertions fail on your system!
  176. */
  177. test.expect (a.get() == (Type) 21);
  178. test.expect (a.compareAndSetValue ((Type) 100, (Type) 50) == (Type) 21);
  179. test.expect (a.get() == (Type) 21);
  180. test.expect (a.compareAndSetValue ((Type) 101, a.get()) == (Type) 21);
  181. test.expect (a.get() == (Type) 101);
  182. test.expect (! a.compareAndSetBool ((Type) 300, (Type) 200));
  183. test.expect (a.get() == (Type) 101);
  184. test.expect (a.compareAndSetBool ((Type) 200, a.get()));
  185. test.expect (a.get() == (Type) 200);
  186. test.expect (a.exchange ((Type) 300) == (Type) 200);
  187. test.expect (a.get() == (Type) 300);
  188. b = a;
  189. test.expect (b.get() == a.get());
  190. }
  191. };
  192. };
  193. static AtomicTests atomicUnitTests;
  194. #endif
  195. END_JUCE_NAMESPACE