OpenShot Library | OpenShotAudio  0.2.2
juce_TimeSliceThread.h
1 
2 /** @weakgroup juce_core-threads
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 class TimeSliceThread;
31 
32 
33 //==============================================================================
34 /**
35  Used by the TimeSliceThread class.
36 
37  To register your class with a TimeSliceThread, derive from this class and
38  use the TimeSliceThread::addTimeSliceClient() method to add it to the list.
39 
40  Make sure you always call TimeSliceThread::removeTimeSliceClient() before
41  deleting your client!
42 
43  @see TimeSliceThread
44 
45  @tags{Core}
46 */
48 {
49 public:
50  /** Destructor. */
51  virtual ~TimeSliceClient() = default;
52 
53  /** Called back by a TimeSliceThread.
54 
55  When you register this class with it, a TimeSliceThread will repeatedly call
56  this method.
57 
58  The implementation of this method should use its time-slice to do something that's
59  quick - never block for longer than absolutely necessary.
60 
61  @returns Your method should return the number of milliseconds which it would like to wait before being called
62  again. Returning 0 will make the thread call again as soon as possible (after possibly servicing
63  other busy clients). If you return a value below zero, your client will be removed from the list of clients,
64  and won't be called again. The value you specify isn't a guarantee, and is only used as a hint by the
65  thread - the actual time before the next callback may be more or less than specified.
66  You can force the TimeSliceThread to wake up and poll again immediately by calling its notify() method.
67  */
68  virtual int useTimeSlice() = 0;
69 
70 
71 private:
72  friend class TimeSliceThread;
73  Time nextCallTime;
74 };
75 
76 
77 //==============================================================================
78 /**
79  A thread that keeps a list of clients, and calls each one in turn, giving them
80  all a chance to run some sort of short task.
81 
82  @see TimeSliceClient, Thread
83 
84  @tags{Core}
85 */
87 {
88 public:
89  //==============================================================================
90  /**
91  Creates a TimeSliceThread.
92 
93  When first created, the thread is not running. Use the startThread()
94  method to start it.
95  */
96  explicit TimeSliceThread (const String& threadName);
97 
98  /** Destructor.
99 
100  Deleting a Thread object that is running will only give the thread a
101  brief opportunity to stop itself cleanly, so it's recommended that you
102  should always call stopThread() with a decent timeout before deleting,
103  to avoid the thread being forcibly killed (which is a Bad Thing).
104  */
105  ~TimeSliceThread() override;
106 
107  //==============================================================================
108  /** Adds a client to the list.
109  The client's callbacks will start after the number of milliseconds specified
110  by millisecondsBeforeStarting (and this may happen before this method has returned).
111  */
112  void addTimeSliceClient (TimeSliceClient* clientToAdd, int millisecondsBeforeStarting = 0);
113 
114  /** If the given client is waiting in the queue, it will be moved to the front
115  and given a time-slice as soon as possible.
116  If the specified client has not been added, nothing will happen.
117  */
118  void moveToFrontOfQueue (TimeSliceClient* clientToMove);
119 
120  /** Removes a client from the list.
121  This method will make sure that all callbacks to the client have completely
122  finished before the method returns.
123  */
124  void removeTimeSliceClient (TimeSliceClient* clientToRemove);
125 
126  /** Removes all the active and pending clients from the list.
127  This method will make sure that all callbacks to clients have finished before the
128  method returns.
129  */
130  void removeAllClients();
131 
132  /** Returns the number of registered clients. */
133  int getNumClients() const;
134 
135  /** Returns one of the registered clients. */
136  TimeSliceClient* getClient (int index) const;
137 
138  //==============================================================================
139  #ifndef DOXYGEN
140  void run() override;
141  #endif
142 
143  //==============================================================================
144 private:
145  CriticalSection callbackLock, listLock;
146  Array<TimeSliceClient*> clients;
147  TimeSliceClient* clientBeingCalled = nullptr;
148 
149  TimeSliceClient* getNextClient (int index) const;
150 
151  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TimeSliceThread)
152 };
153 
154 } // namespace juce
155 
156 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
The JUCE String class!
Definition: juce_String.h:42
Used by the TimeSliceThread class.
Encapsulates a thread.
Definition: juce_Thread.h:46
A thread that keeps a list of clients, and calls each one in turn, giving them all a chance to run so...
Holds a resizable array of primitive or copy-by-value objects.
Definition: juce_Array.h:59
A re-entrant mutex.
Holds an absolute date and time.
Definition: juce_Time.h:40