Audio plugin host https://kx.studio/carla
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.

330 lines
9.3KB

  1. //------------------------------------------------------------------------
  2. // Project : SDK Base
  3. // Version : 1.0
  4. //
  5. // Category : Helpers
  6. // Filename : base/source/timer.cpp
  7. // Created by : Steinberg, 05/2006
  8. // Description : Timer class for receiving triggers at regular intervals
  9. //
  10. //-----------------------------------------------------------------------------
  11. // LICENSE
  12. // (c) 2017, Steinberg Media Technologies GmbH, All Rights Reserved
  13. //-----------------------------------------------------------------------------
  14. // Redistribution and use in source and binary forms, with or without modification,
  15. // are permitted provided that the following conditions are met:
  16. //
  17. // * Redistributions of source code must retain the above copyright notice,
  18. // this list of conditions and the following disclaimer.
  19. // * Redistributions in binary form must reproduce the above copyright notice,
  20. // this list of conditions and the following disclaimer in the documentation
  21. // and/or other materials provided with the distribution.
  22. // * Neither the name of the Steinberg Media Technologies nor the names of its
  23. // contributors may be used to endorse or promote products derived from this
  24. // software without specific prior written permission.
  25. //
  26. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  27. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  28. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  29. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  30. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  34. // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  35. // OF THE POSSIBILITY OF SUCH DAMAGE.
  36. //-----------------------------------------------------------------------------
  37. #include "base/source/timer.h"
  38. namespace Steinberg {
  39. static bool timersEnabled = true;
  40. //------------------------------------------------------------------------
  41. DisableDispatchingTimers::DisableDispatchingTimers ()
  42. {
  43. oldState = timersEnabled;
  44. timersEnabled = false;
  45. }
  46. //------------------------------------------------------------------------
  47. DisableDispatchingTimers::~DisableDispatchingTimers ()
  48. {
  49. timersEnabled = oldState;
  50. }
  51. //------------------------------------------------------------------------
  52. namespace SystemTime {
  53. //------------------------------------------------------------------------
  54. struct ZeroStartTicks
  55. {
  56. static const uint64 startTicks;
  57. static int32 getTicks32 ()
  58. {
  59. return static_cast<int32> (SystemTime::getTicks64 () - startTicks);
  60. }
  61. };
  62. const uint64 ZeroStartTicks::startTicks = SystemTime::getTicks64 ();
  63. //------------------------------------------------------------------------
  64. int32 getTicks ()
  65. {
  66. return ZeroStartTicks::getTicks32 ();
  67. }
  68. //------------------------------------------------------------------------
  69. } // namespace SystemTime
  70. } // namespace Steinberg
  71. #if MAC
  72. #include <CoreFoundation/CoreFoundation.h>
  73. #include <mach/mach_time.h>
  74. #ifdef verify
  75. #undef verify
  76. #endif
  77. namespace Steinberg {
  78. namespace SystemTime {
  79. struct MachTimeBase {
  80. private:
  81. struct mach_timebase_info timebaseInfo;
  82. MachTimeBase ()
  83. {
  84. mach_timebase_info (&timebaseInfo);
  85. }
  86. static const MachTimeBase& instance ()
  87. {
  88. static MachTimeBase gInstance;
  89. return gInstance;
  90. }
  91. public:
  92. static double getTimeNanos ()
  93. {
  94. const MachTimeBase& timeBase = instance ();
  95. double absTime = static_cast<double> (mach_absolute_time ());
  96. double d = (absTime / timeBase.timebaseInfo.denom) * timeBase.timebaseInfo.numer; // nano seconds
  97. return d;
  98. }
  99. };
  100. //------------------------------------------------------------------------
  101. uint64 getTicks64 ()
  102. {
  103. return static_cast<uint64> (MachTimeBase::getTimeNanos () / 1000000.);
  104. }
  105. } // namespace SystemTime
  106. //------------------------------------------------------------------------
  107. class MacPlatformTimer : public Timer
  108. {
  109. public:
  110. MacPlatformTimer (ITimerCallback* callback, uint32 milliseconds);
  111. ~MacPlatformTimer ();
  112. void stop ();
  113. bool verify () const { return platformTimer != 0; }
  114. static void timerCallback (CFRunLoopTimerRef timer, void *info);
  115. protected:
  116. CFRunLoopTimerRef platformTimer;
  117. ITimerCallback* callback;
  118. };
  119. //------------------------------------------------------------------------
  120. MacPlatformTimer::MacPlatformTimer (ITimerCallback* callback, uint32 milliseconds)
  121. : platformTimer (0)
  122. , callback (callback)
  123. {
  124. if (callback)
  125. {
  126. CFRunLoopTimerContext timerContext = {0};
  127. timerContext.info = this;
  128. platformTimer = CFRunLoopTimerCreate (kCFAllocatorDefault, CFAbsoluteTimeGetCurrent () + milliseconds * 0.001, milliseconds * 0.001f, 0, 0, timerCallback, &timerContext);
  129. if (platformTimer)
  130. CFRunLoopAddTimer (CFRunLoopGetMain (), platformTimer, kCFRunLoopCommonModes);
  131. }
  132. }
  133. //------------------------------------------------------------------------
  134. MacPlatformTimer::~MacPlatformTimer ()
  135. {
  136. stop ();
  137. }
  138. //------------------------------------------------------------------------
  139. void MacPlatformTimer::stop ()
  140. {
  141. if (platformTimer)
  142. {
  143. CFRunLoopRemoveTimer (CFRunLoopGetMain (), platformTimer, kCFRunLoopCommonModes);
  144. CFRelease (platformTimer);
  145. platformTimer = 0;
  146. }
  147. }
  148. //------------------------------------------------------------------------
  149. void MacPlatformTimer::timerCallback (CFRunLoopTimerRef , void *info)
  150. {
  151. if (timersEnabled)
  152. {
  153. MacPlatformTimer* timer = (MacPlatformTimer*)info;
  154. if (timer)
  155. {
  156. timer->callback->onTimer (timer);
  157. }
  158. }
  159. }
  160. //------------------------------------------------------------------------
  161. Timer* Timer::create (ITimerCallback* callback, uint32 milliseconds)
  162. {
  163. MacPlatformTimer* timer = NEW MacPlatformTimer (callback, milliseconds);
  164. if (timer->verify ())
  165. return timer;
  166. timer->release ();
  167. return 0;
  168. }
  169. }
  170. #elif WINDOWS
  171. #include <windows.h>
  172. #include <list>
  173. #include <algorithm>
  174. namespace Steinberg {
  175. namespace SystemTime {
  176. /*
  177. @return the current system time in milliseconds
  178. */
  179. uint64 getTicks64 ()
  180. {
  181. return GetTickCount64 ();
  182. }
  183. }
  184. class WinPlatformTimer;
  185. typedef std::list<WinPlatformTimer*> WinPlatformTimerList;
  186. //------------------------------------------------------------------------
  187. // WinPlatformTimer
  188. //------------------------------------------------------------------------
  189. class WinPlatformTimer : public Timer
  190. {
  191. public:
  192. //------------------------------------------------------------------------
  193. WinPlatformTimer (ITimerCallback* callback, uint32 milliseconds);
  194. ~WinPlatformTimer ();
  195. void stop ();
  196. bool verify () const {return id != 0;}
  197. //------------------------------------------------------------------------
  198. private:
  199. UINT_PTR id;
  200. ITimerCallback* callback;
  201. static void addTimer (WinPlatformTimer* t);
  202. static void removeTimer (WinPlatformTimer* t);
  203. static void CALLBACK TimerProc (HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
  204. static WinPlatformTimerList* timers;
  205. };
  206. //------------------------------------------------------------------------
  207. WinPlatformTimerList* WinPlatformTimer::timers = 0;
  208. //------------------------------------------------------------------------
  209. WinPlatformTimer::WinPlatformTimer (ITimerCallback* callback, uint32 milliseconds)
  210. : callback (callback)
  211. {
  212. id = SetTimer (0, 0, milliseconds, TimerProc);
  213. if (id)
  214. addTimer (this);
  215. }
  216. //------------------------------------------------------------------------
  217. WinPlatformTimer::~WinPlatformTimer ()
  218. {
  219. stop ();
  220. }
  221. //------------------------------------------------------------------------
  222. void WinPlatformTimer::addTimer (WinPlatformTimer* t)
  223. {
  224. if (timers == 0)
  225. timers = NEW WinPlatformTimerList;
  226. timers->push_back (t);
  227. }
  228. //------------------------------------------------------------------------
  229. void WinPlatformTimer::removeTimer (WinPlatformTimer* t)
  230. {
  231. if (!timers)
  232. return;
  233. WinPlatformTimerList::iterator it = std::find (timers->begin (), timers->end (), t);
  234. if (it != timers->end ())
  235. timers->erase (it);
  236. if (timers->empty ())
  237. {
  238. delete timers;
  239. timers = 0;
  240. }
  241. }
  242. //------------------------------------------------------------------------
  243. void WinPlatformTimer::stop ()
  244. {
  245. if (!id)
  246. return;
  247. KillTimer (0, id);
  248. removeTimer (this);
  249. id = 0;
  250. }
  251. //------------------------------------------------------------------------
  252. void CALLBACK WinPlatformTimer::TimerProc (HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
  253. {
  254. if (timersEnabled && timers)
  255. {
  256. WinPlatformTimerList::const_iterator it = timers->cbegin ();
  257. while (it != timers->cend ())
  258. {
  259. WinPlatformTimer* timer = *it;
  260. if (timer->id == idEvent)
  261. {
  262. if (timer->callback)
  263. timer->callback->onTimer (timer);
  264. return;
  265. }
  266. }
  267. }
  268. }
  269. //------------------------------------------------------------------------
  270. Timer* Timer::create (ITimerCallback* callback, uint32 milliseconds)
  271. {
  272. WinPlatformTimer* platformTimer = NEW WinPlatformTimer (callback, milliseconds);
  273. if (platformTimer->verify ())
  274. return platformTimer;
  275. platformTimer->release ();
  276. return 0;
  277. }
  278. //------------------------------------------------------------------------
  279. } // namespace Steinberg
  280. #elif LINUX
  281. #warning DEPRECATED No Linux implementation
  282. #endif