Convert the ast_channel data structure over to the astobj2 framework.

There is a lot that could be said about this, but the patch is a big 
improvement for performance, stability, code maintainability, 
and ease of future code development.

The channel list is no longer an unsorted linked list.  The main container 
for channels is an astobj2 hash table.  All of the code related to searching 
for channels or iterating active channels has been rewritten.  Let n be 
the number of active channels.  Iterating the channel list has gone from 
O(n^2) to O(n).  Searching for a channel by name went from O(n) to O(1).  
Searching for a channel by extension is still O(n), but uses a new method 
for doing so, which is more efficient.

The ast_channel object is now a reference counted object.  The benefits 
here are plentiful.  Some benefits directly related to issues in the 
previous code include:

1) When threads other than the channel thread owning a channel wanted 
   access to a channel, it had to hold the lock on it to ensure that it didn't 
   go away.  This is no longer a requirement.  Holding a reference is 
   sufficient.

2) There are places that now require less dealing with channel locks.

3) There are places where channel locks are held for much shorter periods 
   of time.

4) There are places where dealing with more than one channel at a time becomes 
   _MUCH_ easier.  ChanSpy is a great example of this.  Writing code in the 
   future that deals with multiple channels will be much easier.

Some additional information regarding channel locking and reference count 
handling can be found in channel.h, where a new section has been added that 
discusses some of the rules associated with it.

Mark Michelson also assisted with the development of this patch.  He did the 
conversion of ChanSpy and introduced a new API, ast_autochan, which makes it 
much easier to deal with holding on to a channel pointer for an extended period 
of time and having it get automatically updated if the channel gets masqueraded.
Mark was also a huge help in the code review process.

Thanks to David Vossel for his assistance with this branch, as well.  David 
did the conversion of the DAHDIScan application by making it become a wrapper 
for ChanSpy internally.

The changes come from the svn/asterisk/team/russell/ast_channel_ao2 branch.

Review: http://reviewboard.digium.com/r/203/


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@190423 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Russell Bryant
2009-04-24 14:04:26 +00:00
parent f314c5f13f
commit cba19c8a67
42 changed files with 1732 additions and 1514 deletions

View File

