From 4e69133e54bab2a544d7924d474bfb747aba794d Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 23 Aug 2017 12:07:11 +0100 Subject: [PATCH] Added method ThreadPool::moveJobToFront() --- modules/juce_core/threads/juce_ThreadPool.cpp | 25 ++++++++++++++----- modules/juce_core/threads/juce_ThreadPool.h | 15 +++++++---- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/modules/juce_core/threads/juce_ThreadPool.cpp b/modules/juce_core/threads/juce_ThreadPool.cpp index 68e498e6c5..5e67b2f51e 100644 --- a/modules/juce_core/threads/juce_ThreadPool.cpp +++ b/modules/juce_core/threads/juce_ThreadPool.cpp @@ -161,34 +161,47 @@ void ThreadPool::addJob (std::function jobToRun) addJob (new LambdaJobWrapper (jobToRun), true); } -int ThreadPool::getNumJobs() const +int ThreadPool::getNumJobs() const noexcept { return jobs.size(); } -int ThreadPool::getNumThreads() const +int ThreadPool::getNumThreads() const noexcept { return threads.size(); } -ThreadPoolJob* ThreadPool::getJob (const int index) const +ThreadPoolJob* ThreadPool::getJob (int index) const noexcept { const ScopedLock sl (lock); return jobs [index]; } -bool ThreadPool::contains (const ThreadPoolJob* const job) const +bool ThreadPool::contains (const ThreadPoolJob* const job) const noexcept { const ScopedLock sl (lock); return jobs.contains (const_cast (job)); } -bool ThreadPool::isJobRunning (const ThreadPoolJob* const job) const +bool ThreadPool::isJobRunning (const ThreadPoolJob* const job) const noexcept { const ScopedLock sl (lock); return jobs.contains (const_cast (job)) && job->isActive; } +void ThreadPool::moveJobToFront (const ThreadPoolJob* job) noexcept +{ + const ScopedLock sl (lock); + + if (! ! job->isActive) + { + auto index = jobs.indexOf (const_cast (job)); + + if (index > 0) + jobs.move (index, 0); + } +} + bool ThreadPool::waitForJobToFinish (const ThreadPoolJob* const job, const int timeOutMs) const { if (job != nullptr) @@ -272,7 +285,7 @@ bool ThreadPool::removeAllJobs (const bool interruptRunningJobs, const int timeO } } - const uint32 start = Time::getMillisecondCounter(); + auto start = Time::getMillisecondCounter(); for (;;) { diff --git a/modules/juce_core/threads/juce_ThreadPool.h b/modules/juce_core/threads/juce_ThreadPool.h index cc3f059a46..6dda3ed939 100644 --- a/modules/juce_core/threads/juce_ThreadPool.h +++ b/modules/juce_core/threads/juce_ThreadPool.h @@ -254,26 +254,26 @@ public: JobSelector* selectedJobsToRemove = nullptr); /** Returns the number of jobs currently running or queued. */ - int getNumJobs() const; + int getNumJobs() const noexcept; /** Returns the number of threads assigned to this thread pool. */ - int getNumThreads() const; + int getNumThreads() const noexcept; /** Returns one of the jobs in the queue. Note that this can be a very volatile list as jobs might be continuously getting shifted around in the list, and this method may return nullptr if the index is currently out-of-range. */ - ThreadPoolJob* getJob (int index) const; + ThreadPoolJob* getJob (int index) const noexcept; /** Returns true if the given job is currently queued or running. @see isJobRunning() */ - bool contains (const ThreadPoolJob* job) const; + bool contains (const ThreadPoolJob* job) const noexcept; /** Returns true if the given job is currently being run by a thread. */ - bool isJobRunning (const ThreadPoolJob* job) const; + bool isJobRunning (const ThreadPoolJob* job) const noexcept; /** Waits until a job has finished running and has been removed from the pool. @@ -286,6 +286,11 @@ public: bool waitForJobToFinish (const ThreadPoolJob* job, int timeOutMilliseconds) const; + /** If the given job is in the queue, this will move it to the front so that it + is the next one to be executed. + */ + void moveJobToFront (const ThreadPoolJob* jobToMove) noexcept; + /** Returns a list of the names of all the jobs currently running or queued. If onlyReturnActiveJobs is true, only the ones currently running are returned. */