OpenShot Audio Library | OpenShotAudio  0.3.2
juce_ThreadPool.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 class ThreadPool;
27 
28 //==============================================================================
44 class JUCE_API ThreadPoolJob
45 {
46 public:
47  //==============================================================================
51  explicit ThreadPoolJob (const String& name);
52 
54  virtual ~ThreadPoolJob();
55 
56  //==============================================================================
60  String getJobName() const;
61 
65  void setJobName (const String& newName);
66 
67  //==============================================================================
70  enum JobStatus
71  {
72  jobHasFinished = 0,
75  jobNeedsRunningAgain
77  };
78 
93  virtual JobStatus runJob() = 0;
94 
95 
96  //==============================================================================
98  bool isRunning() const noexcept { return isActive; }
99 
107  bool shouldExit() const noexcept { return shouldStop; }
108 
114  void signalJobShouldExit();
115 
121  void addListener (Thread::Listener*);
122 
124  void removeListener (Thread::Listener*);
125 
126  //==============================================================================
130  static ThreadPoolJob* getCurrentThreadPoolJob();
131 
132  //==============================================================================
133 private:
134  friend class ThreadPool;
135  String jobName;
136  ThreadPool* pool = nullptr;
137  std::atomic<bool> shouldStop { false }, isActive { false }, shouldBeDeleted { false };
138  ListenerList<Thread::Listener, Array<Thread::Listener*, CriticalSection>> listeners;
139 
140  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ThreadPoolJob)
141 };
142 
143 
144 //==============================================================================
155 class JUCE_API ThreadPool
156 {
157 public:
158  //==============================================================================
168  ThreadPool (int numberOfThreads, size_t threadStackSize = 0);
169 
176  ThreadPool();
177 
184  ~ThreadPool();
185 
186  //==============================================================================
191  class JUCE_API JobSelector
192  {
193  public:
194  virtual ~JobSelector() = default;
195 
201  virtual bool isJobSuitable (ThreadPoolJob* job) = 0;
202  };
203 
204  //==============================================================================
220  void addJob (ThreadPoolJob* job,
221  bool deleteJobWhenFinished);
222 
226  void addJob (std::function<ThreadPoolJob::JobStatus()> job);
227 
231  void addJob (std::function<void()> job);
232 
250  bool removeJob (ThreadPoolJob* job,
251  bool interruptIfRunning,
252  int timeOutMilliseconds);
253 
265  bool removeAllJobs (bool interruptRunningJobs,
266  int timeOutMilliseconds,
267  JobSelector* selectedJobsToRemove = nullptr);
268 
270  int getNumJobs() const noexcept;
271 
273  int getNumThreads() const noexcept;
274 
280  ThreadPoolJob* getJob (int index) const noexcept;
281 
286  bool contains (const ThreadPoolJob* job) const noexcept;
287 
289  bool isJobRunning (const ThreadPoolJob* job) const noexcept;
290 
299  bool waitForJobToFinish (const ThreadPoolJob* job,
300  int timeOutMilliseconds) const;
301 
305  void moveJobToFront (const ThreadPoolJob* jobToMove) noexcept;
306 
310  StringArray getNamesOfAllJobs (bool onlyReturnActiveJobs) const;
311 
316  bool setThreadPriorities (int newPriority);
317 
318 
319 private:
320  //==============================================================================
321  Array<ThreadPoolJob*> jobs;
322 
323  struct ThreadPoolThread;
324  friend class ThreadPoolJob;
325  OwnedArray<ThreadPoolThread> threads;
326 
327  CriticalSection lock;
328  WaitableEvent jobFinishedSignal;
329 
330  bool runNextJob (ThreadPoolThread&);
331  ThreadPoolJob* pickNextJobToRun();
332  void addToDeleteList (OwnedArray<ThreadPoolJob>&, ThreadPoolJob*) const;
333  void createThreads (int numThreads, size_t threadStackSize = 0);
334  void stopThreads();
335 
336  // Note that this method has changed, and no longer has a parameter to indicate
337  // whether the jobs should be deleted - see the new method for details.
338  void removeAllJobs (bool, int, bool);
339 
340  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ThreadPool)
341 };
342 
343 } // namespace juce
bool isRunning() const noexcept
bool shouldExit() const noexcept
virtual JobStatus runJob()=0
virtual bool isJobSuitable(ThreadPoolJob *job)=0