From 4d471aacd6efd46928c929f8f42572c260001531 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Wed, 11 Mar 2015 15:11:14 +0100 Subject: [PATCH] Using handle close callback now. --- winpr/libwinpr/synch/mutex.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/winpr/libwinpr/synch/mutex.c b/winpr/libwinpr/synch/mutex.c index 89e6eaa..f87fc5a 100644 --- a/winpr/libwinpr/synch/mutex.c +++ b/winpr/libwinpr/synch/mutex.c @@ -28,12 +28,54 @@ #ifndef _WIN32 #include "../handle/handle.h" +static pthread_once_t mutex_initialized = PTHREAD_ONCE_INIT; + +static HANDLE_CLOSE_CB _MutexHandleCloseCb; + +static BOOL MutexCloseHandle(HANDLE handle); + +static BOOL MutexIsHandled(HANDLE handle) +{ + WINPR_TIMER* pMutex = (WINPR_TIMER*) handle; + + if (!pMutex || pMutex->Type != HANDLE_TYPE_MUTEX) + { + SetLastError(ERROR_INVALID_HANDLE); + return FALSE; + } + + return TRUE; +} + +static void MutexInitialize(void) +{ + _MutexHandleCloseCb.IsHandled = MutexIsHandled; + _MutexHandleCloseCb.CloseHandle = MutexCloseHandle; + RegisterHandleCloseCb(&_MutexHandleCloseCb); +} + +BOOL MutexCloseHandle(HANDLE handle) { + WINPR_MUTEX *mutex = (WINPR_MUTEX *) handle; + + if (!MutexIsHandled(handle)) + return FALSE; + + if(pthread_mutex_destroy(&mutex->mutex)) + return FALSE; + + free(handle); + + return TRUE; +} HANDLE CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCWSTR lpName) { HANDLE handle = NULL; WINPR_MUTEX* mutex; + if (pthrea_once(&mutex_initialized, MutexInitialize)) + return NULL; + mutex = (WINPR_MUTEX*) malloc(sizeof(WINPR_MUTEX)); if (mutex) -- 2.7.4