updates.
[silc.git] / lib / silcutil / beos / silcbeosmutex.c
1 /*
2
3   silcbeosmutex.c 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 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 #ifdef SILC_THREADS
24
25 /* SILC Mutex structure */
26 struct SilcMutexStruct {
27   int sema_count;
28   sem_id sema;
29 };
30
31 bool silc_mutex_alloc(SilcMutex *mutex)
32 {
33   int ret;
34
35   *mutex = silc_calloc(1, sizeof(**mutex));
36   if (*mutex == NULL)
37     return FALSE;
38
39   ret = create_sem(0, "SILC_MUTEX");
40   if (ret < B_NO_ERROR) {
41     silc_free(*mutex);
42     return FALSE;
43   }
44
45   (*mutex)->sema_count = 0;
46   (*mutex)->sema = ret;
47
48   return TRUE;
49 }
50
51 void silc_mutex_free(SilcMutex mutex)
52 {
53   delete_sem(mutex->sema);
54   silc_free(mutex);
55 }
56
57 void silc_mutex_lock(SilcMutex mutex)
58 {
59   if (atomic_add(&mutex->sema_count, 1) > 0) {
60     if (acquire_sem(mutex->sema) < B_NO_ERROR)
61       assert(FALSE);
62   }
63 }
64
65 void silc_mutex_unlock(SilcMutex mutex)
66 {
67   if (atomic_add(&mutes->sema_count, -1) > 1) {
68     if (release_sem(mutex->sema) < B_NO_ERROR)
69       assert(FALSE);
70   }
71 }
72
73 #endif /* SILC_THREADS */