Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Common/Cpp/Concurrency/ComputationThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ void ComputationThreadPool::run_in_parallel(
){
m_core->run_in_parallel(func, start, end, block_size);
}

void ComputationThreadPool::stop() {
// force call stop in computation thread pool
m_core->stop();
}



Expand Down
2 changes: 2 additions & 0 deletions Common/Cpp/Concurrency/ComputationThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class ComputationThreadPool final{

void ensure_threads(size_t threads);

void stop();

// void wait_for_everything();


Expand Down
23 changes: 21 additions & 2 deletions Common/Cpp/Concurrency/ComputationThreadPoolCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,40 @@ ComputationThreadPoolCore::ComputationThreadPoolCore(
spawn_thread();
}
}
ComputationThreadPoolCore::~ComputationThreadPoolCore(){

void ComputationThreadPoolCore::stop() {
{
std::lock_guard<std::mutex> lg(m_lock);
if (m_stopping) return;
m_stopping = true;
m_thread_cv.notify_all();
// m_dispatch_cv.notify_all();
}
for (ThreadData& thread : m_threads){
thread.thread.join();
if (thread.thread.joinable()) {
thread.thread.join();
}
}

// DO NOT JOIN AGAIN IN DESTRUCTOR
m_threads.clear();

for (auto& task : m_queue){
task->report_cancelled();
}

// DO NOT CLEAR AGAIN IN DESTRUCTOR
m_queue.clear();

}

ComputationThreadPoolCore::~ComputationThreadPoolCore(){
stop();
}




WallDuration ComputationThreadPoolCore::cpu_time() const{
// TODO: Don't lock the entire queue.
WallDuration ret = WallDuration::zero();
Expand Down
2 changes: 2 additions & 0 deletions Common/Cpp/Concurrency/ComputationThreadPoolCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class ComputationThreadPoolCore final{
WallDuration cpu_time() const;

void ensure_threads(size_t threads);

void stop();
// void wait_for_everything();


Expand Down
10 changes: 9 additions & 1 deletion Common/Cpp/Concurrency/FireForgetDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ FireForgetDispatcher global_dispatcher;
FireForgetDispatcher::FireForgetDispatcher()
: m_stopping(false)
{}
FireForgetDispatcher::~FireForgetDispatcher(){

void FireForgetDispatcher::stop() {
{
std::lock_guard<std::mutex> lg(m_lock);
// like with the other thread option, make sure we're not double stopping
if (m_stopping) return;
m_stopping = true;
m_cv.notify_all();
}
m_thread.join();
}

FireForgetDispatcher::~FireForgetDispatcher(){
stop();
}

void FireForgetDispatcher::dispatch(std::function<void()>&& func){
std::lock_guard<std::mutex> lg(m_lock);
m_queue.emplace_back(std::move(func));
Expand Down
2 changes: 2 additions & 0 deletions Common/Cpp/Concurrency/FireForgetDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class FireForgetDispatcher{
// Call "handle->wait()" to wait for the task to finish.
void dispatch(std::function<void()>&& func);

void stop();


private:
void thread_loop();
Expand Down
4 changes: 4 additions & 0 deletions Common/Cpp/Concurrency/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ void Thread::join(){
}


bool Thread::joinable() const{
return m_data && m_data->m_thread.joinable();
}



}
1 change: 1 addition & 0 deletions Common/Cpp/Concurrency/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Thread{
return m_data;
}
void join();
bool joinable() const;

private:
enum class State;
Expand Down
8 changes: 8 additions & 0 deletions SerialPrograms/Source/CommonFramework/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
//#include <QTextStream>
#include <QMessageBox>
#include "Common/Cpp/Concurrency/AsyncTask.h"
#include "Common/Cpp/Concurrency/FireForgetDispatcher.h"
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/ImageResolution.h"
#include "CommonFramework/Tools/GlobalThreadPools.h"
#include "Globals.h"
#include "GlobalSettingsPanel.h"
#include "PersistentSettings.h"
Expand Down Expand Up @@ -149,6 +151,12 @@ int main(int argc, char *argv[]){
Integration::DppClient::Client::instance().disconnect();
#endif

// Force stop the thread pool
PokemonAutomation::GlobalThreadPools::realtime_inference().stop();
PokemonAutomation::GlobalThreadPools::normal_inference().stop();

PokemonAutomation::global_dispatcher.stop();

// We must clear the OCR cache or it will crash on Linux when the library
// unloads before the cache is destructed from static memory.
OCR::clear_cache();
Expand Down