X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fwin32%2Fsilcwin32mutex.c;h=34db890fe8862e17a5cf193c1a91f02280817aaa;hb=40f8443d8d3a6577336ee66d18e04d9ac4d956bb;hp=95a6e2651262ee7d203cd98dd0fa27b032f71649;hpb=8ea2a4ce03f30c1742dd3af1dd9b6838e60a93eb;p=silc.git diff --git a/lib/silcutil/win32/silcwin32mutex.c b/lib/silcutil/win32/silcwin32mutex.c index 95a6e265..34db890f 100644 --- a/lib/silcutil/win32/silcwin32mutex.c +++ b/lib/silcutil/win32/silcwin32mutex.c @@ -4,13 +4,12 @@ Author: Pekka Riikonen - Copyright (C) 2001 Pekka Riikonen + Copyright (C) 2001 - 2005 Pekka Riikonen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - + the Free Software Foundation; version 2 of the License. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -19,48 +18,59 @@ */ /* $Id$ */ -#include "silcincludes.h" +#include "silc.h" /* SILC Mutex structure */ struct SilcMutexStruct { #ifdef SILC_THREADS - HANDLE mutex; + CRITICAL_SECTION mutex; + BOOL locked; #else void *tmp; #endif /* SILC_THREADS */ }; -bool silc_mutex_alloc(SilcMutex *mutex) +SilcBool silc_mutex_alloc(SilcMutex *mutex) { #ifdef SILC_THREADS *mutex = silc_calloc(1, sizeof(**mutex)); - (*mutex)->mutex = CreateMutex(NULL, FALSE, NULL); - if (!(*mutex)->mutex) { - silc_free(*mutex); + if (!(*mutex)) return FALSE; - } -#endif /* SILC_THREADS */ + InitializeCriticalSection(&((*mutex)->mutex)); return TRUE; +#else + return FALSE; +#endif /* SILC_THREADS */ } void silc_mutex_free(SilcMutex mutex) { #ifdef SILC_THREADS - CloseHandle(mutex->mutex); - silc_free(mutex); + if (mutex) { + DeleteCriticalSection(&mutex->mutex); + silc_free(mutex); + } #endif /* SILC_THREADS */ } void silc_mutex_lock(SilcMutex mutex) { #ifdef SILC_THREADS - WaitForSingleObject(mutex->mutex, INFINITE); + if (mutex) { + EnterCriticalSection(&mutex->mutex); + assert(mutex->locked == FALSE); + mutex->locked = TRUE; + } #endif /* SILC_THREADS */ } void silc_mutex_unlock(SilcMutex mutex) { #ifdef SILC_THREADS - ReleaseMutex(mutex->mutex); + if (mutex) { + assert(mutex->locked == TRUE); + mutex->locked = FALSE; + LeaveCriticalSection(&mutex->mutex); + } #endif /* SILC_THREADS */ }