5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2006 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 Conditional Variable Interface
24 * A conditional variable interface for multi-thread synchronization.
25 * Conditional variables enable threads to suspend execution and yield
26 * the processors until some predicate on some shared data is satisfied.
33 /****s* silcutil/SilcCondAPI/SilcCond
37 * typedef struct SilcCondStruct *SilcCond;
41 * This context is the actual conditional variable and is allocated
42 * by silc_cond_alloc and given as argument to all silc_cond_*
43 * functions. It is freed by the silc_cond_free function.
46 typedef struct SilcCondStruct *SilcCond;
48 /****s* silcutil/SilcCondAPI/silc_cond_alloc
52 * SilcBool silc_cond_alloc(SilcCond *cond);
56 * Allocates SILC Conditional variable context. The conditional must
57 * be allocated before it can be used. It is freed by the
58 * silc_cond_free function. This returns TRUE and allocated
59 * conditional in to the `cond' pointer and FALSE on error.
62 SilcBool silc_cond_alloc(SilcCond *cond);
64 /****s* silcutil/SilcCondAPI/silc_cond_free
68 * void silc_cond_free(SilcCond cond);
72 * Free conditional variable context. If `cond' is NULL this function
76 void silc_cond_free(SilcCond cond);
78 /****s* silcutil/SilcCondAPI/silc_cond_wait
82 * void silc_cond_wait(SilcCond cond, SilcMutex mutex);
86 * Waits for conditional variable `cond' to be signalled. This function
87 * will block the calling thread until the conditional variable is
88 * signalled. The `mutex' must be locked before calling this function.
89 * The `mutex' will be unlocked inside this function. After this
90 * function returns the `mutex' is in locked state again.
94 * silc_mutex_lock(lock);
95 * while (c->a == NULL)
96 * silc_cond_wait(cond, lock);
98 * silc_mutex_unlock(lock);
101 void silc_cond_wait(SilcCond cond, SilcMutex mutex);
103 /****s* silcutil/SilcCondAPI/silc_cond_timedwait
107 * void silc_cond_timedwait(SilcCond cond, SilcMutex mutex, int timeout);
111 * Waits for conditional variable `cond' to be signalled or for the
112 * `timeout' to expire. The timeout is in milliseconds. If it is 0
113 * no timeout exist. Returns FALSE if timeout expired, TRUE when
114 * signalled. This function will block the calling thread until the
115 * conditional variable is signalled. The `mutex' must be locked before
116 * calling this function. The `mutex' will be unlocked inside this
117 * function. After this function returns the `mutex' is in locked
121 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex, int timeout);
123 /****s* silcutil/SilcCondAPI/silc_cond_signal
127 * void silc_cond_signal(SilcCond cond);
131 * Signals a waiting thread and wakes it up. If there are no waiters
132 * this function has no effect. In case of multiple waiters only one
133 * is signalled. To signal all of them use silc_cond_broadcast.
137 * Before calling this function the mutex used with the silc_cond_wait
142 * silc_mutex_lock(lock);
144 * silc_cond_signal(cond);
145 * silc_mutex_unlock(lock);
148 void silc_cond_signal(SilcCond cond);
150 /****s* silcutil/SilcCondAPI/silc_cond_broadcast
154 * void silc_cond_broadcast(SilcCond cond);
158 * Signals and wakes up all waiters. If there are no waiters this
159 * function has no effect.
163 * Before calling this function the mutex used with the silc_cond_wait
167 void silc_cond_broadcast(SilcCond cond);
169 #endif /* SILCCOND_H */