openshot-audio  0.1.7
juce_ThreadPool.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the juce_core module of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission to use, copy, modify, and/or distribute this software for any purpose with
8  or without fee is hereby granted, provided that the above copyright notice and this
9  permission notice appear in all copies.
10 
11  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12  TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18  ------------------------------------------------------------------------------
19 
20  NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
21  All other JUCE modules are covered by a dual GPL/commercial license, so if you are
22  using any other modules, be sure to check that you also comply with their license.
23 
24  For more details, visit www.juce.com
25 
26  ==============================================================================
27 */
28 
29 #ifndef JUCE_THREADPOOL_H_INCLUDED
30 #define JUCE_THREADPOOL_H_INCLUDED
31 
32 class ThreadPool;
33 class ThreadPoolThread;
34 
35 
36 //==============================================================================
51 {
52 public:
53  //==============================================================================
57  explicit ThreadPoolJob (const String& name);
58 
60  virtual ~ThreadPoolJob();
61 
62  //==============================================================================
66  String getJobName() const;
67 
71  void setJobName (const String& newName);
72 
73  //==============================================================================
76  enum JobStatus
77  {
78  jobHasFinished = 0,
81  jobNeedsRunningAgain
83  };
84 
99  virtual JobStatus runJob() = 0;
100 
101 
102  //==============================================================================
104  bool isRunning() const noexcept { return isActive; }
105 
113  bool shouldExit() const noexcept { return shouldStop; }
114 
120  void signalJobShouldExit();
121 
122  //==============================================================================
126  static ThreadPoolJob* getCurrentThreadPoolJob();
127 
128  //==============================================================================
129 private:
130  friend class ThreadPool;
131  friend class ThreadPoolThread;
132  String jobName;
133  ThreadPool* pool;
134  bool shouldStop, isActive, shouldBeDeleted;
135 
137 };
138 
139 
140 //==============================================================================
150 {
151 public:
152  //==============================================================================
158  ThreadPool (int numberOfThreads);
159 
166  ThreadPool();
167 
174  ~ThreadPool();
175 
176  //==============================================================================
182  {
183  public:
184  virtual ~JobSelector() {}
185 
191  virtual bool isJobSuitable (ThreadPoolJob* job) = 0;
192  };
193 
194  //==============================================================================
210  void addJob (ThreadPoolJob* job,
211  bool deleteJobWhenFinished);
212 
230  bool removeJob (ThreadPoolJob* job,
231  bool interruptIfRunning,
232  int timeOutMilliseconds);
233 
245  bool removeAllJobs (bool interruptRunningJobs,
246  int timeOutMilliseconds,
247  JobSelector* selectedJobsToRemove = nullptr);
248 
251  int getNumJobs() const;
252 
258  ThreadPoolJob* getJob (int index) const;
259 
264  bool contains (const ThreadPoolJob* job) const;
265 
268  bool isJobRunning (const ThreadPoolJob* job) const;
269 
278  bool waitForJobToFinish (const ThreadPoolJob* job,
279  int timeOutMilliseconds) const;
280 
284  StringArray getNamesOfAllJobs (bool onlyReturnActiveJobs) const;
285 
291  bool setThreadPriorities (int newPriority);
292 
293 
294 private:
295  //==============================================================================
297 
299  friend class ThreadPoolJob;
300  friend class ThreadPoolThread;
303 
304  CriticalSection lock;
305  WaitableEvent jobFinishedSignal;
306 
307  bool runNextJob (ThreadPoolThread&);
308  ThreadPoolJob* pickNextJobToRun();
309  void addToDeleteList (OwnedArray<ThreadPoolJob>&, ThreadPoolJob*) const;
310  void createThreads (int numThreads);
311  void stopThreads();
312 
313  // Note that this method has changed, and no longer has a parameter to indicate
314  // whether the jobs should be deleted - see the new method for details.
315  void removeAllJobs (bool, int, bool);
316 
318 };
319 
320 
321 #endif // JUCE_THREADPOOL_H_INCLUDED
#define noexcept
Definition: juce_CompilerSupport.h:141
bool shouldExit() const noexcept
Definition: juce_ThreadPool.h:113
Definition: juce_ThreadPool.h:181
virtual ~JobSelector()
Definition: juce_ThreadPool.h:184
Definition: juce_String.h:43
bool isRunning() const noexcept
Definition: juce_ThreadPool.h:104
#define JUCE_API
Definition: juce_StandardHeader.h:139
#define const
Definition: juce_CriticalSection.h:47
Definition: juce_ThreadPool.h:149
Definition: juce_WaitableEvent.h:41
Definition: juce_StringArray.h:39
Definition: juce_ContainerDeletePolicy.h:44
Definition: juce_OwnedArray.h:55
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
Definition: juce_PlatformDefs.h:198
Definition: juce_ThreadPool.h:50
Definition: juce_ThreadPool.cpp:29
JobStatus
Definition: juce_ThreadPool.h:76