Merged from silc_1_0_branch.
[silc.git] / lib / silcutil / win32 / silcwin32mutex.c
1 /*
2
3   silcwin32mutex.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2001 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /* $Id$ */
21
22 #include "silcincludes.h"
23
24 /* SILC Mutex structure */
25 struct SilcMutexStruct {        
26 #ifdef SILC_THREADS
27   CRITICAL_SECTION mutex;  
28   BOOL locked;
29 #else
30   void *tmp;
31 #endif /* SILC_THREADS */
32 };
33
34 bool silc_mutex_alloc(SilcMutex *mutex)
35 {
36 #ifdef SILC_THREADS
37   *mutex = silc_calloc(1, sizeof(**mutex));  
38   InitializeCriticalSection(&((*mutex)->mutex));  
39 #endif /* SILC_THREADS */
40   return TRUE;
41 }
42
43 void silc_mutex_free(SilcMutex mutex)
44 {
45 #ifdef SILC_THREADS
46   DeleteCriticalSection(&mutex->mutex);
47   silc_free(mutex);
48 #endif /* SILC_THREADS */
49 }
50
51 void silc_mutex_lock(SilcMutex mutex)
52 {
53 #ifdef SILC_THREADS
54   EnterCriticalSection(&mutex->mutex);    
55   assert(mutex->locked == FALSE);
56   mutex->locked = TRUE;
57 #endif /* SILC_THREADS */
58 }
59
60 void silc_mutex_unlock(SilcMutex mutex)
61 {
62 #ifdef SILC_THREADS
63   assert(mutex->locked == TRUE);
64   mutex->locked = FALSE;
65   LeaveCriticalSection(&mutex->mutex);       
66 #endif /* SILC_THREADS */
67 }