JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_PlatformTimer_generic.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26class PlatformTimer final : private Thread
27{
28public:
30 : Thread { "HighResolutionTimerThread" },
31 listener { ptl }
32 {
33 startThread (Priority::highest);
34 }
35
37 {
38 stopThread (-1);
39 }
40
41 void startTimer (int newIntervalMs)
42 {
44 jassert (timer == nullptr);
45
46 {
47 std::scoped_lock lock { runCopyMutex };
48 timer = std::make_shared<Timer> (listener, newIntervalMs);
49 }
50
51 notify();
52 }
53
54 void cancelTimer()
55 {
56 jassert (timer != nullptr);
57
58 timer->cancel();
59
60 // Note the only race condition we need to protect against
61 // here is the copy in run().
62 //
63 // Calls to startTimer(), cancelTimer(), and getIntervalMs()
64 // are already guaranteed to be both thread safe and well
65 // synchronised.
66
67 std::scoped_lock lock { runCopyMutex };
68 timer = nullptr;
69 }
70
71 int getIntervalMs() const
72 {
73 return isThreadRunning() && timer != nullptr ? timer->getIntervalMs() : 0;
74 }
75
76private:
77 void run() final
78 {
79 const auto copyTimer = [&]
80 {
81 std::scoped_lock lock { runCopyMutex };
82 return timer;
83 };
84
85 while (! threadShouldExit())
86 {
87 if (auto t = copyTimer())
88 t->run();
89
90 wait (-1);
91 }
92 }
93
94 class Timer
95 {
96 public:
97 Timer (PlatformTimerListener& l, int i)
98 : listener { l }, intervalMs { i } {}
99
100 int getIntervalMs() const
101 {
102 return intervalMs;
103 }
104
105 void cancel()
106 {
107 stop.signal();
108 }
109
110 void run()
111 {
112 #if JUCE_MAC || JUCE_IOS
114 #endif
115
116 const auto millisecondsUntil = [] (auto time)
117 {
119 };
120
121 while (! stop.wait (millisecondsUntil (nextEventTime)))
122 {
123 if (Time::getMillisecondCounterHiRes() >= nextEventTime)
124 {
125 listener.onTimerExpired();
126 nextEventTime += intervalMs;
127 }
128 }
129 }
130
131 private:
132 PlatformTimerListener& listener;
133 const int intervalMs;
134 double nextEventTime = Time::getMillisecondCounterHiRes() + intervalMs;
135 WaitableEvent stop { true };
136
139 };
140
141 PlatformTimerListener& listener;
142 mutable std::mutex runCopyMutex;
144
147};
148
149} // namespace juce
Encapsulates a thread.
Definition juce_Thread.h:43
bool startThread()
Attempts to start a new thread with default ('Priority::normal') priority.
bool threadShouldExit() const
Checks whether the thread has been told to stop running.
bool stopThread(int timeOutMilliseconds)
Attempts to stop the thread running.
void notify() const
Wakes up the thread.
bool isThreadRunning() const
Returns true if the thread is currently active.
static double getMillisecondCounterHiRes() noexcept
Returns the number of millisecs since a fixed event (usually system startup).
Allows threads to wait for events triggered by other threads.
#define jassert(expression)
Platform-independent assertion macro.
#define JUCE_DECLARE_NON_MOVEABLE(className)
This is a shorthand macro for deleting a class's move constructor and move assignment operator.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
JUCE Namespace.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
A selection of options available when creating realtime threads.
Definition juce_Thread.h:77
RealtimeOptions withPeriodMs(double newPeriodMs) const
Specify the approximate amount of time between each thread wake up.
time
wait