OpenShot Audio Library | OpenShotAudio  0.3.1
juce_Thread.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
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 
23 namespace juce
24 {
25 
26 //==============================================================================
42 class JUCE_API Thread
43 {
44 public:
45  //==============================================================================
58  explicit Thread (const String& threadName, size_t threadStackSize = 0);
59 
67  virtual ~Thread();
68 
69  //==============================================================================
78  virtual void run() = 0;
79 
80  //==============================================================================
88  void startThread();
89 
97  void startThread (int priority);
98 
119  bool stopThread (int timeOutMilliseconds);
120 
121  //==============================================================================
133  static void launch (std::function<void()> functionToRun);
134 
135  //==============================================================================
137  bool isThreadRunning() const;
138 
150  void signalThreadShouldExit();
151 
159  bool threadShouldExit() const;
160 
167  static bool currentThreadShouldExit();
168 
176  bool waitForThreadToExit (int timeOutMilliseconds) const;
177 
178  //==============================================================================
180  class JUCE_API Listener
181  {
182  public:
183  virtual ~Listener() = default;
184 
188  virtual void exitSignalSent() = 0;
189  };
190 
196  void addListener (Listener*);
197 
199  void removeListener (Listener*);
200 
201  //==============================================================================
222  enum
223  {
224  realtimeAudioPriority = -1
225  };
226 
235  bool setPriority (int priority);
236 
244  static bool setCurrentThreadPriority (int priority);
245 
246  //==============================================================================
254  void setAffinityMask (uint32 affinityMask);
255 
262  static void JUCE_CALLTYPE setCurrentThreadAffinityMask (uint32 affinityMask);
263 
264  //==============================================================================
272  static void JUCE_CALLTYPE sleep (int milliseconds);
273 
279  static void JUCE_CALLTYPE yield();
280 
281  //==============================================================================
289  bool wait (int timeOutMilliseconds) const;
290 
297  void notify() const;
298 
299  //==============================================================================
304  using ThreadID = void*;
305 
313  static ThreadID JUCE_CALLTYPE getCurrentThreadId();
314 
320  static Thread* JUCE_CALLTYPE getCurrentThread();
321 
330  ThreadID getThreadId() const noexcept;
331 
333  const String& getThreadName() const noexcept { return threadName; }
334 
339  static void JUCE_CALLTYPE setCurrentThreadName (const String& newThreadName);
340 
341  #if JUCE_ANDROID || defined (DOXYGEN)
342  //==============================================================================
380  static void initialiseJUCE (void* jniEnv, void* jContext);
381  #endif
382 
383 private:
384  //==============================================================================
385  const String threadName;
386  Atomic<void*> threadHandle { nullptr };
387  Atomic<ThreadID> threadId = {};
388  CriticalSection startStopLock;
389  WaitableEvent startSuspensionEvent, defaultEvent;
390  int threadPriority = 5;
391  size_t threadStackSize;
392  uint32 affinityMask = 0;
393  bool deleteOnThreadEnd = false;
394  Atomic<int32> shouldExit { 0 };
396 
397  #if JUCE_ANDROID
398  bool isAndroidRealtimeThread = false;
399  #endif
400 
401  #ifndef DOXYGEN
402  friend void JUCE_API juce_threadEntryPoint (void*);
403  #endif
404 
405  void launchThread();
406  void closeThreadHandle();
407  void killThread();
408  void threadEntryPoint();
409  static bool setThreadPriority (void*, int);
410 
411  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Thread)
412 };
413 
414 } // namespace juce
void * ThreadID
Definition: juce_Thread.h:304