77#include < QCoreApplication>
88#include < QThread>
99#include " Common/Cpp/Containers/Pimpl.tpp"
10+ #include " Common/Cpp/Concurrency/Qt6.9ThreadBugWorkaround.h"
1011#include " Common/Cpp/Concurrency/Thread.h"
1112
1213// #include <iostream>
@@ -22,7 +23,7 @@ namespace PokemonAutomation{
2223struct Thread ::Data : public QThread{
2324 Data (std::function<void ()>&& function)
2425 : m_function(std::move(function))
25- , m_finished(false )
26+ // , m_finished(false)
2627 {
2728 start ();
2829 }
@@ -37,13 +38,34 @@ struct Thread::Data : public QThread{
3738 // What the fuck?!?! This hangs on Qt6.9 even after run() returns!
3839 wait ();
3940
41+ #if 0
42+ #ifndef PA_ENABLE_QT_ADOPTION_WORKAROUND
43+ wait();
44+ #else
45+ while (m_finished.load(std::memory_order_acquire));
46+ if (wait(1000)){
47+ return;
48+ }
49+ std::cout << "QThread did not quit in a timely manner... It is likely hung..." << std::endl;
50+ abort();
51+ #endif
52+ #endif
53+
54+ //
55+ // On further investigation, wait() hangs because the Qt6.9 thread
56+ // adoption bug affects not just std::thread, but its own as well.
57+ //
58+ // When this QThread ends, the handle adopts one of the video backend
59+ // threads which do not join. Thus it hangs here.
60+ //
61+
4062// cout << "finished: " << this << endl;
4163 }
4264
4365 virtual void run () override {
4466// cout << "starting: " << this << endl;
4567 m_function ();
46- m_finished.store (true , std::memory_order_acquire);
68+ // m_finished.store(true, std::memory_order_acquire);
4769// cout << "exiting: " << this << endl;
4870 }
4971
@@ -83,6 +105,14 @@ void Thread::join(){
83105 return ;
84106 }
85107
108+ #ifdef PA_ENABLE_QT_ADOPTION_WORKAROUND
109+ // Exit the thread.
110+ m_data->quit ();
111+
112+ // But instead of waiting and joining (which will hang), we leak it intentionally.
113+ m_data.release ();
114+ #endif
115+
86116 // Clear the Pimpl data, marking this Thread as joined/empty
87117 // After this, operator bool() returns false and join() becomes no-op
88118 m_data.clear ();
0 commit comments