9037505ad42690487b989ee3887e523018125862
[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 "silcincludes.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   InitializeCriticalSection(&((*mutex)->mutex));
38 #endif /* SILC_THREADS */
39   return TRUE;
40 }
41
42 void silc_mutex_free(SilcMutex mutex)
43 {
44 #ifdef SILC_THREADS
45   if (mutex) {
46     DeleteCriticalSection(&mutex->mutex);
47     silc_free(mutex);
48   }
49 #endif /* SILC_THREADS */
50 }
51
52 void silc_mutex_lock(SilcMutex mutex)
53 {
54 #ifdef SILC_THREADS
55   if (mutex) {
56     EnterCriticalSection(&mutex->mutex);
57     assert(mutex->locked == FALSE);
58     mutex->locked = TRUE;
59   }
60 #endif /* SILC_THREADS */
61 }
62
63 void silc_mutex_unlock(SilcMutex mutex)
64 {
65 #ifdef SILC_THREADS
66   if (mutex) {
67     assert(mutex->locked == TRUE);
68     mutex->locked = FALSE;
69     LeaveCriticalSection(&mutex->mutex);
70   }
71 #endif /* SILC_THREADS */
72 }