ARI: Fix a crash caused by hanging during playback to a channel in a bridge
[asterisk/asterisk.git] / res / stasis / command.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@digium.com>
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 /*! \file
20  *
21  * \brief Stasis application command support.
22  *
23  * \author David M. Lee, II <dlee@digium.com>
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include "command.h"
31
32 #include "asterisk/lock.h"
33 #include "asterisk/stasis_app_impl.h"
34
35 struct stasis_app_command {
36         ast_mutex_t lock;
37         ast_cond_t condition;
38         stasis_app_command_cb callback;
39         void *data;
40         command_data_destructor_fn data_destructor;
41         int retval;
42         int is_done:1;
43 };
44
45 static void command_dtor(void *obj)
46 {
47         struct stasis_app_command *command = obj;
48
49         if (command->data_destructor) {
50                 command->data_destructor(command->data);
51         }
52
53         ast_mutex_destroy(&command->lock);
54         ast_cond_destroy(&command->condition);
55 }
56
57 struct stasis_app_command *command_create(
58         stasis_app_command_cb callback, void *data, command_data_destructor_fn data_destructor)
59 {
60         struct stasis_app_command *command;
61
62         command = ao2_alloc(sizeof(*command), command_dtor);
63         if (!command) {
64                 if (data_destructor) {
65                         data_destructor(data);
66                 }
67                 return NULL;
68         }
69
70         ast_mutex_init(&command->lock);
71         ast_cond_init(&command->condition, 0);
72         command->callback = callback;
73         command->data = data;
74         command->data_destructor = data_destructor;
75
76         return command;
77 }
78
79 void command_complete(struct stasis_app_command *command, int retval)
80 {
81         SCOPED_MUTEX(lock, &command->lock);
82
83         command->is_done = 1;
84         command->retval = retval;
85         ast_cond_signal(&command->condition);
86 }
87
88 int command_join(struct stasis_app_command *command)
89 {
90         SCOPED_MUTEX(lock, &command->lock);
91         while (!command->is_done) {
92                 ast_cond_wait(&command->condition, &command->lock);
93         }
94
95         return command->retval;
96 }
97
98 void command_invoke(struct stasis_app_command *command,
99         struct stasis_app_control *control, struct ast_channel *chan)
100 {
101         int retval = command->callback(control, chan, command->data);
102         if (command->data_destructor) {
103                 command->data_destructor(command->data);
104                 command->data_destructor = NULL;
105         }
106         command_complete(command, retval);
107 }
108
109 static void command_queue_prestart_destroy(void *obj)
110 {
111         /* Clean up the container */
112         ao2_cleanup(obj);
113 }
114
115 static const struct ast_datastore_info command_queue_prestart = {
116         .type = "stasis-command-prestart-queue",
117         .destroy = command_queue_prestart_destroy,
118 };
119
120 int command_prestart_queue_command(struct ast_channel *chan,
121         stasis_app_command_cb command_fn, void *data, command_data_destructor_fn data_destructor)
122 {
123         struct ast_datastore *datastore;
124         struct ao2_container *command_queue;
125         RAII_VAR(struct stasis_app_command *, command,
126                 command_create(command_fn, data, data_destructor), ao2_cleanup);
127
128         if (!command) {
129                 return -1;
130         }
131
132         datastore = ast_channel_datastore_find(chan, &command_queue_prestart, NULL);
133         if (datastore) {
134                 command_queue = datastore->data;
135                 ao2_link(command_queue, command);
136                 return 0;
137         }
138
139         command_queue = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL);
140         if (!command_queue) {
141                 return -1;
142         }
143
144         datastore = ast_datastore_alloc(&command_queue_prestart, NULL);
145         if (!datastore) {
146                 ao2_cleanup(command_queue);
147                 return -1;
148         }
149         ast_channel_datastore_add(chan, datastore);
150
151         datastore->data = command_queue;
152         ao2_link(command_queue, command);
153
154         return 0;
155 }
156
157 struct ao2_container *command_prestart_get_container(struct ast_channel *chan)
158 {
159         struct ast_datastore *datastore = ast_channel_datastore_find(chan, &command_queue_prestart, NULL);
160
161         if (!datastore) {
162                 return NULL;
163         }
164
165         return ao2_bump(datastore->data);
166 }