Applied #13055: socket thread safety

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_8_BRANCH@67249 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Julian Smart
2011-03-19 20:35:11 +00:00
parent e430a1b28f
commit 22e8503f19
2 changed files with 65 additions and 16 deletions

View File

@@ -160,6 +160,7 @@ wxGTK:
wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT colour index. wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT colour index.
- wxComboCtrl and wxOwnerDrawnComboBox had wrong background colours under some - wxComboCtrl and wxOwnerDrawnComboBox had wrong background colours under some
themes (Marcin Wojdyr). themes (Marcin Wojdyr).
- Added thread-safe fixes to socket code.
wxUniv: wxUniv:

View File

@@ -25,7 +25,9 @@
#include "wx/gsocket.h" #include "wx/gsocket.h"
#include "wx/unix/gsockunx.h" #include "wx/unix/gsockunx.h"
#if wxUSE_THREADS
#include "wx/thread.h"
#endif
extern "C" { extern "C" {
static static
@@ -42,6 +44,13 @@ void _GSocket_GDK_Input(gpointer data,
} }
} }
typedef struct {
#if wxUSE_THREADS
wxMutex* m_mutex;
#endif
gint m_id[2];
} GSocketGTKMutexProtected;
bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop() bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop()
{ return true; } { return true; }
@@ -56,25 +65,37 @@ void GSocketGUIFunctionsTableConcrete::OnExit(void)
bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket) bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket)
{ {
gint *m_id; socket->m_gui_dependent = (char *)malloc(sizeof(GSocketGTKMutexProtected));
socket->m_gui_dependent = (char *)malloc(sizeof(gint)*2); GSocketGTKMutexProtected* guispecific = (GSocketGTKMutexProtected*)socket->m_gui_dependent;
m_id = (gint *)(socket->m_gui_dependent);
m_id[0] = -1; #if wxUSE_THREADS
m_id[1] = -1; guispecific->m_mutex = new wxMutex(wxMUTEX_RECURSIVE);
#endif
guispecific->m_id[0] = -1;
guispecific->m_id[1] = -1;
return TRUE; return TRUE;
} }
void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket) void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket)
{ {
free(socket->m_gui_dependent); GSocketGTKMutexProtected* guispecific = (GSocketGTKMutexProtected*)socket->m_gui_dependent;
#if wxUSE_THREADS
delete guispecific->m_mutex;
#endif
free(guispecific);
} }
void GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket *socket, GSocketEvent event) void GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket *socket, GSocketEvent event)
{ {
gint *m_id = (gint *)(socket->m_gui_dependent); GSocketGTKMutexProtected* guispecific = (GSocketGTKMutexProtected*)socket->m_gui_dependent;
assert(guispecific != NULL);
int c; int c;
if (socket->m_fd == -1) if (socket->m_fd == -1)
@@ -89,21 +110,40 @@ void GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket *socket, GSocket
default: return; default: return;
} }
if (m_id[c] != -1) #if wxUSE_THREADS
gdk_input_remove(m_id[c]); guispecific->m_mutex->Lock();
#endif
gint current_id = guispecific->m_id[c];
guispecific->m_id[c] = -1;
#if wxUSE_THREADS
guispecific->m_mutex->Unlock();
#endif
m_id[c] = gdk_input_add(socket->m_fd, if (current_id != -1) {
gdk_input_remove(current_id);
}
current_id = gdk_input_add(socket->m_fd,
(c ? GDK_INPUT_WRITE : GDK_INPUT_READ), (c ? GDK_INPUT_WRITE : GDK_INPUT_READ),
_GSocket_GDK_Input, _GSocket_GDK_Input,
(gpointer)socket); (gpointer)socket);
#if wxUSE_THREADS
guispecific->m_mutex->Lock();
#endif
guispecific->m_id[c] = current_id;
#if wxUSE_THREADS
guispecific->m_mutex->Unlock();
#endif
} }
void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket *socket, GSocketEvent event) void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket *socket, GSocketEvent event)
{ {
gint *m_id = (gint *)(socket->m_gui_dependent); GSocketGTKMutexProtected* guispecific = (GSocketGTKMutexProtected*)socket->m_gui_dependent;
int c; int c;
assert( m_id != NULL ); assert( guispecific != NULL );
switch (event) switch (event)
{ {
@@ -114,10 +154,18 @@ void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket *socket, GSock
default: return; default: return;
} }
if (m_id[c] != -1) #if wxUSE_THREADS
guispecific->m_mutex->Lock();
#endif
gint current_id = guispecific->m_id[c];
guispecific->m_id[c] = -1;
#if wxUSE_THREADS
guispecific->m_mutex->Unlock();
#endif
if (current_id != -1)
{ {
gdk_input_remove(m_id[c]); gdk_input_remove(current_id);
m_id[c] = -1;
} }
} }