2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Tilghman Lesher
6 * Tilghman Lesher <func_lock_2007@the-tilghman.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Dialplan mutexes
23 * \author Tilghman Lesher <func_lock_2007@the-tilghman.com>
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/lock.h"
34 #include "asterisk/file.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/linkedlists.h"
41 <function name="LOCK" language="en_US">
43 Attempt to obtain a named mutex.
46 <parameter name="lockname" required="true" />
49 <para>Attempts to grab a named lock exclusively, and prevents other channels from
50 obtaining the same lock. LOCK will wait for the lock to become available.
51 Returns <literal>1</literal> if the lock was obtained or <literal>0</literal> on error.</para>
52 <note><para>To avoid the possibility of a deadlock, LOCK will only attempt to
53 obtain the lock for 3 seconds if the channel already has another lock.</para></note>
56 <function name="TRYLOCK" language="en_US">
58 Attempt to obtain a named mutex.
61 <parameter name="lockname" required="true" />
64 <para>Attempts to grab a named lock exclusively, and prevents other channels
65 from obtaining the same lock. Returns <literal>1</literal> if the lock was
66 available or <literal>0</literal> otherwise.</para>
69 <function name="UNLOCK" language="en_US">
71 Unlocks a named mutex.
74 <parameter name="lockname" required="true" />
77 <para>Unlocks a previously locked mutex. Returns <literal>1</literal> if the channel
78 had a lock or <literal>0</literal> otherwise.</para>
79 <note><para>It is generally unnecessary to unlock in a hangup routine, as any locks
80 held are automatically freed when the channel is destroyed.</para></note>
87 static AST_LIST_HEAD_STATIC(locklist, lock_frame);
89 static void lock_free(void *data);
90 static int unloading = 0;
92 static struct ast_datastore_info lock_info = {
98 AST_LIST_ENTRY(lock_frame) entries;
100 /*! count is needed so if a recursive mutex exits early, we know how many times to unlock it. */
103 struct ast_channel *channel;
104 /*! name of the lock */
108 struct channel_lock_frame {
109 AST_LIST_ENTRY(channel_lock_frame) list;
110 /*! Need to save channel pointer here, because during destruction, we won't have it. */
111 struct ast_channel *channel;
112 struct lock_frame *lock_frame;
115 static void lock_free(void *data)
117 AST_LIST_HEAD(, channel_lock_frame) *oldlist = data;
118 struct channel_lock_frame *clframe;
119 AST_LIST_LOCK(oldlist);
120 while ((clframe = AST_LIST_REMOVE_HEAD(oldlist, list))) {
121 /* Only unlock if we own the lock */
122 if (clframe->channel == clframe->lock_frame->channel) {
123 clframe->lock_frame->channel = NULL;
124 while (clframe->lock_frame->count > 0) {
125 clframe->lock_frame->count--;
126 ast_mutex_unlock(&clframe->lock_frame->mutex);
131 AST_LIST_UNLOCK(oldlist);
132 AST_LIST_HEAD_DESTROY(oldlist);
136 static int get_lock(struct ast_channel *chan, char *lockname, int try)
138 struct ast_datastore *lock_store = ast_channel_datastore_find(chan, &lock_info, NULL);
139 struct lock_frame *current;
140 struct channel_lock_frame *clframe = NULL, *save_clframe = NULL;
141 AST_LIST_HEAD(, channel_lock_frame) *list;
142 int res, count_channel_locks = 0;
145 ast_debug(1, "Channel %s has no lock datastore, so we're allocating one.\n", chan->name);
146 lock_store = ast_datastore_alloc(&lock_info, NULL);
148 ast_log(LOG_ERROR, "Unable to allocate new datastore. No locks will be obtained.\n");
152 list = ast_calloc(1, sizeof(*list));
154 ast_log(LOG_ERROR, "Unable to allocate datastore list head. %sLOCK will fail.\n", try ? "TRY" : "");
155 ast_datastore_free(lock_store);
159 lock_store->data = list;
160 AST_LIST_HEAD_INIT(list);
161 ast_channel_datastore_add(chan, lock_store);
163 list = lock_store->data;
165 /* Lock already exists? */
166 AST_LIST_LOCK(&locklist);
167 AST_LIST_TRAVERSE(&locklist, current, entries) {
168 if (strcmp(current->name, lockname) == 0) {
176 AST_LIST_UNLOCK(&locklist);
180 /* Create new lock entry */
181 current = ast_calloc(1, sizeof(*current) + strlen(lockname) + 1);
183 AST_LIST_UNLOCK(&locklist);
187 strcpy((char *)current + sizeof(*current), lockname);
188 ast_mutex_init(¤t->mutex);
189 AST_LIST_INSERT_TAIL(&locklist, current, entries);
191 AST_LIST_UNLOCK(&locklist);
193 /* Found lock or created one - now find or create the corresponding link in the channel */
195 AST_LIST_TRAVERSE(list, clframe, list) {
196 if (clframe->lock_frame == current)
197 save_clframe = clframe;
199 /* Only count mutexes that we currently hold */
200 if (clframe->lock_frame->channel == chan)
201 count_channel_locks++;
205 clframe = save_clframe;
209 AST_LIST_UNLOCK(list);
213 clframe = ast_calloc(1, sizeof(*clframe));
215 ast_log(LOG_ERROR, "Unable to allocate channel lock frame. %sLOCK will fail.\n", try ? "TRY" : "");
216 AST_LIST_UNLOCK(list);
220 clframe->lock_frame = current;
221 clframe->channel = chan;
222 /* Count the lock just created */
223 count_channel_locks++;
224 AST_LIST_INSERT_TAIL(list, clframe, list);
226 AST_LIST_UNLOCK(list);
228 /* Okay, we have both frames, so now we need to try to lock the mutex. */
229 if (count_channel_locks > 1) {
230 struct timeval start = ast_tvnow();
232 if ((res = ast_mutex_trylock(¤t->mutex)) == 0)
234 if (ast_tvdiff_ms(ast_tvnow(), start) > 3000)
235 break; /* bail after 3 seconds of waiting */
239 /* If the channel doesn't have any locks so far, then there's no possible deadlock. */
240 res = try ? ast_mutex_trylock(¤t->mutex) : ast_mutex_lock(¤t->mutex);
245 current->channel = chan;
251 static int unlock_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
253 struct ast_datastore *lock_store = ast_channel_datastore_find(chan, &lock_info, NULL);
254 struct channel_lock_frame *clframe;
255 AST_LIST_HEAD(, channel_lock_frame) *list;
258 ast_log(LOG_WARNING, "No datastore for dialplan locks. Nothing was ever locked!\n");
259 ast_copy_string(buf, "0", len);
263 if (!(list = lock_store->data)) {
264 ast_debug(1, "This should NEVER happen\n");
265 ast_copy_string(buf, "0", len);
269 /* Find item in the channel list */
271 AST_LIST_TRAVERSE(list, clframe, list) {
272 if (clframe->lock_frame && clframe->lock_frame->channel == chan && strcmp(clframe->lock_frame->name, data) == 0) {
276 /* We never destroy anything until channel destruction, which will never
277 * happen while this routine is executing, so we don't need to hold the
278 * lock beyond this point. */
279 AST_LIST_UNLOCK(list);
282 /* We didn't have this lock in the first place */
283 ast_copy_string(buf, "0", len);
287 /* Decrement before we release, because if a channel is waiting on the
288 * mutex, there's otherwise a race to alter count. */
289 clframe->lock_frame->count--;
290 /* If we get another lock, this one shouldn't count against us for deadlock avoidance. */
291 clframe->lock_frame->channel = NULL;
292 ast_mutex_unlock(&clframe->lock_frame->mutex);
294 ast_copy_string(buf, "1", len);
298 static int lock_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
301 ast_autoservice_start(chan);
303 ast_copy_string(buf, get_lock(chan, data, 0) ? "0" : "1", len);
306 ast_autoservice_stop(chan);
311 static int trylock_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
314 ast_autoservice_start(chan);
316 ast_copy_string(buf, get_lock(chan, data, 1) ? "0" : "1", len);
319 ast_autoservice_stop(chan);
324 static struct ast_custom_function lock_function = {
330 static struct ast_custom_function trylock_function = {
332 .read = trylock_read,
336 static struct ast_custom_function unlock_function = {
342 static int unload_module(void)
344 struct lock_frame *current;
349 AST_LIST_LOCK(&locklist);
350 while ((current = AST_LIST_REMOVE_HEAD(&locklist, entries))) {
351 /* If any locks are currently in use, then we cannot unload this module */
352 if (current->channel) {
354 AST_LIST_INSERT_HEAD(&locklist, current, entries);
355 AST_LIST_UNLOCK(&locklist);
359 ast_mutex_destroy(¤t->mutex);
363 /* No locks left, unregister functions */
364 ast_custom_function_unregister(&lock_function);
365 ast_custom_function_unregister(&trylock_function);
366 ast_custom_function_unregister(&unlock_function);
368 AST_LIST_UNLOCK(&locklist);
372 static int load_module(void)
374 int res = ast_custom_function_register(&lock_function);
375 res |= ast_custom_function_register(&trylock_function);
376 res |= ast_custom_function_register(&unlock_function);
380 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialplan mutexes");