summaryrefslogtreecommitdiff
path: root/apps/app_directed_pickup.c
diff options
context:
space:
mode:
authorRussell Bryant <russell@russellbryant.com>2009-04-24 14:04:26 +0000
committerRussell Bryant <russell@russellbryant.com>2009-04-24 14:04:26 +0000
commitcba19c8a671c76a3969a7d36bc43444792a13a81 (patch)
tree1812569845aaf29df5f2a18285e73bc1fcc6268c /apps/app_directed_pickup.c
parentf314c5f13fd84bc2084e183e39b0d1fbec4b9a5a (diff)
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
Diffstat (limited to 'apps/app_directed_pickup.c')
-rw-r--r--apps/app_directed_pickup.c112
1 files changed, 65 insertions, 47 deletions
diff --git a/apps/app_directed_pickup.c b/apps/app_directed_pickup.c
index 5ce217d7a..0cf691c59 100644
--- a/apps/app_directed_pickup.c
+++ b/apps/app_directed_pickup.c
@@ -135,17 +135,35 @@ static int can_pickup(struct ast_channel *chan)
return 0;
}
+struct pickup_by_name_args {
+ const char *name;
+ size_t len;
+};
+
+static int pickup_by_name_cb(void *obj, void *arg, void *data, int flags)
+{
+ struct ast_channel *chan = obj;
+ struct pickup_by_name_args *args = data;
+
+ ast_channel_lock(chan);
+ if (!strncasecmp(chan->name, args->name, args->len) && can_pickup(chan)) {
+ /* Return with the channel still locked on purpose */
+ return CMP_MATCH | CMP_STOP;
+ }
+ ast_channel_unlock(chan);
+
+ return 0;
+}
+
/*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
static struct ast_channel *my_ast_get_channel_by_name_locked(const char *channame)
{
- struct ast_channel *chan;
char *chkchan;
- size_t channame_len, chkchan_len;
+ struct pickup_by_name_args pickup_args;
- channame_len = strlen(channame);
- chkchan_len = channame_len + 2;
+ pickup_args.len = strlen(channame) + 2;
- chkchan = alloca(chkchan_len);
+ chkchan = alloca(pickup_args.len);
/* need to append a '-' for the comparison so we check full channel name,
* i.e SIP/hgc- , use a temporary variable so original stays the same for
@@ -154,15 +172,9 @@ static struct ast_channel *my_ast_get_channel_by_name_locked(const char *channam
strcpy(chkchan, channame);
strcat(chkchan, "-");
- for (chan = ast_walk_channel_by_name_prefix_locked(NULL, channame, channame_len);
- chan;
- chan = ast_walk_channel_by_name_prefix_locked(chan, channame, channame_len)) {
- if (!strncasecmp(chan->name, chkchan, chkchan_len) && can_pickup(chan)) {
- return chan;
- }
- ast_channel_unlock(chan);
- }
- return NULL;
+ pickup_args.name = chkchan;
+
+ return ast_channel_callback(pickup_by_name_cb, NULL, &pickup_args, 0);
}
/*! \brief Attempt to pick up specified channel named , does not use context */
@@ -171,76 +183,82 @@ static int pickup_by_channel(struct ast_channel *chan, char *pickup)
int res = 0;
struct ast_channel *target;
- if (!(target = my_ast_get_channel_by_name_locked(pickup)))
+ if (!(target = my_ast_get_channel_by_name_locked(pickup))) {
return -1;
+ }
/* Just check that we are not picking up the SAME as target */
- if (chan->name != target->name && chan != target) {
+ if (chan != target) {
res = pickup_do(chan, target);
}
+
ast_channel_unlock(target);
+ target = ast_channel_unref(target);
return res;
}
-struct pickup_criteria {
- const char *exten;
- const char *context;
-};
-
-static int find_by_exten(struct ast_channel *c, void *data)
-{
- struct pickup_criteria *info = data;
-
- return (!strcasecmp(c->macroexten, info->exten) || !strcasecmp(c->exten, info->exten)) &&
- !strcasecmp(c->dialcontext, info->context) &&
- can_pickup(c);
-}
-
/* Attempt to pick up specified extension with context */
static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
{
struct ast_channel *target = NULL;
- struct pickup_criteria search = {
- .exten = exten,
- .context = context,
- };
+ struct ast_channel_iterator *iter;
+ int res = -1;
+
+ if (!(iter = ast_channel_iterator_by_exten_new(0, exten, context))) {
+ return -1;
+ }
- target = ast_channel_search_locked(find_by_exten, &search);
+ while ((target = ast_channel_iterator_next(iter))) {
+ ast_channel_lock(target);
+ if (can_pickup(target)) {
+ break;
+ }
+ ast_channel_unlock(target);
+ target = ast_channel_unref(target);
+ }
if (target) {
- int res = pickup_do(chan, target);
+ res = pickup_do(chan, target);
ast_channel_unlock(target);
- target = NULL;
- return res;
+ target = ast_channel_unref(target);
}
- return -1;
+ return res;
}
-static int find_by_mark(struct ast_channel *c, void *data)
+static int find_by_mark(void *obj, void *arg, void *data, int flags)
{
+ struct ast_channel *c = obj;
const char *mark = data;
const char *tmp;
+ int res;
+
+ ast_channel_lock(c);
- return (tmp = pbx_builtin_getvar_helper(c, PICKUPMARK)) &&
+ res = (tmp = pbx_builtin_getvar_helper(c, PICKUPMARK)) &&
!strcasecmp(tmp, mark) &&
can_pickup(c);
+
+ ast_channel_unlock(c);
+
+ return res ? CMP_MATCH | CMP_STOP : 0;
}
/* Attempt to pick up specified mark */
static int pickup_by_mark(struct ast_channel *chan, const char *mark)
{
- struct ast_channel *target = ast_channel_search_locked(find_by_mark, (char *) mark);
+ struct ast_channel *target;
+ int res = -1;
- if (target) {
- int res = pickup_do(chan, target);
+ if ((target = ast_channel_callback(find_by_mark, NULL, (char *) mark, 0))) {
+ ast_channel_lock(target);
+ res = pickup_do(chan, target);
ast_channel_unlock(target);
- target = NULL;
- return res;
+ target = ast_channel_unref(target);
}
- return -1;
+ return res;
}
/* application entry point for Pickup() */