11namespace tracktion {
inline namespace engine
14class BackgroundJobManager;
23 virtual float getCurrentTaskProgress() = 0;
24 virtual bool canCancel()
const {
return false; }
65 job->setManager (*
this);
66 pool.
addJob (job, takeOwnership);
71 pool.
removeJob (job, interruptIfRunning, timeOutMilliseconds);
74 void stopAndDeleteAllRunningJobs()
88 : name (j.
getJobName()), progress (j.getCurrentTaskProgress()), jobId (
id), canCancel (j.canCancel()) {}
90 JobInfo (
const JobInfo& o) : name (o.name), progress (o.progress), jobId (o.jobId), canCancel (o.canCancel) {}
91 JobInfo& operator= (
const JobInfo& o) { name = o.name; progress = o.progress; jobId = o.jobId; canCancel = o.canCancel;
return *
this; }
96 bool canCancel =
false;
99 void signalJobShouldExit (
const JobInfo& info)
103 for (
int i = jobs.
size(); --i >= 0;)
105 if (pair->info.jobId == info.jobId)
106 pair->job.signalJobShouldExit();
109 JobInfo getJobInfo (
int index)
const noexcept
113 if (
auto j = jobs[index])
120 float getTotalProgress() const noexcept {
return totalProgress; }
128 virtual void backgroundJobsChanged() = 0;
131 void addListener (
Listener* l) { listeners.add (l); }
132 void removeListener (Listener* l) { listeners.remove (l); }
138 : job (j), info (j, jobId) {}
140 ThreadPoolJobWithProgress& job;
145 friend class ThreadPoolJobWithProgress;
150 float totalProgress = 1.0f;
153 void addJobInternal (ThreadPoolJobWithProgress& job)
157 jobs.
add (
new JobInfoPair (job, getNextJobId()));
162 void removeJobInternal (ThreadPoolJobWithProgress& job)
166 for (
int i = jobs.
size(); --i >= 0;)
173 int getNextJobId() noexcept {
return ++nextJobId &= 0xffffff; }
178 if (app->isInitialising())
181 float newTotal = 0.0f;
187 for (
int i = jobs.
size(); --i >= 0;)
191 const float p = pair->job.getCurrentTaskProgress();
193 pair->info.progress = p;
200 newTotal = (numJobs > 0) ? (newTotal / numJobs) : 1.0f;
202 if (newTotal > totalProgress)
203 totalProgress = totalProgress + ((newTotal - totalProgress) / 2.0f);
205 totalProgress = newTotal;
207 jassert (totalProgress == totalProgress
210 if (totalProgress >= 1.0f)
214 void handleAsyncUpdate()
override { listeners.
call (&Listener::backgroundJobsChanged); }
215 void timerCallback()
override { updateJobs(); }
220inline ThreadPoolJobWithProgress::~ThreadPoolJobWithProgress()
224 if (manager !=
nullptr)
225 manager->removeJobInternal (*
this);
228inline void ThreadPoolJobWithProgress::setManager (BackgroundJobManager& m)
231 manager->addJobInternal (*
this);
232 manager->triggerAsyncUpdate();
239 if (manager !=
nullptr)
240 manager->triggerAsyncUpdate();
245 if (manager !=
nullptr)
246 manager->removeJobInternal (*
this);
void triggerAsyncUpdate()
static JUCEApplicationBase * getInstance() noexcept
void call(Callback &&callback)
int size() const noexcept
ObjectClass * getUnchecked(int index) const noexcept
void remove(int indexToRemove, bool deleteObject=true)
ObjectClass * add(ObjectClass *newObject)
void setJobName(const String &newName)
String getJobName() const
ThreadPoolJob(const String &name)
void addJob(ThreadPoolJob *job, bool deleteJobWhenFinished)
int getNumJobs() const noexcept
bool removeAllJobs(bool interruptRunningJobs, int timeOutMilliseconds, JobSelector *selectedJobsToRemove=nullptr)
bool removeJob(ThreadPoolJob *job, bool interruptIfRunning, int timeOutMilliseconds)
void stopTimer() noexcept
void startTimerHz(int timerFrequencyHz) noexcept
Manages a set of background tasks that can be run concurrently on a background thread.
void prepareForJobDeletion()
Call this in your sub-class destructor to to remvoe it from the manager queue before this class's des...
void setName(const juce::String &newName)
Sets the job's name but also updates the manager so the list will reflect it.
bool isPositiveAndNotGreaterThan(Type1 valueToTest, Type2 upperLimit) noexcept