Added SILC Server library.
[silc.git] / lib / silcutil / unix / silcunixmutex.c
1 /*
2
3   silcunixmutex.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   pthread_mutex_t mutex;
27 #else
28   void *tmp;
29 #endif /* SILC_THREADS */
30 };
31
32 SilcBool silc_mutex_alloc(SilcMutex *mutex)
33 {
34 #ifdef SILC_THREADS
35   *mutex = silc_calloc(1, sizeof(**mutex));
36   if (*mutex == NULL)
37     return FALSE;
38   pthread_mutex_init(&(*mutex)->mutex, NULL);
39   return TRUE;
40 #else
41   return FALSE;
42 #endif /* SILC_THREADS */
43 }
44
45 void silc_mutex_free(SilcMutex mutex)
46 {
47 #ifdef SILC_THREADS
48   if (mutex) {
49     pthread_mutex_destroy(&mutex->mutex);
50     silc_free(mutex);
51   }
52 #endif /* SILC_THREADS */
53 }
54
55 void silc_mutex_lock(SilcMutex mutex)
56 {
57 #ifdef SILC_THREADS
58   if (mutex) {
59     if (pthread_mutex_lock(&mutex->mutex))
60       assert(FALSE);
61   }
62 #endif /* SILC_THREADS */
63 }
64
65 void silc_mutex_unlock(SilcMutex mutex)
66 {
67 #ifdef SILC_THREADS
68   if (mutex) {
69     if (pthread_mutex_unlock(&mutex->mutex))
70       assert(FALSE);
71   }
72 #endif /* SILC_THREADS */
73 }