Loading...
Searching...
No Matches
juce_ThreadPool.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
class ThreadPool;
//==============================================================================
class JUCE_API ThreadPoolJob
{
public:
//==============================================================================
explicit ThreadPoolJob (const String& name);
virtual ~ThreadPoolJob();
//==============================================================================
String getJobName() const;
void setJobName (const String& newName);
//==============================================================================
enum JobStatus
{
jobHasFinished = 0,
jobNeedsRunningAgain
};
virtual JobStatus runJob() = 0;
//==============================================================================
bool isRunning() const noexcept { return isActive; }
bool shouldExit() const noexcept { return shouldStop; }
void signalJobShouldExit();
void addListener (Thread::Listener*);
void removeListener (Thread::Listener*);
//==============================================================================
static ThreadPoolJob* getCurrentThreadPoolJob();
//==============================================================================
private:
friend class ThreadPool;
String jobName;
ThreadPool* pool = nullptr;
std::atomic<bool> shouldStop { false }, isActive { false }, shouldBeDeleted { false };
ListenerList<Thread::Listener, Array<Thread::Listener*, CriticalSection>> listeners;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ThreadPoolJob)
};
//==============================================================================
struct ThreadPoolOptions
{
[[nodiscard]] ThreadPoolOptions withThreadName (String newThreadName) const
{
return withMember (*this, &ThreadPoolOptions::threadName, newThreadName);
}
[[nodiscard]] ThreadPoolOptions withNumberOfThreads (int newNumberOfThreads) const
{
return withMember (*this, &ThreadPoolOptions::numberOfThreads, newNumberOfThreads);
}
[[nodiscard]] ThreadPoolOptions withThreadStackSizeBytes (size_t newThreadStackSizeBytes) const
{
return withMember (*this, &ThreadPoolOptions::threadStackSizeBytes, newThreadStackSizeBytes);
}
[[nodiscard]] ThreadPoolOptions withDesiredThreadPriority (Thread::Priority newDesiredThreadPriority) const
{
return withMember (*this, &ThreadPoolOptions::desiredThreadPriority, newDesiredThreadPriority);
}
String threadName { "Pool" };
int numberOfThreads { SystemStats::getNumCpus() };
size_t threadStackSizeBytes { Thread::osDefaultStackSize };
Thread::Priority desiredThreadPriority { Thread::Priority::normal };
};
//==============================================================================
class JUCE_API ThreadPool
{
public:
using Options = ThreadPoolOptions;
//==============================================================================
explicit ThreadPool (const Options& options);
ThreadPool() : ThreadPool { Options{} } {}
ThreadPool (int numberOfThreads,
size_t threadStackSizeBytes = Thread::osDefaultStackSize,
Thread::Priority desiredThreadPriority = Thread::Priority::normal);
~ThreadPool();
//==============================================================================
class JUCE_API JobSelector
{
public:
virtual ~JobSelector() = default;
virtual bool isJobSuitable (ThreadPoolJob* job) = 0;
};
//==============================================================================
void addJob (ThreadPoolJob* job,
bool deleteJobWhenFinished);
void addJob (std::function<ThreadPoolJob::JobStatus()> job);
void addJob (std::function<void()> job);
bool removeJob (ThreadPoolJob* job,
bool interruptIfRunning,
int timeOutMilliseconds);
bool removeAllJobs (bool interruptRunningJobs,
int timeOutMilliseconds,
JobSelector* selectedJobsToRemove = nullptr);
int getNumJobs() const noexcept;
int getNumThreads() const noexcept;
ThreadPoolJob* getJob (int index) const noexcept;
bool contains (const ThreadPoolJob* job) const noexcept;
bool isJobRunning (const ThreadPoolJob* job) const noexcept;
bool waitForJobToFinish (const ThreadPoolJob* job,
int timeOutMilliseconds) const;
void moveJobToFront (const ThreadPoolJob* jobToMove) noexcept;
StringArray getNamesOfAllJobs (bool onlyReturnActiveJobs) const;
private:
//==============================================================================
Array<ThreadPoolJob*> jobs;
struct ThreadPoolThread;
friend class ThreadPoolJob;
OwnedArray<ThreadPoolThread> threads;
CriticalSection lock;
WaitableEvent jobFinishedSignal;
bool runNextJob (ThreadPoolThread&);
ThreadPoolJob* pickNextJobToRun();
void addToDeleteList (OwnedArray<ThreadPoolJob>&, ThreadPoolJob*) const;
void stopThreads();
// Note that this method has changed, and no longer has a parameter to indicate
// whether the jobs should be deleted - see the new method for details.
void removeAllJobs (bool, int, bool);
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ThreadPool)
};
} // namespace juce
juce::Array
Definition juce_Array.h:56
juce::CriticalSection
Definition juce_CriticalSection.h:43
juce::OwnedArray
Definition juce_OwnedArray.h:50
juce::StringArray
Definition juce_StringArray.h:35
juce::String
Definition juce_String.h:53
juce::SystemStats::getNumCpus
static int getNumCpus() noexcept
Definition juce_SystemStats.cpp:149
juce::ThreadPoolJob
Definition juce_ThreadPool.h:45
juce::ThreadPoolJob::JobStatus
JobStatus
Definition juce_ThreadPool.h:71
juce::ThreadPoolJob::jobHasFinished
@ jobHasFinished
Definition juce_ThreadPool.h:72
juce::ThreadPoolJob::jobNeedsRunningAgain
@ jobNeedsRunningAgain
Definition juce_ThreadPool.h:75
juce::ThreadPoolJob::setJobName
void setJobName(const String &newName)
Definition juce_ThreadPool.cpp:67
juce::ThreadPoolJob::getJobName
String getJobName() const
Definition juce_ThreadPool.cpp:62
juce::ThreadPoolJob::isRunning
bool isRunning() const noexcept
Definition juce_ThreadPool.h:98
juce::ThreadPoolJob::shouldExit
bool shouldExit() const noexcept
Definition juce_ThreadPool.h:107
juce::ThreadPoolJob::runJob
virtual JobStatus runJob()=0
juce::ThreadPoolJob::ThreadPoolJob
ThreadPoolJob(const String &name)
Definition juce_ThreadPool.cpp:51
juce::ThreadPool::JobSelector
Definition juce_ThreadPool.h:250
juce::ThreadPool::JobSelector::isJobSuitable
virtual bool isJobSuitable(ThreadPoolJob *job)=0
juce::ThreadPool
Definition juce_ThreadPool.h:201
juce::ThreadPool::moveJobToFront
void moveJobToFront(const ThreadPoolJob *jobToMove) noexcept
Definition juce_ThreadPool.cpp:210
juce::ThreadPool::addJob
void addJob(ThreadPoolJob *job, bool deleteJobWhenFinished)
Definition juce_ThreadPool.cpp:133
juce::ThreadPool::getNumThreads
int getNumThreads() const noexcept
Definition juce_ThreadPool.cpp:187
juce::ThreadPool::getJob
ThreadPoolJob * getJob(int index) const noexcept
Definition juce_ThreadPool.cpp:192
juce::ThreadPool::getNumJobs
int getNumJobs() const noexcept
Definition juce_ThreadPool.cpp:181
juce::ThreadPool::removeAllJobs
bool removeAllJobs(bool interruptRunningJobs, int timeOutMilliseconds, JobSelector *selectedJobsToRemove=nullptr)
Definition juce_ThreadPool.cpp:267
juce::ThreadPool::getNamesOfAllJobs
StringArray getNamesOfAllJobs(bool onlyReturnActiveJobs) const
Definition juce_ThreadPool.cpp:325
juce::ThreadPool::ThreadPool
ThreadPool(const Options &options)
Definition juce_ThreadPool.cpp:97
juce::ThreadPool::isJobRunning
bool isJobRunning(const ThreadPoolJob *job) const noexcept
Definition juce_ThreadPool.cpp:204
juce::ThreadPool::removeJob
bool removeJob(ThreadPoolJob *job, bool interruptIfRunning, int timeOutMilliseconds)
Definition juce_ThreadPool.cpp:238
juce::ThreadPool::contains
bool contains(const ThreadPoolJob *job) const noexcept
Definition juce_ThreadPool.cpp:198
juce::ThreadPool::waitForJobToFinish
bool waitForJobToFinish(const ThreadPoolJob *job, int timeOutMilliseconds) const
Definition juce_ThreadPool.cpp:220
juce::ThreadPool::ThreadPool
ThreadPool()
Definition juce_ThreadPool.h:220
juce::Thread::Listener
Definition juce_Thread.h:396
juce::Thread::Priority
Priority
Definition juce_Thread.h:54
juce::Thread::Priority::normal
@ normal
Definition juce_Thread.h:62
juce::WaitableEvent
Definition juce_WaitableEvent.h:36
juce::ThreadPoolOptions
Definition juce_ThreadPool.h:155
juce::ThreadPoolOptions::withThreadName
ThreadPoolOptions withThreadName(String newThreadName) const
Definition juce_ThreadPool.h:157
juce::ThreadPoolOptions::withNumberOfThreads
ThreadPoolOptions withNumberOfThreads(int newNumberOfThreads) const
Definition juce_ThreadPool.h:165
juce::ThreadPoolOptions::withDesiredThreadPriority
ThreadPoolOptions withDesiredThreadPriority(Thread::Priority newDesiredThreadPriority) const
Definition juce_ThreadPool.h:177
juce::ThreadPoolOptions::withThreadStackSizeBytes
ThreadPoolOptions withThreadStackSizeBytes(size_t newThreadStackSizeBytes) const
Definition juce_ThreadPool.h:171