Added SILC Server library.
[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 - 2005 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; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19 /* $Id$ */
20
21 #include "silc.h"
22
23 /* SILC Mutex structure */
24 struct SilcMutexStruct {
25 #ifdef SILC_THREADS
26   CRITICAL_SECTION mutex;
27   BOOL locked;
28 #else
29   void *tmp;
30 #endif /* SILC_THREADS */
31 };
32
33 SilcBool silc_mutex_alloc(SilcMutex *mutex)
34 {
35 #ifdef SILC_THREADS
36   *mutex = silc_calloc(1, sizeof(**mutex));
37   if (!(*mutex))
38     return FALSE;
39   InitializeCriticalSection(&((*mutex)->mutex));
40   return TRUE;
41 #else
42   return FALSE;
43 #endif /* SILC_THREADS */
44 }
45
46 void silc_mutex_free(SilcMutex mutex)
47 {
48 #ifdef SILC_THREADS
49   if (mutex) {
50     DeleteCriticalSection(&mutex->mutex);
51     silc_free(mutex);
52   }
53 #endif /* SILC_THREADS */
54 }
55
56 void silc_mutex_lock(SilcMutex mutex)
57 {
58 #ifdef SILC_THREADS
59   if (mutex) {
60     EnterCriticalSection(&mutex->mutex);
61     assert(mutex->locked == FALSE);
62     mutex->locked = TRUE;
63   }
64 #endif /* SILC_THREADS */
65 }
66
67 void silc_mutex_unlock(SilcMutex mutex)
68 {
69 #ifdef SILC_THREADS
70   if (mutex) {
71     assert(mutex->locked == TRUE);
72     mutex->locked = FALSE;
73     LeaveCriticalSection(&mutex->mutex);
74   }
75 #endif /* SILC_THREADS */
76 }