LMMS
Loading...
Searching...
No Matches
juce_ThreadPool.h
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 ThreadPool;
27
28//==============================================================================
45{
46public:
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 //==============================================================================
78
93 virtual JobStatus runJob() = 0;
94
95
96 //==============================================================================
98 bool isRunning() const noexcept { return isActive; }
99
108
114 void signalJobShouldExit();
115
121 void addListener (Thread::Listener*);
122
124 void removeListener (Thread::Listener*);
125
126 //==============================================================================
130 static ThreadPoolJob* getCurrentThreadPoolJob();
131
132 //==============================================================================
133private:
134 friend class ThreadPool;
136 ThreadPool* pool = nullptr;
137 std::atomic<bool> shouldStop { false }, isActive { false }, shouldBeDeleted { false };
139
141};
142
143
144//==============================================================================
156{
157public:
158 //==============================================================================
168 ThreadPool (int numberOfThreads, size_t threadStackSize = 0);
169
176 ThreadPool();
177
184 ~ThreadPool();
185
186 //==============================================================================
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
271
274
280 ThreadPoolJob* getJob (int index) const noexcept;
281
287
290
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
319private:
320 //==============================================================================
322
323 struct ThreadPoolThread;
324 friend class ThreadPoolJob;
326
329
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
341};
342
343} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
Definition juce_Array.h:56
Definition juce_CriticalSection.h:43
Definition juce_ListenerList.h:70
Definition juce_OwnedArray.h:51
Definition juce_StringArray.h:35
Definition juce_String.h:53
Definition juce_Thread.h:181
Definition juce_ThreadPool.h:192
virtual bool isJobSuitable(ThreadPoolJob *job)=0
virtual ~JobSelector()=default
void moveJobToFront(const ThreadPoolJob *jobToMove) noexcept
Definition juce_ThreadPool.cpp:206
void createThreads(int numThreads, size_t threadStackSize=0)
Definition juce_ThreadPool.cpp:111
void addToDeleteList(OwnedArray< ThreadPoolJob > &, ThreadPoolJob *) const
Definition juce_ThreadPool.cpp:423
void addJob(ThreadPoolJob *job, bool deleteJobWhenFinished)
Definition juce_ThreadPool.cpp:129
OwnedArray< ThreadPoolThread > threads
Definition juce_ThreadPool.h:325
friend class ThreadPoolJob
Definition juce_ThreadPool.h:324
ThreadPoolJob * pickNextJobToRun()
Definition juce_ThreadPool.cpp:344
void stopThreads()
Definition juce_ThreadPool.cpp:120
CriticalSection lock
Definition juce_ThreadPool.h:327
int getNumThreads() const noexcept
Definition juce_ThreadPool.cpp:183
ThreadPoolJob * getJob(int index) const noexcept
Definition juce_ThreadPool.cpp:188
int getNumJobs() const noexcept
Definition juce_ThreadPool.cpp:177
bool runNextJob(ThreadPoolThread &)
Definition juce_ThreadPool.cpp:375
bool removeAllJobs(bool interruptRunningJobs, int timeOutMilliseconds, JobSelector *selectedJobsToRemove=nullptr)
Definition juce_ThreadPool.cpp:263
WaitableEvent jobFinishedSignal
Definition juce_ThreadPool.h:328
ThreadPool(int numberOfThreads, size_t threadStackSize=0)
Definition juce_ThreadPool.cpp:93
StringArray getNamesOfAllJobs(bool onlyReturnActiveJobs) const
Definition juce_ThreadPool.cpp:321
bool isJobRunning(const ThreadPoolJob *job) const noexcept
Definition juce_ThreadPool.cpp:200
bool setThreadPriorities(int newPriority)
Definition juce_ThreadPool.cpp:333
bool removeJob(ThreadPoolJob *job, bool interruptIfRunning, int timeOutMilliseconds)
Definition juce_ThreadPool.cpp:234
bool contains(const ThreadPoolJob *job) const noexcept
Definition juce_ThreadPool.cpp:194
bool waitForJobToFinish(const ThreadPoolJob *job, int timeOutMilliseconds) const
Definition juce_ThreadPool.cpp:216
Array< ThreadPoolJob * > jobs
Definition juce_ThreadPool.h:321
Definition juce_ThreadPool.h:45
ListenerList< Thread::Listener, Array< Thread::Listener *, CriticalSection > > listeners
Definition juce_ThreadPool.h:138
std::atomic< bool > isActive
Definition juce_ThreadPool.h:137
std::atomic< bool > shouldStop
Definition juce_ThreadPool.h:137
JobStatus
Definition juce_ThreadPool.h:71
@ jobHasFinished
Definition juce_ThreadPool.h:72
@ jobNeedsRunningAgain
Definition juce_ThreadPool.h:75
friend class ThreadPool
Definition juce_ThreadPool.h:134
ThreadPool * pool
Definition juce_ThreadPool.h:136
void setJobName(const String &newName)
Definition juce_ThreadPool.cpp:63
String getJobName() const
Definition juce_ThreadPool.cpp:58
bool isRunning() const noexcept
Definition juce_ThreadPool.h:98
bool shouldExit() const noexcept
Definition juce_ThreadPool.h:107
virtual JobStatus runJob()=0
String jobName
Definition juce_ThreadPool.h:135
ThreadPoolJob(const String &name)
Definition juce_ThreadPool.cpp:47
Definition juce_WaitableEvent.h:36
static const char * name
Definition pugl.h:1582
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
#define JUCE_API
Definition juce_StandardHeader.h:152
Definition carla_juce.cpp:31
Definition juce_ThreadPool.cpp:27
#define const
Definition zconf.h:137