use wxCriticalSection instead of CRITICAL_SECTION and, more importantly, wxCSLocker instead of manually entering/leaving it

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57553 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-12-25 19:32:12 +00:00
parent 116de99148
commit f2c94e8af5

View File

@@ -30,6 +30,7 @@
#include "wx/private/socket.h" #include "wx/private/socket.h"
#include "wx/apptrait.h" #include "wx/apptrait.h"
#include "wx/thread.h"
extern "C" WXDLLIMPEXP_BASE HINSTANCE wxGetInstance(); extern "C" WXDLLIMPEXP_BASE HINSTANCE wxGetInstance();
#define INSTANCE wxGetInstance() #define INSTANCE wxGetInstance()
@@ -101,7 +102,7 @@ LRESULT CALLBACK wxSocket_Internal_WinProc(HWND, UINT, WPARAM, LPARAM);
/* Global variables */ /* Global variables */
static HWND hWin; static HWND hWin;
static CRITICAL_SECTION critical; wxCRIT_SECT_DECLARE_MEMBER(gs_critical);
static wxSocketImplMSW *socketList[MAXSOCKETS]; static wxSocketImplMSW *socketList[MAXSOCKETS];
static int firstAvailable; static int firstAvailable;
@@ -193,8 +194,6 @@ public:
virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event); virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event);
}; };
/* Global initializers */
bool wxSocketMSWManager::OnInit() bool wxSocketMSWManager::OnInit()
{ {
static LPCTSTR pclassname = NULL; static LPCTSTR pclassname = NULL;
@@ -206,8 +205,6 @@ bool wxSocketMSWManager::OnInit()
return false; return false;
/* Initialize socket list */ /* Initialize socket list */
InitializeCriticalSection(&critical);
for (i = 0; i < MAXSOCKETS; i++) for (i = 0; i < MAXSOCKETS; i++)
{ {
socketList[i] = NULL; socketList[i] = NULL;
@@ -277,9 +274,6 @@ void wxSocketMSWManager::OnExit()
gs_wsock32dll = 0; gs_wsock32dll = 0;
} }
/* Delete critical section */
DeleteCriticalSection(&critical);
WSACleanup(); WSACleanup();
} }
@@ -289,7 +283,7 @@ wxSocketImplMSW::wxSocketImplMSW(wxSocketBase& wxsocket)
: wxSocketImpl(wxsocket) : wxSocketImpl(wxsocket)
{ {
/* Allocate a new message number for this socket */ /* Allocate a new message number for this socket */
EnterCriticalSection(&critical); wxCRIT_SECT_LOCKER(lock, gs_critical);
int i = firstAvailable; int i = firstAvailable;
while (socketList[i] != NULL) while (socketList[i] != NULL)
@@ -298,7 +292,6 @@ wxSocketImplMSW::wxSocketImplMSW(wxSocketBase& wxsocket)
if (i == firstAvailable) /* abort! */ if (i == firstAvailable) /* abort! */
{ {
LeaveCriticalSection(&critical);
m_msgnumber = 0; // invalid m_msgnumber = 0; // invalid
return; return;
} }
@@ -306,14 +299,12 @@ wxSocketImplMSW::wxSocketImplMSW(wxSocketBase& wxsocket)
socketList[i] = this; socketList[i] = this;
firstAvailable = (i + 1) % MAXSOCKETS; firstAvailable = (i + 1) % MAXSOCKETS;
m_msgnumber = (i + WM_USER); m_msgnumber = (i + WM_USER);
LeaveCriticalSection(&critical);
} }
wxSocketImplMSW::~wxSocketImplMSW() wxSocketImplMSW::~wxSocketImplMSW()
{ {
/* Remove the socket from the list */ /* Remove the socket from the list */
EnterCriticalSection(&critical); wxCRIT_SECT_LOCKER(lock, gs_critical);
if ( m_msgnumber ) if ( m_msgnumber )
{ {
@@ -327,8 +318,6 @@ wxSocketImplMSW::~wxSocketImplMSW()
socketList[m_msgnumber - WM_USER] = NULL; socketList[m_msgnumber - WM_USER] = NULL;
} }
//else: the socket has never been created successfully //else: the socket has never been created successfully
LeaveCriticalSection(&critical);
} }
/* Windows proc for asynchronous event handling */ /* Windows proc for asynchronous event handling */
@@ -338,12 +327,14 @@ LRESULT CALLBACK wxSocket_Internal_WinProc(HWND hWnd,
WPARAM wParam, WPARAM wParam,
LPARAM lParam) LPARAM lParam)
{ {
if ( uMsg < WM_USER || uMsg > (WM_USER + MAXSOCKETS - 1))
return DefWindowProc(hWnd, uMsg, wParam, lParam);
wxSocketImplMSW *socket; wxSocketImplMSW *socket;
wxSocketNotify event; wxSocketNotify event;
if (uMsg >= WM_USER && uMsg <= (WM_USER + MAXSOCKETS - 1))
{ {
EnterCriticalSection(&critical); wxCRIT_SECT_LOCKER(lock, gs_critical);
socket = socketList[(uMsg - WM_USER)]; socket = socketList[(uMsg - WM_USER)];
event = (wxSocketNotify) -1; event = (wxSocketNotify) -1;
@@ -377,17 +368,13 @@ LRESULT CALLBACK wxSocket_Internal_WinProc(HWND hWnd,
socket->m_detected |= (1 << event); socket->m_detected |= (1 << event);
} }
} }
} // unlock gs_critical
LeaveCriticalSection(&critical);
if ( socket ) if ( socket )
socket->NotifyOnStateChange(event); socket->NotifyOnStateChange(event);
return (LRESULT) 0; return (LRESULT) 0;
} }
else
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
/* /*
* Enable all event notifications; we need to be notified of all * Enable all event notifications; we need to be notified of all