Debian Codegraph @

main/autochan.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2009, Digium, Inc.
5 *
6 * Mark Michelson <[email protected]>
7 *
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.
13 *
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.
17 */
18
19 /*!
20 * \file
21 * \brief "smart" channels
22 *
23 * \author Mark Michelson <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/autochan.h"
33 #include "asterisk/utils.h"
34 #include "asterisk/linkedlists.h"
35 #include "asterisk/options.h"
36 #include "asterisk/channel.h"
37
38 struct ast_autochan *ast_autochan_setup(struct ast_channel *chan)
39 {
40 struct ast_autochan *autochan;
41
42 if (!chan) {
43 return NULL;
44 }
45
46 if (!(autochan = ast_calloc(1, sizeof(*autochan)))) {
47 return NULL;
48 }
49 ast_mutex_init(&autochan->lock);
50
51 autochan->chan = ast_channel_ref(chan);
52
53 ast_debug(1, "Created autochan %p to hold channel %s (%p)\n",
54 autochan, ast_channel_name(chan), chan);
55
56 /* autochan is still private, no need for ast_autochan_channel_lock() */
57 ast_channel_lock(autochan->chan);
58 AST_LIST_INSERT_TAIL(ast_channel_autochans(autochan->chan), autochan, list);
59 ast_channel_unlock(autochan->chan);
60
61 return autochan;
62 }
63
64 void ast_autochan_destroy(struct ast_autochan *autochan)
65 {
66 struct ast_autochan *autochan_iter;
67
68 ast_autochan_channel_lock(autochan);
69 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_autochans(autochan->chan), autochan_iter, list) {
70 if (autochan_iter == autochan) {
71 AST_LIST_REMOVE_CURRENT(list);
72 ast_debug(1, "Removed autochan %p from the list, about to free it\n", autochan);
73 break;
74 }
75 }
76 AST_LIST_TRAVERSE_SAFE_END;
77 ast_autochan_channel_unlock(autochan);
78
79 autochan->chan = ast_channel_unref(autochan->chan);
80
81 ast_mutex_destroy(&autochan->lock);
82
83 ast_free(autochan);
84 }
85
86 void ast_autochan_new_channel(struct ast_channel *old_chan, struct ast_channel *new_chan)
87 {
88 struct ast_autochan *autochan;
89
90 AST_LIST_APPEND_LIST(ast_channel_autochans(new_chan), ast_channel_autochans(old_chan), list);
91
92 /* Deadlock avoidance is not needed since the channels are already locked. */
93 AST_LIST_TRAVERSE(ast_channel_autochans(new_chan), autochan, list) {
94 ast_mutex_lock(&autochan->lock);
95 if (autochan->chan == old_chan) {
96 autochan->chan = ast_channel_ref(new_chan);
97 ast_channel_unref(old_chan);
98
99 ast_debug(1, "Autochan %p used to hold channel %s (%p) but now holds channel %s (%p)\n",
100 autochan, ast_channel_name(old_chan), old_chan, ast_channel_name(new_chan), new_chan);
101 }
102 ast_mutex_unlock(&autochan->lock);
103 }
104 }