@@ -352,7 +352,8 @@ static int func_channels_read(struct ast_channel *chan, const char *function, ch
regex_t re;
int res;
size_t buflen = 0;
struct ast_channel_iterator *iter;
buf[0] = '\0';
if (!ast_strlen_zero(data)) {
@@ -363,7 +364,15 @@ static int func_channels_read(struct ast_channel *chan, const char *function, ch
}
}
for (c = ast_channel_walk_locked(NULL); c; ast_channel_unlock(c), c = ast_channel_walk_locked(c)) {
if (!(iter = ast_channel_iterator_all_new(0))) {
if (!ast_strlen_zero(data)) {
regfree(&re);
}
return -1;
}
while ((c = ast_channel_iterator_next(iter))) {
ast_channel_lock(c);
if (ast_strlen_zero(data) || regexec(&re, c->name, 0, NULL, 0) == 0) {
size_t namelen = strlen(c->name);
if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
@@ -377,8 +386,12 @@ static int func_channels_read(struct ast_channel *chan, const char *function, ch
ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space. Output will be truncated!\n");
}
}
ast_channel_unlock(c);
c = ast_channel_unref(c);
}
ast_channel_iterator_destroy(iter);
if (!ast_strlen_zero(data)) {
regfree(&re);
}

View File

@@ -134,6 +134,7 @@ static int shared_read(struct ast_channel *chan, const char *cmd, char *data, ch
AST_APP_ARG(var);
AST_APP_ARG(chan);
);
struct ast_channel *c_ref = NULL;
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
@@ -145,15 +146,20 @@ static int shared_read(struct ast_channel *chan, const char *cmd, char *data, ch
if (!ast_strlen_zero(args.chan)) {
char *prefix = alloca(strlen(args.chan) + 2);
sprintf(prefix, "%s-", args.chan);
if (!(chan = ast_get_channel_by_name_locked(args.chan)) && !(chan = ast_get_channel_by_name_prefix_locked(prefix, strlen(prefix)))) {
if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' will be blank.\n", args.chan, args.var);
return -1;
}
} else
ast_channel_lock(chan);
chan = c_ref;
}
ast_channel_lock(chan);
if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
ast_channel_unlock(chan);
if (c_ref) {
c_ref = ast_channel_unref(c_ref);
}
return -1;
}
@@ -170,6 +176,10 @@ static int shared_read(struct ast_channel *chan, const char *cmd, char *data, ch
ast_channel_unlock(chan);
if (c_ref) {
c_ref = ast_channel_unref(c_ref);
}
return 0;
}
@@ -182,6 +192,7 @@ static int shared_write(struct ast_channel *chan, const char *cmd, char *data, c
AST_APP_ARG(var);
AST_APP_ARG(chan);
);
struct ast_channel *c_ref = NULL;
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
@@ -193,17 +204,22 @@ static int shared_write(struct ast_channel *chan, const char *cmd, char *data, c
if (!ast_strlen_zero(args.chan)) {
char *prefix = alloca(strlen(args.chan) + 2);
sprintf(prefix, "%s-", args.chan);
if (!(chan = ast_get_channel_by_name_locked(args.chan)) && !(chan = ast_get_channel_by_name_prefix_locked(prefix, strlen(prefix)))) {
if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' not set to '%s'.\n", args.chan, args.var, value);
return -1;
}
} else
ast_channel_lock(chan);
chan = c_ref;
}
ast_channel_lock(chan);
if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
if (!(varstore = ast_datastore_alloc(&shared_variable_info, NULL))) {
ast_log(LOG_ERROR, "Unable to allocate new datastore. Shared variable not set.\n");
ast_channel_unlock(chan);
if (c_ref) {
c_ref = ast_channel_unref(c_ref);
}
return -1;
}
@@ -211,6 +227,9 @@ static int shared_write(struct ast_channel *chan, const char *cmd, char *data, c
ast_log(LOG_ERROR, "Unable to allocate variable structure. Shared variable not set.\n");
ast_datastore_free(varstore);
ast_channel_unlock(chan);
if (c_ref) {
c_ref = ast_channel_unref(c_ref);
}
return -1;
}
@@ -241,6 +260,10 @@ static int shared_write(struct ast_channel *chan, const char *cmd, char *data, c
ast_channel_unlock(chan);
if (c_ref) {
c_ref = ast_channel_unref(c_ref);
}
return 0;
}

View File

@@ -230,18 +230,22 @@ static int acf_import(struct ast_channel *chan, const char *cmd, char *data, cha
AST_APP_ARG(varname);
);
AST_STANDARD_APP_ARGS(args, data);
buf[0] = 0;
buf[0] = '\0';
if (!ast_strlen_zero(args.varname)) {
struct ast_channel *chan2 = ast_get_channel_by_name_locked(args.channel);
if (chan2) {
struct ast_channel *chan2;
if ((chan2 = ast_channel_get_by_name(args.channel))) {
char *s = alloca(strlen(args.varname) + 4);
if (s) {
sprintf(s, "${%s}", args.varname);
pbx_substitute_variables_helper(chan2, s, buf, len);
}
sprintf(s, "${%s}", args.varname);
ast_channel_lock(chan2);
pbx_substitute_variables_helper(chan2, s, buf, len);
ast_channel_unlock(chan2);
chan2 = ast_channel_unref(chan2);
}
}
return 0;
}

View File

@@ -257,7 +257,7 @@ static int acf_odbc_write(struct ast_channel *chan, const char *cmd, char *s, co
if (chan)
ast_autoservice_stop(chan);
if (bogus_chan) {
ast_channel_free(chan);
ast_channel_release(chan);
} else {
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
}
@@ -367,7 +367,7 @@ static int acf_odbc_write(struct ast_channel *chan, const char *cmd, char *s, co
if (chan)
ast_autoservice_stop(chan);
if (bogus_chan)
ast_channel_free(chan);
ast_channel_release(chan);
return 0;
}
@@ -473,7 +473,7 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
ast_autoservice_stop(chan);
}
if (bogus_chan) {
ast_channel_free(chan);
ast_channel_release(chan);
}
return -1;
}
@@ -490,7 +490,7 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
ast_autoservice_stop(chan);
}
if (bogus_chan) {
ast_channel_free(chan);
ast_channel_release(chan);
}
return -1;
}
@@ -517,7 +517,7 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
if (chan)
ast_autoservice_stop(chan);
if (bogus_chan)
ast_channel_free(chan);
ast_channel_release(chan);
return res1;
}
@@ -560,7 +560,7 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
if (chan)
ast_autoservice_stop(chan);
if (bogus_chan)
ast_channel_free(chan);
ast_channel_release(chan);
return -1;
}
resultset = tmp;
@@ -656,7 +656,7 @@ end_acf_read:
if (chan)
ast_autoservice_stop(chan);
if (bogus_chan)
ast_channel_free(chan);
ast_channel_release(chan);
return -1;
}
odbc_store->data = resultset;
@@ -669,7 +669,7 @@ end_acf_read:
if (chan)
ast_autoservice_stop(chan);
if (bogus_chan)
ast_channel_free(chan);
ast_channel_release(chan);
return 0;
}
@@ -1059,7 +1059,7 @@ static char *cli_odbc_read(struct ast_cli_entry *e, int cmd, struct ast_cli_args
}
ast_str_substitute_variables(&sql, 0, chan, query->sql_read);
ast_channel_free(chan);
chan = ast_channel_release(chan);
if (a->argc == 5 && !strcmp(a->argv[4], "exec")) {
/* Execute the query */
@@ -1272,7 +1272,7 @@ static char *cli_odbc_write(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
pbx_builtin_pushvar_helper(chan, "VALUE", S_OR(a->argv[4], ""));
ast_str_substitute_variables(&sql, 0, chan, query->sql_write);
ast_debug(1, "SQL is %s\n", ast_str_buffer(sql));
ast_channel_free(chan);
chan = ast_channel_release(chan);
if (a->argc == 6 && !strcmp(a->argv[5], "exec")) {
/* Execute the query */