Mercurial > foo_out_sdl
comparison foosdk/sdk/foobar2000/helpers/fb2kWorkerTool.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 #include <functional> | |
| 3 #include <list> | |
| 4 | |
| 5 #include <SDK/threadPool.h> | |
| 6 | |
| 7 namespace fb2k { | |
| 8 class workerTool { | |
| 9 public: | |
| 10 struct work_t { | |
| 11 std::function<void()> work, done; | |
| 12 }; | |
| 13 | |
| 14 void operator+=(work_t && work) { | |
| 15 m_workQueue.push_back( std::make_shared< work_t> ( std::move(work) ) ); | |
| 16 kickWork(); | |
| 17 } | |
| 18 void flush() { | |
| 19 if ( m_working ) { | |
| 20 m_abort->set(); | |
| 21 m_abort = std::make_shared<abort_callback_impl>(); | |
| 22 m_working = false; | |
| 23 } | |
| 24 m_workQueue.clear(); | |
| 25 } | |
| 26 | |
| 27 void workDone() { | |
| 28 m_working = false; | |
| 29 kickWork(); | |
| 30 } | |
| 31 void kickWork() { | |
| 32 PFC_ASSERT( core_api::is_main_thread() ); | |
| 33 if (!m_working && !m_workQueue.empty()) { | |
| 34 m_working = true; | |
| 35 auto iter = m_workQueue.begin(); | |
| 36 auto work = std::move(*iter); m_workQueue.erase(iter); | |
| 37 auto pThis = this; | |
| 38 auto a = m_abort; | |
| 39 fb2k::inCpuWorkerThread( [ work, pThis, a] { | |
| 40 try { | |
| 41 // release lambdas early, synchronously from same context as they're executed | |
| 42 { auto f = std::move(work->work); if (f) f(); } | |
| 43 a->check(); | |
| 44 fb2k::inMainThread( [work, pThis, a] { | |
| 45 if ( ! a->is_set() ) { | |
| 46 // release lambdas early, synchronously from same context as they're executed | |
| 47 { auto f = std::move(work->done); if (f) f(); } | |
| 48 pThis->workDone(); | |
| 49 } | |
| 50 }); | |
| 51 } catch(exception_aborted const &) {} | |
| 52 } ); | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 workerTool() {} | |
| 57 ~workerTool() { m_abort->set(); } | |
| 58 workerTool(const workerTool&) = delete; | |
| 59 void operator=(const workerTool&) = delete; | |
| 60 | |
| 61 std::shared_ptr<abort_callback_impl> aborter() const { return m_abort; } | |
| 62 private: | |
| 63 std::shared_ptr<abort_callback_impl> m_abort = std::make_shared<abort_callback_impl>(); | |
| 64 | |
| 65 bool m_working = false; | |
| 66 std::list< std::shared_ptr<work_t> > m_workQueue; | |
| 67 }; | |
| 68 | |
| 69 } |
