Merged from silc_1_0_branch.
[runtime.git] / lib / silcutil / win32 / silcwin32mutex.c
index 0a186f59610ce0774139a976e5c59247b12be2b1..11c03c9c8e354ff7ca985f7f4a603f9e0412e9bd 100644 (file)
 
 #include "silcincludes.h"
 
-#ifdef SILC_THREADS
-
 /* SILC Mutex structure */
-struct SilcMutexStruct {
-  HANDLE mutex;
+struct SilcMutexStruct {       
+#ifdef SILC_THREADS
+  CRITICAL_SECTION mutex;  
+  BOOL locked;
+#else
+  void *tmp;
+#endif /* SILC_THREADS */
 };
 
 bool silc_mutex_alloc(SilcMutex *mutex)
 {
-  *mutex = silc_calloc(1, sizeof(**mutex));
-  (*mutex)->mutex = CreateMutex(NULL, FALSE, NULL);
-  if (!(*mutex)->mutex) {
-    silc_free(*mutex);
-    return FALSE;
-  }
+#ifdef SILC_THREADS
+  *mutex = silc_calloc(1, sizeof(**mutex));  
+  InitializeCriticalSection(&((*mutex)->mutex));  
+#endif /* SILC_THREADS */
   return TRUE;
 }
 
 void silc_mutex_free(SilcMutex mutex)
 {
-  CloseHandle(mutex->mutex);
+#ifdef SILC_THREADS
+  DeleteCriticalSection(&mutex->mutex);
   silc_free(mutex);
+#endif /* SILC_THREADS */
 }
 
 void silc_mutex_lock(SilcMutex mutex)
 {
-  WaitForSingleObject(mutex->mutex, INFINITE);
+#ifdef SILC_THREADS
+  EnterCriticalSection(&mutex->mutex);    
+  assert(mutex->locked == FALSE);
+  mutex->locked = TRUE;
+#endif /* SILC_THREADS */
 }
 
 void silc_mutex_unlock(SilcMutex mutex)
 {
-  ReleaseMutex(mutex->mutex);
-}
-
+#ifdef SILC_THREADS
+  assert(mutex->locked == TRUE);
+  mutex->locked = FALSE;
+  LeaveCriticalSection(&mutex->mutex);       
 #endif /* SILC_THREADS */
+}