]>
Commit | Line | Data |
---|---|---|
5cd99866 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/osx/core/evtloop_cf.cpp | |
3 | // Purpose: wxEventLoop implementation common to both Carbon and Cocoa | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2009-10-18 | |
0056673c | 6 | // RCS-ID: $Id$ |
5cd99866 VZ |
7 | // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org> |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // for compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #include "wx/evtloop.h" | |
27 | ||
fe5816f0 VZ |
28 | #ifndef WX_PRECOMP |
29 | #include "wx/log.h" | |
0056673c | 30 | #include "wx/app.h" |
fe5816f0 VZ |
31 | #endif |
32 | ||
5cd99866 VZ |
33 | #include "wx/evtloopsrc.h" |
34 | ||
35 | #include "wx/scopedptr.h" | |
36 | ||
37 | #include "wx/osx/private.h" | |
38 | #include "wx/osx/core/cfref.h" | |
a94c4b85 | 39 | #include "wx/thread.h" |
5cd99866 | 40 | |
976e63a7 VZ |
41 | #if wxUSE_GUI |
42 | #include "wx/nonownedwnd.h" | |
43 | #endif | |
44 | ||
5cd99866 VZ |
45 | // ============================================================================ |
46 | // wxCFEventLoopSource and wxCFEventLoop implementation | |
47 | // ============================================================================ | |
48 | ||
a6956131 VZ |
49 | #if wxUSE_EVENTLOOP_SOURCE |
50 | ||
5cd99866 VZ |
51 | namespace |
52 | { | |
53 | ||
54 | void EnableDescriptorCallBacks(CFFileDescriptorRef cffd, int flags) | |
55 | { | |
56 | if ( flags & wxEVENT_SOURCE_INPUT ) | |
57 | CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack); | |
58 | if ( flags & wxEVENT_SOURCE_OUTPUT ) | |
59 | CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorWriteCallBack); | |
60 | } | |
61 | ||
62 | void | |
63 | wx_cffiledescriptor_callback(CFFileDescriptorRef cffd, | |
64 | CFOptionFlags flags, | |
65 | void *ctxData) | |
66 | { | |
67 | wxLogTrace(wxTRACE_EVT_SOURCE, | |
68 | "CFFileDescriptor callback, flags=%d", flags); | |
69 | ||
70 | wxCFEventLoopSource * const | |
71 | source = static_cast<wxCFEventLoopSource *>(ctxData); | |
72 | ||
73 | wxEventLoopSourceHandler * const | |
74 | handler = source->GetHandler(); | |
75 | if ( flags & kCFFileDescriptorReadCallBack ) | |
76 | handler->OnReadWaiting(); | |
77 | if ( flags & kCFFileDescriptorWriteCallBack ) | |
78 | handler->OnWriteWaiting(); | |
79 | ||
80 | // we need to re-enable callbacks to be called again | |
81 | EnableDescriptorCallBacks(cffd, source->GetFlags()); | |
82 | } | |
83 | ||
84 | } // anonymous namespace | |
85 | ||
86 | wxEventLoopSource * | |
87 | wxCFEventLoop::AddSourceForFD(int fd, | |
88 | wxEventLoopSourceHandler *handler, | |
89 | int flags) | |
90 | { | |
91 | wxCHECK_MSG( fd != -1, NULL, "can't monitor invalid fd" ); | |
92 | ||
93 | wxScopedPtr<wxCFEventLoopSource> | |
94 | source(new wxCFEventLoopSource(handler, flags)); | |
95 | ||
96 | CFFileDescriptorContext ctx = { 0, source.get(), NULL, NULL, NULL }; | |
97 | wxCFRef<CFFileDescriptorRef> | |
98 | cffd(CFFileDescriptorCreate | |
99 | ( | |
100 | kCFAllocatorDefault, | |
101 | fd, | |
102 | true, // close on invalidate | |
103 | wx_cffiledescriptor_callback, | |
104 | &ctx | |
105 | )); | |
106 | if ( !cffd ) | |
107 | return NULL; | |
108 | ||
5cd99866 VZ |
109 | wxCFRef<CFRunLoopSourceRef> |
110 | cfsrc(CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, cffd, 0)); | |
111 | if ( !cfsrc ) | |
112 | return NULL; | |
113 | ||
114 | CFRunLoopRef cfloop = CFGetCurrentRunLoop(); | |
115 | CFRunLoopAddSource(cfloop, cfsrc, kCFRunLoopDefaultMode); | |
116 | ||
1dcca9b5 VZ |
117 | // Enable the callbacks initially. |
118 | EnableDescriptorCallBacks(cffd, source->GetFlags()); | |
119 | ||
bbacbe39 VZ |
120 | source->SetFileDescriptor(cffd.release()); |
121 | ||
5cd99866 VZ |
122 | return source.release(); |
123 | } | |
124 | ||
125 | void wxCFEventLoopSource::SetFileDescriptor(CFFileDescriptorRef cffd) | |
126 | { | |
127 | wxASSERT_MSG( !m_cffd, "shouldn't be called more than once" ); | |
128 | ||
129 | m_cffd = cffd; | |
130 | } | |
131 | ||
132 | wxCFEventLoopSource::~wxCFEventLoopSource() | |
133 | { | |
134 | if ( m_cffd ) | |
135 | CFRelease(m_cffd); | |
136 | } | |
137 | ||
5cd99866 | 138 | #endif // wxUSE_EVENTLOOP_SOURCE |
0056673c | 139 | |
3cac3654 | 140 | void wxCFEventLoop::OSXCommonModeObserverCallBack(CFRunLoopObserverRef observer, int activity, void *info) |
293a13ba SC |
141 | { |
142 | wxCFEventLoop * eventloop = static_cast<wxCFEventLoop *>(info); | |
143 | if ( eventloop ) | |
3cac3654 | 144 | eventloop->CommonModeObserverCallBack(observer, activity); |
976e63a7 | 145 | } |
293a13ba | 146 | |
3cac3654 SC |
147 | void wxCFEventLoop::OSXDefaultModeObserverCallBack(CFRunLoopObserverRef observer, int activity, void *info) |
148 | { | |
149 | wxCFEventLoop * eventloop = static_cast<wxCFEventLoop *>(info); | |
150 | if ( eventloop ) | |
151 | eventloop->DefaultModeObserverCallBack(observer, activity); | |
152 | } | |
153 | ||
154 | void wxCFEventLoop::CommonModeObserverCallBack(CFRunLoopObserverRef WXUNUSED(observer), int activity) | |
293a13ba SC |
155 | { |
156 | if ( activity & kCFRunLoopBeforeTimers ) | |
157 | { | |
158 | // process pending wx events first as they correspond to low-level events | |
159 | // which happened before, i.e. typically pending events were queued by a | |
160 | // previous call to Dispatch() and if we didn't process them now the next | |
161 | // call to it might enqueue them again (as happens with e.g. socket events | |
162 | // which would be generated as long as there is input available on socket | |
163 | // and this input is only removed from it when pending event handlers are | |
164 | // executed) | |
165 | ||
19736e64 | 166 | if ( wxTheApp && ShouldProcessIdleEvents() ) |
293a13ba SC |
167 | wxTheApp->ProcessPendingEvents(); |
168 | } | |
976e63a7 | 169 | |
3cac3654 SC |
170 | if ( activity & kCFRunLoopBeforeWaiting ) |
171 | { | |
19736e64 | 172 | if ( ShouldProcessIdleEvents() && ProcessIdle() ) |
592a3c6e SC |
173 | { |
174 | WakeUp(); | |
175 | } | |
176 | else | |
177 | { | |
3cac3654 | 178 | #if wxUSE_THREADS |
592a3c6e | 179 | wxMutexGuiLeaveOrEnter(); |
3cac3654 | 180 | #endif |
592a3c6e | 181 | } |
3cac3654 SC |
182 | } |
183 | } | |
184 | ||
6a06eecf VZ |
185 | void |
186 | wxCFEventLoop::DefaultModeObserverCallBack(CFRunLoopObserverRef WXUNUSED(observer), | |
187 | int WXUNUSED(activity)) | |
3cac3654 | 188 | { |
592a3c6e | 189 | /* |
3cac3654 SC |
190 | if ( activity & kCFRunLoopBeforeTimers ) |
191 | { | |
192 | } | |
193 | ||
293a13ba SC |
194 | if ( activity & kCFRunLoopBeforeWaiting ) |
195 | { | |
293a13ba | 196 | } |
592a3c6e | 197 | */ |
293a13ba SC |
198 | } |
199 | ||
3cac3654 | 200 | |
0056673c SC |
201 | wxCFEventLoop::wxCFEventLoop() |
202 | { | |
203 | m_shouldExit = false; | |
4c4cbded | 204 | m_processIdleEvents = true; |
976e63a7 | 205 | |
d60957aa SC |
206 | #if wxUSE_UIACTIONSIMULATOR |
207 | m_shouldWaitForEvent = false; | |
208 | #endif | |
209 | ||
9aee1212 | 210 | m_runLoop = CFGetCurrentRunLoop(); |
976e63a7 | 211 | |
293a13ba SC |
212 | CFRunLoopObserverContext ctxt; |
213 | bzero( &ctxt, sizeof(ctxt) ); | |
214 | ctxt.info = this; | |
3cac3654 SC |
215 | m_commonModeRunLoopObserver = CFRunLoopObserverCreate( kCFAllocatorDefault, kCFRunLoopBeforeTimers | kCFRunLoopBeforeWaiting , true /* repeats */, 0, |
216 | (CFRunLoopObserverCallBack) wxCFEventLoop::OSXCommonModeObserverCallBack, &ctxt ); | |
217 | CFRunLoopAddObserver(m_runLoop, m_commonModeRunLoopObserver, kCFRunLoopCommonModes); | |
3cac3654 SC |
218 | |
219 | m_defaultModeRunLoopObserver = CFRunLoopObserverCreate( kCFAllocatorDefault, kCFRunLoopBeforeTimers | kCFRunLoopBeforeWaiting , true /* repeats */, 0, | |
220 | (CFRunLoopObserverCallBack) wxCFEventLoop::OSXDefaultModeObserverCallBack, &ctxt ); | |
221 | CFRunLoopAddObserver(m_runLoop, m_defaultModeRunLoopObserver, kCFRunLoopDefaultMode); | |
0056673c SC |
222 | } |
223 | ||
224 | wxCFEventLoop::~wxCFEventLoop() | |
225 | { | |
3cac3654 SC |
226 | CFRunLoopRemoveObserver(m_runLoop, m_commonModeRunLoopObserver, kCFRunLoopCommonModes); |
227 | CFRunLoopRemoveObserver(m_runLoop, m_defaultModeRunLoopObserver, kCFRunLoopDefaultMode); | |
4c4cbded SC |
228 | |
229 | CFRelease(m_defaultModeRunLoopObserver); | |
230 | CFRelease(m_commonModeRunLoopObserver); | |
0056673c | 231 | } |
976e63a7 | 232 | |
0056673c SC |
233 | |
234 | CFRunLoopRef wxCFEventLoop::CFGetCurrentRunLoop() const | |
235 | { | |
293a13ba | 236 | return CFRunLoopGetCurrent(); |
0056673c SC |
237 | } |
238 | ||
239 | void wxCFEventLoop::WakeUp() | |
240 | { | |
58e74b83 | 241 | CFRunLoopWakeUp(m_runLoop); |
0056673c SC |
242 | } |
243 | ||
3bfba421 SC |
244 | #if wxUSE_BASE |
245 | ||
246 | void wxMacWakeUp() | |
247 | { | |
248 | wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); | |
ce00f59b | 249 | |
3bfba421 SC |
250 | if ( loop ) |
251 | loop->WakeUp(); | |
252 | } | |
253 | ||
254 | #endif | |
255 | ||
0056673c SC |
256 | bool wxCFEventLoop::YieldFor(long eventsToProcess) |
257 | { | |
258 | #if wxUSE_THREADS | |
259 | // Yielding from a non-gui thread needs to bail out, otherwise we end up | |
260 | // possibly sending events in the thread too. | |
261 | if ( !wxThread::IsMain() ) | |
262 | { | |
263 | return true; | |
264 | } | |
265 | #endif // wxUSE_THREADS | |
976e63a7 | 266 | |
0056673c SC |
267 | m_isInsideYield = true; |
268 | m_eventsToProcessInsideYield = eventsToProcess; | |
976e63a7 | 269 | |
0056673c SC |
270 | #if wxUSE_LOG |
271 | // disable log flushing from here because a call to wxYield() shouldn't | |
272 | // normally result in message boxes popping up &c | |
273 | wxLog::Suspend(); | |
274 | #endif // wxUSE_LOG | |
976e63a7 | 275 | |
0056673c SC |
276 | // process all pending events: |
277 | while ( DoProcessEvents() == 1 ) | |
278 | ; | |
976e63a7 | 279 | |
0056673c SC |
280 | // it's necessary to call ProcessIdle() to update the frames sizes which |
281 | // might have been changed (it also will update other things set from | |
282 | // OnUpdateUI() which is a nice (and desired) side effect) | |
283 | while ( ProcessIdle() ) {} | |
976e63a7 | 284 | |
0056673c SC |
285 | // if there are pending events, we must process them. |
286 | if (wxTheApp) | |
287 | wxTheApp->ProcessPendingEvents(); | |
976e63a7 | 288 | |
0056673c SC |
289 | #if wxUSE_LOG |
290 | wxLog::Resume(); | |
291 | #endif // wxUSE_LOG | |
292 | m_isInsideYield = false; | |
976e63a7 | 293 | |
0056673c SC |
294 | return true; |
295 | } | |
296 | ||
297 | // implement/override base class pure virtual | |
298 | bool wxCFEventLoop::Pending() const | |
299 | { | |
300 | return true; | |
301 | } | |
302 | ||
303 | int wxCFEventLoop::DoProcessEvents() | |
304 | { | |
d60957aa SC |
305 | if ( m_shouldWaitForEvent ) |
306 | { | |
57fe8307 | 307 | int handled = DispatchTimeout( 1000 ); |
d60957aa SC |
308 | wxASSERT_MSG( handled == 1, "No Event Available"); |
309 | m_shouldWaitForEvent = false; | |
57fe8307 | 310 | return handled; |
d60957aa | 311 | } |
57fe8307 SC |
312 | else |
313 | return DispatchTimeout( 0 ); | |
0056673c SC |
314 | } |
315 | ||
316 | bool wxCFEventLoop::Dispatch() | |
317 | { | |
293a13ba | 318 | return DoProcessEvents() != 0; |
0056673c SC |
319 | } |
320 | ||
321 | int wxCFEventLoop::DispatchTimeout(unsigned long timeout) | |
322 | { | |
323 | if ( !wxTheApp ) | |
324 | return 0; | |
325 | ||
0056673c | 326 | int status = DoDispatchTimeout(timeout); |
976e63a7 | 327 | |
0056673c SC |
328 | switch( status ) |
329 | { | |
330 | case 0: | |
331 | break; | |
332 | case -1: | |
333 | if ( m_shouldExit ) | |
334 | return 0; | |
976e63a7 | 335 | |
0056673c SC |
336 | break; |
337 | case 1: | |
0056673c SC |
338 | break; |
339 | } | |
976e63a7 | 340 | |
0056673c SC |
341 | return status; |
342 | } | |
343 | ||
344 | int wxCFEventLoop::DoDispatchTimeout(unsigned long timeout) | |
976e63a7 | 345 | { |
0056673c SC |
346 | SInt32 status = CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeout / 1000.0 , true); |
347 | switch( status ) | |
348 | { | |
349 | case kCFRunLoopRunFinished: | |
350 | wxFAIL_MSG( "incorrect run loop state" ); | |
351 | break; | |
352 | case kCFRunLoopRunStopped: | |
353 | return 0; | |
354 | break; | |
355 | case kCFRunLoopRunTimedOut: | |
356 | return -1; | |
357 | break; | |
358 | case kCFRunLoopRunHandledSource: | |
359 | default: | |
360 | break; | |
361 | } | |
362 | return 1; | |
363 | } | |
364 | ||
80eee837 SC |
365 | void wxCFEventLoop::DoRun() |
366 | { | |
367 | for ( ;; ) | |
368 | { | |
369 | // generate and process idle events for as long as we don't | |
370 | // have anything else to do | |
371 | DoProcessEvents(); | |
976e63a7 | 372 | |
80eee837 SC |
373 | // if the "should exit" flag is set, the loop should terminate |
374 | // but not before processing any remaining messages so while | |
375 | // Pending() returns true, do process them | |
376 | if ( m_shouldExit ) | |
377 | { | |
378 | while ( DoProcessEvents() == 1 ) | |
379 | ; | |
976e63a7 | 380 | |
80eee837 SC |
381 | break; |
382 | } | |
383 | } | |
384 | } | |
385 | ||
386 | void wxCFEventLoop::DoStop() | |
387 | { | |
388 | CFRunLoopStop(CFGetCurrentRunLoop()); | |
389 | } | |
390 | ||
0056673c SC |
391 | // enters a loop calling OnNextIteration(), Pending() and Dispatch() and |
392 | // terminating when Exit() is called | |
393 | int wxCFEventLoop::Run() | |
394 | { | |
395 | // event loops are not recursive, you need to create another loop! | |
396 | wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") ); | |
976e63a7 | 397 | |
0056673c SC |
398 | // ProcessIdle() and ProcessEvents() below may throw so the code here should |
399 | // be exception-safe, hence we must use local objects for all actions we | |
400 | // should undo | |
401 | wxEventLoopActivator activate(this); | |
976e63a7 | 402 | |
0056673c SC |
403 | // we must ensure that OnExit() is called even if an exception is thrown |
404 | // from inside ProcessEvents() but we must call it from Exit() in normal | |
405 | // situations because it is supposed to be called synchronously, | |
406 | // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or | |
407 | // something similar here) | |
408 | #if wxUSE_EXCEPTIONS | |
409 | for ( ;; ) | |
410 | { | |
411 | try | |
412 | { | |
413 | #endif // wxUSE_EXCEPTIONS | |
976e63a7 | 414 | |
80eee837 | 415 | DoRun(); |
976e63a7 | 416 | |
0056673c SC |
417 | #if wxUSE_EXCEPTIONS |
418 | // exit the outer loop as well | |
419 | break; | |
420 | } | |
421 | catch ( ... ) | |
422 | { | |
423 | try | |
424 | { | |
425 | if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() ) | |
426 | { | |
427 | OnExit(); | |
428 | break; | |
429 | } | |
430 | //else: continue running the event loop | |
431 | } | |
432 | catch ( ... ) | |
433 | { | |
434 | // OnException() throwed, possibly rethrowing the same | |
435 | // exception again: very good, but we still need OnExit() to | |
436 | // be called | |
437 | OnExit(); | |
438 | throw; | |
439 | } | |
440 | } | |
441 | } | |
442 | #endif // wxUSE_EXCEPTIONS | |
976e63a7 | 443 | |
0056673c SC |
444 | return m_exitcode; |
445 | } | |
446 | ||
447 | // sets the "should exit" flag and wakes up the loop so that it terminates | |
448 | // soon | |
449 | void wxCFEventLoop::Exit(int rc) | |
450 | { | |
451 | m_exitcode = rc; | |
452 | m_shouldExit = true; | |
80eee837 SC |
453 | DoStop(); |
454 | } | |
a94c4b85 | 455 | |
c657294b | 456 | wxCFEventLoopPauseIdleEvents::wxCFEventLoopPauseIdleEvents() |
4c4cbded SC |
457 | { |
458 | wxCFEventLoop* cfl = dynamic_cast<wxCFEventLoop*>(wxEventLoopBase::GetActive()); | |
459 | if ( cfl ) | |
19736e64 SC |
460 | { |
461 | m_formerState = cfl->ShouldProcessIdleEvents(); | |
4c4cbded | 462 | cfl->SetProcessIdleEvents(false); |
19736e64 SC |
463 | } |
464 | else | |
465 | m_formerState = true; | |
4c4cbded SC |
466 | } |
467 | ||
c657294b | 468 | wxCFEventLoopPauseIdleEvents::~wxCFEventLoopPauseIdleEvents() |
4c4cbded SC |
469 | { |
470 | wxCFEventLoop* cfl = dynamic_cast<wxCFEventLoop*>(wxEventLoopBase::GetActive()); | |
471 | if ( cfl ) | |
19736e64 | 472 | cfl->SetProcessIdleEvents(m_formerState); |
4c4cbded SC |
473 | } |
474 | ||
a94c4b85 SC |
475 | // TODO Move to thread_osx.cpp |
476 | ||
477 | #if wxUSE_THREADS | |
478 | ||
479 | // ---------------------------------------------------------------------------- | |
480 | // GUI Serialization copied from MSW implementation | |
481 | // ---------------------------------------------------------------------------- | |
482 | ||
483 | // if it's false, some secondary thread is holding the GUI lock | |
484 | static bool gs_bGuiOwnedByMainThread = true; | |
485 | ||
486 | // critical section which controls access to all GUI functions: any secondary | |
487 | // thread (i.e. except the main one) must enter this crit section before doing | |
488 | // any GUI calls | |
489 | static wxCriticalSection *gs_critsectGui = NULL; | |
490 | ||
491 | // critical section which protects gs_nWaitingForGui variable | |
492 | static wxCriticalSection *gs_critsectWaitingForGui = NULL; | |
493 | ||
494 | // number of threads waiting for GUI in wxMutexGuiEnter() | |
495 | static size_t gs_nWaitingForGui = 0; | |
496 | ||
497 | void wxOSXThreadModuleOnInit() | |
498 | { | |
ce00f59b | 499 | gs_critsectWaitingForGui = new wxCriticalSection(); |
a94c4b85 SC |
500 | gs_critsectGui = new wxCriticalSection(); |
501 | gs_critsectGui->Enter(); | |
502 | } | |
503 | ||
504 | ||
505 | void wxOSXThreadModuleOnExit() | |
506 | { | |
507 | if ( gs_critsectGui ) | |
508 | { | |
509 | if ( !wxGuiOwnedByMainThread() ) | |
510 | { | |
511 | gs_critsectGui->Enter(); | |
512 | gs_bGuiOwnedByMainThread = true; | |
513 | } | |
ce00f59b | 514 | |
a94c4b85 SC |
515 | gs_critsectGui->Leave(); |
516 | wxDELETE(gs_critsectGui); | |
517 | } | |
ce00f59b | 518 | |
a94c4b85 SC |
519 | wxDELETE(gs_critsectWaitingForGui); |
520 | } | |
521 | ||
522 | ||
523 | // wake up the main thread | |
524 | void WXDLLIMPEXP_BASE wxWakeUpMainThread() | |
525 | { | |
526 | wxMacWakeUp(); | |
527 | } | |
528 | ||
529 | void wxMutexGuiEnterImpl() | |
530 | { | |
531 | // this would dead lock everything... | |
532 | wxASSERT_MSG( !wxThread::IsMain(), | |
533 | wxT("main thread doesn't want to block in wxMutexGuiEnter()!") ); | |
ce00f59b | 534 | |
a94c4b85 | 535 | // the order in which we enter the critical sections here is crucial!! |
ce00f59b | 536 | |
a94c4b85 SC |
537 | // set the flag telling to the main thread that we want to do some GUI |
538 | { | |
539 | wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); | |
ce00f59b | 540 | |
a94c4b85 SC |
541 | gs_nWaitingForGui++; |
542 | } | |
ce00f59b | 543 | |
a94c4b85 | 544 | wxWakeUpMainThread(); |
ce00f59b | 545 | |
a94c4b85 SC |
546 | // now we may block here because the main thread will soon let us in |
547 | // (during the next iteration of OnIdle()) | |
548 | gs_critsectGui->Enter(); | |
549 | } | |
550 | ||
551 | void wxMutexGuiLeaveImpl() | |
552 | { | |
553 | wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); | |
ce00f59b | 554 | |
a94c4b85 SC |
555 | if ( wxThread::IsMain() ) |
556 | { | |
557 | gs_bGuiOwnedByMainThread = false; | |
558 | } | |
559 | else | |
560 | { | |
561 | // decrement the number of threads waiting for GUI access now | |
562 | wxASSERT_MSG( gs_nWaitingForGui > 0, | |
563 | wxT("calling wxMutexGuiLeave() without entering it first?") ); | |
ce00f59b | 564 | |
a94c4b85 | 565 | gs_nWaitingForGui--; |
ce00f59b | 566 | |
a94c4b85 SC |
567 | wxWakeUpMainThread(); |
568 | } | |
ce00f59b | 569 | |
a94c4b85 SC |
570 | gs_critsectGui->Leave(); |
571 | } | |
572 | ||
573 | void WXDLLIMPEXP_BASE wxMutexGuiLeaveOrEnter() | |
574 | { | |
575 | wxASSERT_MSG( wxThread::IsMain(), | |
576 | wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") ); | |
ce00f59b | 577 | |
a94c4b85 SC |
578 | if ( !gs_critsectWaitingForGui ) |
579 | return; | |
ce00f59b | 580 | |
a94c4b85 | 581 | wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); |
ce00f59b | 582 | |
a94c4b85 SC |
583 | if ( gs_nWaitingForGui == 0 ) |
584 | { | |
585 | // no threads are waiting for GUI - so we may acquire the lock without | |
586 | // any danger (but only if we don't already have it) | |
587 | if ( !wxGuiOwnedByMainThread() ) | |
588 | { | |
589 | gs_critsectGui->Enter(); | |
ce00f59b | 590 | |
a94c4b85 SC |
591 | gs_bGuiOwnedByMainThread = true; |
592 | } | |
593 | //else: already have it, nothing to do | |
594 | } | |
595 | else | |
596 | { | |
597 | // some threads are waiting, release the GUI lock if we have it | |
598 | if ( wxGuiOwnedByMainThread() ) | |
599 | wxMutexGuiLeave(); | |
600 | //else: some other worker thread is doing GUI | |
601 | } | |
602 | } | |
603 | ||
604 | bool WXDLLIMPEXP_BASE wxGuiOwnedByMainThread() | |
605 | { | |
606 | return gs_bGuiOwnedByMainThread; | |
607 | } | |
608 | ||
609 | #endif |