5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 - 2007 Pekka Riikonen
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.
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.
20 /****h* silcutil/SILC Mutex Interface
24 * Interface for mutual exclusion locks and read/write locks. This is
25 * platform independent interface for applications that need concurrency
33 /****s* silcutil/SilcMutexAPI/SilcMutex
37 * typedef struct SilcMutexStruct *SilcMutex;
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.
46 typedef struct SilcMutexStruct *SilcMutex;
48 /****s* silcutil/SilcMutexAPI/SilcRwLock
52 * typedef struct SilcRwLockStruct *SilcRwLock;
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.
61 typedef struct SilcRwLockStruct *SilcRwLock;
63 /****f* silcutil/SilcMutexAPI/silc_mutex_alloc
67 * SilcBool silc_mutex_alloc(SilcMutex *mutex);
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. If threads support is not compiled in this returns FALSE,
75 * but should not be considered as an error.
78 SilcBool silc_mutex_alloc(SilcMutex *mutex);
80 /****f* silcutil/SilcMutexAPI/silc_mutex_free
84 * void silc_mutex_free(SilcMutex mutex);
88 * Free SILC Mutex object and frees all allocated memory. If `mutex'
89 * is NULL this function has no effect.
92 void silc_mutex_free(SilcMutex mutex);
94 /****f* silcutil/SilcMutexAPI/silc_mutex_lock
98 * void silc_mutex_lock(SilcMutex mutex);
102 * Locks the mutex. If the mutex is locked by another thread the
103 * current thread will block until the other thread has issued
104 * silc_mutex_unlock for the mutex. If `mutex' is NULL this function
109 * The caller must not call silc_mutex_lock for mutex that has been
110 * already locked in the current thread. In this case deadlock will
114 void silc_mutex_lock(SilcMutex mutex);
116 /****f* silcutil/SilcMutexAPI/silc_mutex_unlock
120 * void silc_mutex_unlock(SilcMutex mutex);
124 * Unlocks the mutex and thus releases it for another thread that
125 * may be waiting for the lock. If `mutex' is NULL this function
130 * The caller must not call the silc_mutex_unlock for an unlocked
131 * mutex or mutex not locked by the current thread.
134 void silc_mutex_unlock(SilcMutex mutex);
136 /****f* silcutil/SilcMutexAPI/silc_mutex_assert_locked
140 * void silc_mutex_assert_locked(SilcMutex mutex);
144 * Asserts that the `mutex' is locked. It is fatal error if the mutex
145 * is not locked. If debugging is not compiled in this function has
146 * no effect (SILC_DEBUG define).
149 void silc_mutex_assert_locked(SilcMutex mutex);
151 /****f* silcutil/SilcMutexAPI/silc_rwlock_alloc
155 * SilcBool silc_rwlock_alloc(SilcRwLock *rwlock);
159 * Allocates SILC read/write lock. The read/write lock must be allocated
160 * before it can be used. It is freed by the silc_rwlock_free function.
161 * This returns TRUE and allocated read/write lock in to the `rwlock' and
165 SilcBool silc_rwlock_alloc(SilcRwLock *rwlock);
167 /****f* silcutil/SilcRwLockAPI/silc_rwlock_free
171 * void silc_rwlock_free(SilcRwLock rwlock);
175 * Free SILC Rwlock object and frees all allocated memory. If `rwlock'
176 * is NULL this function has no effect.
179 void silc_rwlock_free(SilcRwLock rwlock);
181 /****f* silcutil/SilcRwLockAPI/silc_rwlock_rdlock
185 * void silc_rwlock_rdlock(SilcRwLock rwlock);
189 * Acquires read lock of the read/write lock `rwlock'. If the `rwlock'
190 * is locked by a writer the current thread will block until the other
191 * thread has issued silc_rwlock_unlock for the `rwlock'. This function
192 * may be called multiple times to acquire the read lock. There must be
193 * same amount of silc_rwlock_unlock calls. If `rwlock' is NULL this
194 * function has no effect.
197 void silc_rwlock_rdlock(SilcRwLock rwlock);
199 /****f* silcutil/SilcRwLockAPI/silc_rwlock_wrlock
203 * void silc_rwlock_wrlock(SilcRwLock rwlock);
207 * Acquires write lock of the read/write lock `rwlock'. If the `rwlock'
208 * is locked by a writer or a reader the current thread will block until
209 * the other thread(s) have issued silc_rwlock_unlock for the `rwlock'.
210 * If `rwlock' is NULL this function has no effect.
213 void silc_rwlock_wrlock(SilcRwLock rwlock);
215 /****f* silcutil/SilcRwLockAPI/silc_rwlock_unlock
219 * void silc_rwlock_unlock(SilcRwLock rwlock);
223 * Releases the lock of the read/write lock `rwlock'. If `rwlock' was
224 * locked by a writer this will release the writer lock. Otherwise this
225 * releases the reader lock. If `rwlock' is NULL this function has no
229 void silc_rwlock_unlock(SilcRwLock rwlock);