Mercurial > foo_out_sdl
comparison foosdk/sdk/foobar2000/helpers/fb2k_threads.h @ 1:20d02a178406 default tip
*: check in everything else
yay
| author | Paper <paper@tflc.us> |
|---|---|
| date | Mon, 05 Jan 2026 02:15:46 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 0:e9bb126753e7 | 1:20d02a178406 |
|---|---|
| 1 #pragma once | |
| 2 | |
| 3 inline static t_size GetOptimalWorkerThreadCount() throw() { | |
| 4 return pfc::getOptimalWorkerThreadCount(); | |
| 5 } | |
| 6 | |
| 7 //! IMPORTANT: all classes derived from CVerySimpleThread must call WaitTillThreadDone() in their destructor, to avoid object destruction during a virtual function call! | |
| 8 class CVerySimpleThread : private pfc::thread { | |
| 9 public: | |
| 10 #ifdef _WIN32 | |
| 11 void StartThread(int winPriority) { | |
| 12 StartThread(pfc::thread::argWinPriority(winPriority)); | |
| 13 } | |
| 14 #endif | |
| 15 void StartThread( pfc::thread::arg_t const & arg = pfc::thread::argCurrentThread() ) { | |
| 16 pfc::thread::start( arg ); | |
| 17 } | |
| 18 | |
| 19 bool IsThreadActive() const { | |
| 20 return this->pfc::thread::isActive(); | |
| 21 } | |
| 22 void WaitTillThreadDone() { | |
| 23 this->pfc::thread::waitTillDone(); | |
| 24 } | |
| 25 protected: | |
| 26 CVerySimpleThread() {} | |
| 27 virtual void ThreadProc() = 0; | |
| 28 private: | |
| 29 | |
| 30 void threadProc() { | |
| 31 this->ThreadProc(); | |
| 32 } | |
| 33 | |
| 34 PFC_CLASS_NOT_COPYABLE_EX(CVerySimpleThread) | |
| 35 }; | |
| 36 | |
| 37 //! IMPORTANT: all classes derived from CSimpleThread must call AbortThread()/WaitTillThreadDone() in their destructors, to avoid object destruction during a virtual function call! | |
| 38 class CSimpleThread : private completion_notify_receiver, private pfc::thread { | |
| 39 public: | |
| 40 #ifdef _WIN32 | |
| 41 void StartThread(int winPriority) { | |
| 42 StartThread(pfc::thread::argWinPriority(winPriority)); | |
| 43 } | |
| 44 #endif | |
| 45 void StartThread(pfc::thread::arg_t const & arg = pfc::thread::argCurrentThread() ) { | |
| 46 AbortThread(); | |
| 47 m_abort.reset(); | |
| 48 m_ownNotify = create_task(0); | |
| 49 this->pfc::thread::start( arg ); | |
| 50 } | |
| 51 void AbortThread() { | |
| 52 m_abort.abort(); | |
| 53 CloseThread(); | |
| 54 } | |
| 55 bool IsThreadActive() const { | |
| 56 return this->pfc::thread::isActive(); | |
| 57 } | |
| 58 void WaitTillThreadDone() { | |
| 59 CloseThread(); | |
| 60 } | |
| 61 protected: | |
| 62 CSimpleThread() {} | |
| 63 ~CSimpleThread() {AbortThread();} | |
| 64 | |
| 65 virtual unsigned ThreadProc(abort_callback & p_abort) = 0; | |
| 66 //! Called when the thread has completed normally, with p_code equal to ThreadProc retval. Not called when AbortThread() or WaitTillThreadDone() was used to abort the thread / wait for the thread to finish. | |
| 67 virtual void ThreadDone(unsigned p_code) {}; | |
| 68 private: | |
| 69 void CloseThread() { | |
| 70 this->pfc::thread::waitTillDone(); | |
| 71 orphan_all_tasks(); | |
| 72 } | |
| 73 | |
| 74 void on_task_completion(unsigned p_id,unsigned p_status) { | |
| 75 if (IsThreadActive()) { | |
| 76 CloseThread(); | |
| 77 ThreadDone(p_status); | |
| 78 } | |
| 79 } | |
| 80 void threadProc() { | |
| 81 unsigned code = ~0; | |
| 82 try { | |
| 83 code = ThreadProc(m_abort); | |
| 84 } catch(...) {} | |
| 85 if (!m_abort.is_aborting()) m_ownNotify->on_completion_async(code); | |
| 86 } | |
| 87 abort_callback_impl m_abort; | |
| 88 completion_notify_ptr m_ownNotify; | |
| 89 | |
| 90 PFC_CLASS_NOT_COPYABLE_EX(CSimpleThread); | |
| 91 }; | |
| 92 |
