winpr-comm: added _HandleCloseCbsLock to protect accesses to _HandleCloseCbs

This commit is contained in:
Emmanuel Ledoux 2014-07-03 11:24:37 +02:00 committed by Emmanuel Ledoux
parent 87ce88370a
commit fba58a5b90
1 changed files with 16 additions and 1 deletions

View File

@ -45,6 +45,7 @@
/* _HandleCreators is a NULL-terminated array with a maximun of HANDLE_CREATOR_MAX HANDLE_CREATOR */
#define HANDLE_CLOSE_CB_MAX 128
static HANDLE_CLOSE_CB **_HandleCloseCbs = NULL;
static CRITICAL_SECTION _HandleCloseCbsLock;
static pthread_once_t _HandleCloseCbsInitialized = PTHREAD_ONCE_INIT;
static void _HandleCloseCbsInit()
@ -55,6 +56,8 @@ static void _HandleCloseCbsInit()
_HandleCloseCbs = (HANDLE_CLOSE_CB**)calloc(HANDLE_CLOSE_CB_MAX+1, sizeof(HANDLE_CLOSE_CB*));
InitializeCriticalSection(&_HandleCloseCbsLock);
assert(_HandleCloseCbs != NULL);
}
@ -75,16 +78,20 @@ BOOL RegisterHandleCloseCb(HANDLE_CLOSE_CB *pHandleCloseCb)
return FALSE;
}
EnterCriticalSection(&_HandleCloseCbsLock);
for (i=0; i<HANDLE_CLOSE_CB_MAX; i++)
{
if (_HandleCloseCbs[i] == NULL)
{
_HandleCloseCbs[i] = pHandleCloseCb;
LeaveCriticalSection(&_HandleCloseCbsLock);
return TRUE;
}
}
LeaveCriticalSection(&_HandleCloseCbsLock);
return FALSE;
}
@ -109,15 +116,23 @@ BOOL CloseHandle(HANDLE hObject)
return FALSE;
}
EnterCriticalSection(&_HandleCloseCbsLock);
for (i=0; _HandleCloseCbs[i] != NULL; i++)
{
HANDLE_CLOSE_CB *close_cb = (HANDLE_CLOSE_CB*)_HandleCloseCbs[i];
if (close_cb && close_cb->IsHandled(hObject))
{
return close_cb->CloseHandle(hObject);
BOOL result = close_cb->CloseHandle(hObject);
LeaveCriticalSection(&_HandleCloseCbsLock);
return result;
}
}
LeaveCriticalSection(&_HandleCloseCbsLock);
if (Type == HANDLE_TYPE_THREAD)
{