X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0a6476912577c0ce870b1d2a829825bad45d35a8..30413fd03ea2a41832d5ad101819bbad54b8252e:/src/mac/corefoundation/gsockosx.cpp?ds=inline diff --git a/src/mac/corefoundation/gsockosx.cpp b/src/mac/corefoundation/gsockosx.cpp index a68578e1c9..0d2915900e 100644 --- a/src/mac/corefoundation/gsockosx.cpp +++ b/src/mac/corefoundation/gsockosx.cpp @@ -24,6 +24,10 @@ struct MacGSocketData CFRunLoopSourceRef source; }; +// Sockets must use the event loop on the main thread +// We will store the main loop's reference when Initialize is called +static CFRunLoopRef s_mainRunLoop = NULL; + void Mac_Socket_Callback(CFSocketRef s, CFSocketCallBackType callbackType, CFDataRef address, const void* data, void* info) { @@ -81,11 +85,24 @@ bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop() bool GSocketGUIFunctionsTableConcrete::OnInit(void) { + // No need to store the main loop again + if (s_mainRunLoop != NULL) + return true; + + // Get the loop for the main thread so our events will actually fire. + // The common socket.cpp code will assert if initialize is called from a + // secondary thread, otherwise Mac would have the same problems as MSW + s_mainRunLoop = CFRunLoopGetCurrent(); + CFRetain(s_mainRunLoop); + return true; } void GSocketGUIFunctionsTableConcrete::OnExit(void) { + // Release the reference count, and set the reference back to NULL + CFRelease(s_mainRunLoop); + s_mainRunLoop = NULL; } bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket) @@ -106,7 +123,8 @@ void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket) struct MacGSocketData *data = (struct MacGSocketData*)(socket->m_gui_dependent); if (data) { - CFRelease(data->socket); + if ( data->socket ) + CFRelease(data->socket); free(data); } } @@ -168,7 +186,7 @@ void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket *socket) struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket); if (!data) return; - CFRunLoopAddSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopDefaultMode); + CFRunLoopAddSource(s_mainRunLoop, data->source, kCFRunLoopCommonModes); } void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket) @@ -177,7 +195,7 @@ void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket) if (!data) return; /* CFSocketInvalidate does CFRunLoopRemoveSource anyway */ - CFRunLoopRemoveSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopCommonModes); + CFRunLoopRemoveSource(s_mainRunLoop, data->source, kCFRunLoopCommonModes); CFSocketInvalidate(data->socket); }