comparison foosdk/sdk/foobar2000/helpers/ThreadUtils.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 "fb2k_threads.h"
3
4 #ifdef _WIN32
5 #include <functional>
6
7 namespace ThreadUtils {
8 bool WaitAbortable(HANDLE ev, abort_callback & abort, DWORD timeout = INFINITE);
9
10 void ProcessPendingMessages();
11 void ProcessPendingMessagesWithDialog(HWND hDialog);
12
13 void WaitAbortable_MsgLoop(HANDLE ev, abort_callback & abort);
14
15
16 // Throws an exception if aborted, returns index of triggered event otherwise.
17 t_size MultiWaitAbortable_MsgLoop2(const HANDLE* ev, t_size evCount, abort_callback& abort);
18
19 // Do not use, broken version of MultiWaitAbortable_MsgLoop2 retained for compatibility (returns 1 based index)
20 t_size MultiWaitAbortable_MsgLoop(const HANDLE* ev, t_size evCount, abort_callback& abort);
21
22 void SleepAbortable_MsgLoop(abort_callback & abort, DWORD timeoutMS);
23 bool WaitAbortable_MsgLoop(HANDLE ev, abort_callback & abort, DWORD timeoutMS);
24
25 DWORD MultiWait_MsgLoop(const HANDLE* ev, DWORD evCount, DWORD timeoutMS);
26 DWORD MultiWait_MsgLoop(const HANDLE* ev, DWORD evCount);
27
28 // Drop-in replacement for pfc::event::g_wait_for()
29 bool pfcWaitMsgLoop(HANDLE ev, double timeout);
30
31 template<typename TWhat>
32 class CObjectQueue {
33 public:
34 CObjectQueue() { m_event.create(true,false); }
35
36 template<typename TSource> void Add(const TSource & source) {
37 insync(m_sync);
38 m_content.add_item(source);
39 if (m_content.get_count() == 1) m_event.set_state(true);
40 }
41 template<typename TDestination> void Get(TDestination & out, abort_callback & abort) {
42 WaitAbortable(m_event.get(), abort);
43 _Get(out);
44 }
45
46 template<typename TDestination> void Get_MsgLoop(TDestination & out, abort_callback & abort) {
47 WaitAbortable_MsgLoop(m_event.get(), abort);
48 _Get(out);
49 }
50
51 private:
52 template<typename TDestination> void _Get(TDestination & out) {
53 insync(m_sync);
54 auto iter = m_content.cfirst();
55 FB2K_DYNAMIC_ASSERT( iter.is_valid() );
56 out = *iter;
57 m_content.remove(iter);
58 if (m_content.get_count() == 0) m_event.set_state(false);
59 }
60 win32_event m_event;
61 critical_section m_sync;
62 pfc::chain_list_v2_t<TWhat> m_content;
63 };
64
65 }
66 #endif // _WIN32