Added SILC Thread Queue API
[silc.git] / lib / silcutil / os2 / silcos2mutex.c
1 /*
2
3   silcos2mutex.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 /* I used Apache's APR code as a reference here. */
20 /* $Id$ */
21
22 #include "silc.h"
23
24 #ifdef SILC_THREADS
25
26 /* SILC Mutex structure */
27 struct SilcMutexStruct {
28   HMTX mutex;
29 };
30
31 SilcBool silc_mutex_alloc(SilcMutex *mutex)
32 {
33   char name[64];
34
35   *mutex = silc_calloc(1, sizeof(**mutex));
36   if (*mutex == NULL)
37     return FALSE;
38
39   /* Create the lock. Is the name working? :) */
40   memset(name, 0, sizeof(name));
41   snprintf(name, sizeof(name) - 1, "%p/SEM32/SILC1234$", *mutex);
42   if (!DosCreateMutexSem(name, &(*mutex)->mutex, DC_SEM_SHARED, FALSE)) {
43     silc_free(*mutex);
44     return FALSE;
45   }
46
47   return TRUE;
48 }
49
50 void silc_mutex_free(SilcMutex mutex)
51 {
52   DosCloseMutexSem(mutex->mutex);
53   silc_free(mutex);
54 }
55
56 void silc_mutex_lock(SilcMutex mutex)
57 {
58   if (!DosRequestMutexSem(mutex->mutex, SEM_INDEFINITE_WAIT))
59     assert(FALSE);
60 }
61
62 void silc_mutex_unlock(SilcMutex mutex)
63 {
64   if (!DosReleaseMutexSem(mutex->mutex)
65     assert(FALSE);
66 }
67
68 #endif /* SILC_THREADS */