95a6e2651262ee7d203cd98dd0fa27b032f71649
[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   HANDLE mutex;
28 #else
29   void *tmp;
30 #endif /* SILC_THREADS */
31 };
32
33 bool silc_mutex_alloc(SilcMutex *mutex)
34 {
35 #ifdef SILC_THREADS
36   *mutex = silc_calloc(1, sizeof(**mutex));
37   (*mutex)->mutex = CreateMutex(NULL, FALSE, NULL);
38   if (!(*mutex)->mutex) {
39     silc_free(*mutex);
40     return FALSE;
41   }
42 #endif /* SILC_THREADS */
43   return TRUE;
44 }
45
46 void silc_mutex_free(SilcMutex mutex)
47 {
48 #ifdef SILC_THREADS
49   CloseHandle(mutex->mutex);
50   silc_free(mutex);
51 #endif /* SILC_THREADS */
52 }
53
54 void silc_mutex_lock(SilcMutex mutex)
55 {
56 #ifdef SILC_THREADS
57   WaitForSingleObject(mutex->mutex, INFINITE);
58 #endif /* SILC_THREADS */
59 }
60
61 void silc_mutex_unlock(SilcMutex mutex)
62 {
63 #ifdef SILC_THREADS
64   ReleaseMutex(mutex->mutex);
65 #endif /* SILC_THREADS */
66 }