Added SilcRwLock, a read/write lock API.
[silc.git] / lib / silcutil / silcmutex.h
1 /*
2
3   silcmutex.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2001 - 2007 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
20 /****h* silcutil/SILC Mutex Interface
21  *
22  * DESCRIPTION
23  *
24  * Interface for mutual exclusion locks and read/write locks.  This is
25  * platform independent interface for applications that need concurrency
26  * control.
27  *
28  ***/
29
30 #ifndef SILCMUTEX_H
31 #define SILCMUTEX_H
32
33 /****s* silcutil/SilcMutexAPI/SilcMutex
34  *
35  * NAME
36  *
37  *    typedef struct SilcMutexStruct *SilcMutex;
38  *
39  * DESCRIPTION
40  *
41  *    This context is the actual SILC Mutex and is allocated
42  *    by silc_mutex_alloc and given as argument to all silc_mutex_*
43  *    functions.  It is freed by the silc_mutex_free function.
44  *
45  ***/
46 typedef struct SilcMutexStruct *SilcMutex;
47
48 /****s* silcutil/SilcMutexAPI/SilcRwLock
49  *
50  * NAME
51  *
52  *    typedef struct SilcRwLockStruct *SilcRwLock;
53  *
54  * DESCRIPTION
55  *
56  *    This context is the actual SILC read/write lock and is allocated
57  *    by silc_rwlock_alloc and given as argument to all silc_rwlock_*
58  *    functions.  It is freed by the silc_rwlock_free function.
59  *
60  ***/
61 typedef struct SilcRwLockStruct *SilcRwLock;
62
63 /****f* silcutil/SilcMutexAPI/silc_mutex_alloc
64  *
65  * SYNOPSIS
66  *
67  *    SilcBool silc_mutex_alloc(SilcMutex *mutex);
68  *
69  * DESCRIPTION
70  *
71  *    Allocates SILC Mutex object.  The mutex object must be allocated
72  *    before it can be used.  It is freed by the silc_mutex_free function.
73  *    This returns TRUE and allocated mutex in to the `mutex' and FALSE
74  *    on error.
75  *
76  ***/
77 SilcBool silc_mutex_alloc(SilcMutex *mutex);
78
79 /****f* silcutil/SilcMutexAPI/silc_mutex_free
80  *
81  * SYNOPSIS
82  *
83  *    void silc_mutex_free(SilcMutex mutex);
84  *
85  * DESCRIPTION
86  *
87  *    Free SILC Mutex object and frees all allocated memory.  If `mutex'
88  *    is NULL this function has no effect.
89  *
90  ***/
91 void silc_mutex_free(SilcMutex mutex);
92
93 /****f* silcutil/SilcMutexAPI/silc_mutex_lock
94  *
95  * SYNOPSIS
96  *
97  *    void silc_mutex_lock(SilcMutex mutex);
98  *
99  * DESCRIPTION
100  *
101  *    Locks the mutex. If the mutex is locked by another thread the
102  *    current thread will block until the other thread has issued
103  *    silc_mutex_unlock for the mutex.  If `mutex' is NULL this function
104  *    has no effect.
105  *
106  * NOTES
107  *
108  *    The caller must not call silc_mutex_lock for mutex that has been
109  *    already locked in the current thread.  In this case deadlock will
110  *    occur.
111  *
112  ***/
113 void silc_mutex_lock(SilcMutex mutex);
114
115 /****f* silcutil/SilcMutexAPI/silc_mutex_unlock
116  *
117  * SYNOPSIS
118  *
119  *    void silc_mutex_unlock(SilcMutex mutex);
120  *
121  * DESCRIPTION
122  *
123  *    Unlocks the mutex and thus releases it for another thread that
124  *    may be waiting for the lock.  If `mutex' is NULL this function
125  *    has no effect.
126  *
127  * NOTES
128  *
129  *    The caller must not call the silc_mutex_unlock for an unlocked
130  *    mutex or mutex not locked by the current thread.
131  *
132  ***/
133 void silc_mutex_unlock(SilcMutex mutex);
134
135 /****f* silcutil/SilcMutexAPI/silc_mutex_assert_locked
136  *
137  * SYNOPSIS
138  *
139  *    void silc_mutex_assert_locked(SilcMutex mutex);
140  *
141  * DESCRIPTION
142  *
143  *    Asserts that the `mutex' is locked.  It is fatal error if the mutex
144  *    is not locked.  If debugging is not compiled in this function has
145  *    no effect (SILC_DEBUG define).
146  *
147  ***/
148 void silc_mutex_assert_locked(SilcMutex mutex);
149
150 /****f* silcutil/SilcMutexAPI/silc_rwlock_alloc
151  *
152  * SYNOPSIS
153  *
154  *    SilcBool silc_rwlock_alloc(SilcRwLock *rwlock);
155  *
156  * DESCRIPTION
157  *
158  *    Allocates SILC read/write lock.  The read/write lock must be allocated
159  *    before it can be used.  It is freed by the silc_rwlock_free function.
160  *    This returns TRUE and allocated read/write lock in to the `rwlock' and
161  *    FALSE on error.
162  *
163  ***/
164 SilcBool silc_rwlock_alloc(SilcRwLock *rwlock);
165
166 /****f* silcutil/SilcRwLockAPI/silc_rwlock_free
167  *
168  * SYNOPSIS
169  *
170  *    void silc_rwlock_free(SilcRwLock rwlock);
171  *
172  * DESCRIPTION
173  *
174  *    Free SILC Rwlock object and frees all allocated memory.  If `rwlock'
175  *    is NULL this function has no effect.
176  *
177  ***/
178 void silc_rwlock_free(SilcRwLock rwlock);
179
180 /****f* silcutil/SilcRwLockAPI/silc_rwlock_rdlock
181  *
182  * SYNOPSIS
183  *
184  *    void silc_rwlock_rdlock(SilcRwLock rwlock);
185  *
186  * DESCRIPTION
187  *
188  *    Acquires read lock of the read/write lock `rwlock'.  If the `rwlock'
189  *    is locked by a writer the current thread will block until the other
190  *    thread has issued silc_rwlock_unlock for the `rwlock'.  This function
191  *    may be called multiple times to acquire the read lock.  There must be
192  *    same amount of silc_rwlock_unlock calls.  If `rwlock' is NULL this
193  *    function has no effect.
194  *
195  ***/
196 void silc_rwlock_rdlock(SilcRwLock rwlock);
197
198 /****f* silcutil/SilcRwLockAPI/silc_rwlock_wrlock
199  *
200  * SYNOPSIS
201  *
202  *    void silc_rwlock_wrlock(SilcRwLock rwlock);
203  *
204  * DESCRIPTION
205  *
206  *    Acquires write lock of the read/write lock `rwlock'.  If the `rwlock'
207  *    is locked by a writer or a reader the current thread will block until
208  *    the other thread(s) have issued silc_rwlock_unlock for the `rwlock'.
209  *    If `rwlock' is NULL this function has no effect.
210  *
211  ***/
212 void silc_rwlock_wrlock(SilcRwLock rwlock);
213
214 /****f* silcutil/SilcRwLockAPI/silc_rwlock_unlock
215  *
216  * SYNOPSIS
217  *
218  *    void silc_rwlock_unlock(SilcRwLock rwlock);
219  *
220  * DESCRIPTION
221  *
222  *    Releases the lock of the read/write lock `rwlock'.  If `rwlock' was
223  *    locked by a writer this will release the writer lock.  Otherwise this
224  *    releases the reader lock.  If `rwlock' is NULL this function has no
225  *    effect.
226  *
227  ***/
228 void silc_rwlock_unlock(SilcRwLock rwlock);
229
230 #endif