|
1
|
1 #include "StdAfx.h"
|
|
|
2
|
|
|
3 #include "ThreadUtils.h"
|
|
|
4 #include "rethrow.h"
|
|
|
5
|
|
|
6 #include <exception>
|
|
|
7
|
|
|
8 namespace ThreadUtils {
|
|
|
9 bool CRethrow::exec( std::function<void () > f ) throw() {
|
|
|
10 m_exception = nullptr;
|
|
|
11 bool rv = false;
|
|
|
12 try {
|
|
|
13 f();
|
|
|
14 rv = true;
|
|
|
15 } catch( ... ) {
|
|
|
16 m_exception = std::current_exception();
|
|
|
17 }
|
|
|
18
|
|
|
19 return rv;
|
|
|
20 }
|
|
|
21
|
|
|
22 void CRethrow::rethrow() const {
|
|
|
23 if (m_exception) std::rethrow_exception(m_exception);
|
|
|
24 }
|
|
|
25 }
|
|
|
26 #ifdef _WIN32
|
|
|
27
|
|
|
28 #include "win32_misc.h"
|
|
|
29
|
|
|
30 #ifdef FOOBAR2000_MOBILE
|
|
|
31 #include <pfc/pp-winapi.h>
|
|
|
32 #endif
|
|
|
33
|
|
|
34
|
|
|
35 namespace ThreadUtils {
|
|
|
36 bool WaitAbortable(HANDLE ev, abort_callback & abort, DWORD timeout) {
|
|
|
37 const HANDLE handles[2] = {ev, abort.get_abort_event()};
|
|
|
38 SetLastError(0);
|
|
|
39 const DWORD status = WaitForMultipleObjects(2, handles, FALSE, timeout);
|
|
|
40 switch(status) {
|
|
|
41 case WAIT_TIMEOUT:
|
|
|
42 PFC_ASSERT( timeout != INFINITE );
|
|
|
43 return false;
|
|
|
44 case WAIT_OBJECT_0:
|
|
|
45 return true;
|
|
|
46 case WAIT_OBJECT_0 + 1:
|
|
|
47 throw exception_aborted();
|
|
|
48 case WAIT_FAILED:
|
|
|
49 WIN32_OP_FAIL();
|
|
|
50 default:
|
|
|
51 uBugCheck();
|
|
|
52 }
|
|
|
53 }
|
|
|
54 #ifdef FOOBAR2000_DESKTOP_WINDOWS
|
|
|
55 void ProcessPendingMessagesWithDialog(HWND hDialog) {
|
|
|
56 MSG msg = {};
|
|
|
57 while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
|
|
58 if (!IsDialogMessage(hDialog, &msg)) {
|
|
|
59 DispatchMessage(&msg);
|
|
|
60 }
|
|
|
61 }
|
|
|
62 }
|
|
|
63 void ProcessPendingMessages() {
|
|
|
64 MSG msg = {};
|
|
|
65 while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
|
|
66 DispatchMessage(&msg);
|
|
|
67 }
|
|
|
68 }
|
|
|
69 void WaitAbortable_MsgLoop(HANDLE ev, abort_callback & abort) {
|
|
|
70 abort.check();
|
|
|
71 const HANDLE handles[2] = {ev, abort.get_abort_event()};
|
|
|
72 MultiWait_MsgLoop(handles, 2);
|
|
|
73 abort.check();
|
|
|
74
|
|
|
75 }
|
|
|
76 t_size MultiWaitAbortable_MsgLoop2(const HANDLE* ev, t_size evCount, abort_callback& abort) {
|
|
|
77 abort.check();
|
|
|
78 const size_t evCountEx = evCount + 1;
|
|
|
79 HANDLE handles1[16];
|
|
|
80 pfc::array_staticsize_t<HANDLE> handles2;
|
|
|
81 HANDLE* pHandles = handles1;
|
|
|
82 if (evCountEx > 16) {
|
|
|
83 handles2.set_size_discard(evCountEx);
|
|
|
84 pHandles = handles2.get_ptr();
|
|
|
85 }
|
|
|
86 pHandles[0] = abort.get_abort_event();
|
|
|
87 pfc::memcpy_t(pHandles + 1, ev, evCount);
|
|
|
88 DWORD status = MultiWait_MsgLoop(pHandles, (DWORD) evCountEx);
|
|
|
89 abort.check();
|
|
|
90 size_t ret = (size_t)(status - WAIT_OBJECT_0 - 1);
|
|
|
91 PFC_ASSERT(ret < evCount);
|
|
|
92 return ret;
|
|
|
93 }
|
|
|
94
|
|
|
95 t_size MultiWaitAbortable_MsgLoop(const HANDLE * ev, t_size evCount, abort_callback & abort) {
|
|
|
96 // Retval is 1-based!
|
|
|
97 // Originally a bug, now kept for compatibility if someone relies on this
|
|
|
98 // Use MultiWaitAbortable_MsgLoop2() instead
|
|
|
99 return MultiWaitAbortable_MsgLoop2(ev, evCount, abort) + 1;
|
|
|
100 }
|
|
|
101
|
|
|
102 void SleepAbortable_MsgLoop(abort_callback & abort, DWORD timeout) {
|
|
|
103 HANDLE handles[] = { abort.get_abort_event() };
|
|
|
104 MultiWait_MsgLoop(handles, 1, timeout);
|
|
|
105 abort.check();
|
|
|
106 }
|
|
|
107
|
|
|
108 bool WaitAbortable_MsgLoop(HANDLE ev, abort_callback & abort, DWORD timeout) {
|
|
|
109 abort.check();
|
|
|
110 HANDLE handles[2] = { abort.get_abort_event(), ev };
|
|
|
111 DWORD status = MultiWait_MsgLoop(handles, 2, timeout);
|
|
|
112 abort.check();
|
|
|
113 return status != WAIT_TIMEOUT;
|
|
|
114 }
|
|
|
115
|
|
|
116 DWORD MultiWait_MsgLoop(const HANDLE* ev, DWORD evCount) {
|
|
|
117 for (;; ) {
|
|
|
118 SetLastError(0);
|
|
|
119 const DWORD status = MsgWaitForMultipleObjects((DWORD) evCount, ev, FALSE, INFINITE, QS_ALLINPUT);
|
|
|
120 if (status == WAIT_FAILED) WIN32_OP_FAIL();
|
|
|
121 if (status == WAIT_OBJECT_0 + evCount) {
|
|
|
122 ProcessPendingMessages();
|
|
|
123 } else if ( status >= WAIT_OBJECT_0 && status < WAIT_OBJECT_0 + evCount ) {
|
|
|
124 return status;
|
|
|
125 } else {
|
|
|
126 uBugCheck();
|
|
|
127 }
|
|
|
128 }
|
|
|
129 }
|
|
|
130
|
|
|
131 DWORD MultiWait_MsgLoop(const HANDLE* ev, DWORD evCount, DWORD timeout) {
|
|
|
132 if (timeout == INFINITE) return MultiWait_MsgLoop(ev, evCount);
|
|
|
133 const DWORD entry = GetTickCount();
|
|
|
134 DWORD now = entry;
|
|
|
135 for (;;) {
|
|
|
136 const DWORD done = now - entry;
|
|
|
137 if (done >= timeout) return WAIT_TIMEOUT;
|
|
|
138 SetLastError(0);
|
|
|
139 const DWORD status = MsgWaitForMultipleObjects((DWORD)evCount, ev, FALSE, timeout - done, QS_ALLINPUT);
|
|
|
140 if (status == WAIT_FAILED) WIN32_OP_FAIL();
|
|
|
141 if (status == WAIT_OBJECT_0 + evCount) {
|
|
|
142 ProcessPendingMessages();
|
|
|
143 } else if (status == WAIT_TIMEOUT || (status >= WAIT_OBJECT_0 && status < WAIT_OBJECT_0 + evCount) ) {
|
|
|
144 return status;
|
|
|
145 } else {
|
|
|
146 uBugCheck();
|
|
|
147 }
|
|
|
148 now = GetTickCount();
|
|
|
149 }
|
|
|
150 }
|
|
|
151
|
|
|
152 bool pfcWaitMsgLoop(HANDLE ev, double timeout) {
|
|
|
153 DWORD ms = pfc::event::g_calculate_wait_time(timeout);
|
|
|
154 DWORD status = MultiWait_MsgLoop(&ev, 1, ms);
|
|
|
155 return status == WAIT_OBJECT_0;
|
|
|
156 }
|
|
|
157
|
|
|
158 #endif // FOOBAR2000_DESKTOP_WINDOWS
|
|
|
159
|
|
|
160 }
|
|
|
161 #endif |