/*! the list of registered channel types */
static AST_LIST_HEAD_NOLOCK_STATIC(backends, chanlist);
-/*! the list of channels we have */
-static struct ast_channel *channels = NULL;
-
-/*! Protect the channel list, both backends and channels. */
-AST_MUTEX_DEFINE_STATIC(chlock);
+/*! the list of channels we have. Note that the lock for this list is used for
+ both the channels list and the backends list. */
+static AST_LIST_HEAD_STATIC(channels, ast_channel);
/*! map AST_CAUSE's to readable string representations */
const struct ast_cause {
ast_cli(fd, FORMAT, "Type", "Description", "Devicestate", "Indications", "Transfer");
ast_cli(fd, FORMAT, "----------", "-----------", "-----------", "-----------", "--------");
- if (ast_mutex_lock(&chlock)) {
+ if (AST_LIST_LOCK(&channels)) {
ast_log(LOG_WARNING, "Unable to lock channel list\n");
return -1;
}
(cl->tech->transfer) ? "yes" : "no");
count_chan++;
}
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
ast_cli(fd, "----------\n%d channel drivers registered.\n", count_chan);
return RESULT_SUCCESS;
if (argc != 3)
return RESULT_SHOWUSAGE;
- if (ast_mutex_lock(&chlock)) {
+ if (AST_LIST_LOCK(&channels)) {
ast_log(LOG_WARNING, "Unable to lock channel list\n");
return RESULT_FAILURE;
}
if (!cl) {
ast_cli(fd, "\n%s is not a registered channel driver.\n", argv[2]);
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
return RESULT_FAILURE;
}
);
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
return RESULT_SUCCESS;
}
struct ast_channel *c;
shutting_down = 1;
if (hangup) {
- ast_mutex_lock(&chlock);
- for (c = channels; c; c = c->next)
+ AST_LIST_LOCK(&channels);
+ AST_LIST_TRAVERSE(&channels, c, list)
ast_softhangup(c, AST_SOFTHANGUP_SHUTDOWN);
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
}
}
{
struct ast_channel *c;
int cnt = 0;
- ast_mutex_lock(&chlock);
- for (c = channels; c; c = c->next)
+ AST_LIST_LOCK(&channels);
+ AST_LIST_TRAVERSE(&channels, c, list)
cnt++;
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
return cnt;
}
{
struct chanlist *chan;
- ast_mutex_lock(&chlock);
+ AST_LIST_LOCK(&channels);
AST_LIST_TRAVERSE(&backends, chan, list) {
if (!strcasecmp(tech->type, chan->tech->type)) {
ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
return -1;
}
}
chan = malloc(sizeof(*chan));
if (!chan) {
ast_log(LOG_WARNING, "Out of memory\n");
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
return -1;
}
chan->tech = tech;
ast_verbose(VERBOSE_PREFIX_2 "Registered channel type '%s' (%s)\n", chan->tech->type,
chan->tech->description);
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
return 0;
}
if (option_debug)
ast_log(LOG_DEBUG, "Unregistering channel type '%s'\n", tech->type);
- ast_mutex_lock(&chlock);
+ AST_LIST_LOCK(&channels);
AST_LIST_TRAVERSE_SAFE_BEGIN(&backends, chan, list) {
if (chan->tech == tech) {
}
AST_LIST_TRAVERSE_SAFE_END
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
}
const struct ast_channel_tech *ast_get_channel_tech(const char *name)
struct chanlist *chanls;
const struct ast_channel_tech *ret = NULL;
- if (ast_mutex_lock(&chlock)) {
+ if (AST_LIST_LOCK(&channels)) {
ast_log(LOG_WARNING, "Unable to lock channel tech list\n");
return NULL;
}
}
}
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
return ret;
}
tmp->tech = &null_tech;
- ast_mutex_lock(&chlock);
- tmp->next = channels;
- channels = tmp;
-
- ast_mutex_unlock(&chlock);
+ AST_LIST_LOCK(&channels);
+ AST_LIST_INSERT_HEAD(&channels, tmp, list);
+ AST_LIST_UNLOCK(&channels);
return tmp;
}
struct ast_channel *c;
for (retries = 0; retries < 10; retries++) {
- ast_mutex_lock(&chlock);
- for (c = channels; c; c = c->next) {
+ AST_LIST_LOCK(&channels);
+ AST_LIST_TRAVERSE(&channels, c, list) {
if (!prev) {
/* want head of list */
if (!name && !exten)
break;
}
} else if (c == prev) { /* found, return c->next */
- c = c->next;
+ c = AST_LIST_NEXT(c, list);
break;
}
}
/* this is slightly unsafe, as we _should_ hold the lock to access c->name */
if (!done && c)
ast_log(LOG_DEBUG, "Avoiding %s for '%s'\n", msg, c->name);
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
if (done)
return c;
usleep(1);
/*! \brief Free a channel structure */
void ast_channel_free(struct ast_channel *chan)
{
- struct ast_channel *last=NULL, *cur;
int fd;
struct ast_var_t *vardata;
struct ast_frame *f, *fp;
headp=&chan->varshead;
- ast_mutex_lock(&chlock);
- for (cur = channels; cur; cur = cur->next) {
- if (cur == chan) {
- if (last)
- last->next = cur->next;
- else
- channels = cur->next;
- break;
- }
- last = cur;
- }
- if (!cur)
- ast_log(LOG_WARNING, "Unable to find channel in list\n");
- else {
- /* Lock and unlock the channel just to be sure nobody
- has it locked still */
- ast_mutex_lock(&cur->lock);
- ast_mutex_unlock(&cur->lock);
- }
+ AST_LIST_LOCK(&channels);
+ AST_LIST_REMOVE(&channels, chan, list);
+ /* Lock and unlock the channel just to be sure nobody
+ has it locked still */
+ ast_mutex_lock(&chan->lock);
+ ast_mutex_unlock(&chan->lock);
if (chan->tech_pvt) {
ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n", chan->name);
free(chan->tech_pvt);
ast_var_delete(vardata);
free(chan);
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
ast_device_state_changed_literal(name);
}
cause = &foo;
*cause = AST_CAUSE_NOTDEFINED;
- if (ast_mutex_lock(&chlock)) {
+ if (AST_LIST_LOCK(&channels)) {
ast_log(LOG_WARNING, "Unable to lock channel list\n");
return NULL;
}
res = ast_translator_best_choice(&fmt, &capabilities);
if (res < 0) {
ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %d) to %d\n", type, chan->tech->capabilities, format);
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
return NULL;
}
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
if (!chan->tech->requester)
return NULL;
ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
*cause = AST_CAUSE_NOSUCHDRIVER;
- ast_mutex_unlock(&chlock);
+ AST_LIST_UNLOCK(&channels);
return NULL;
}