summaryrefslogtreecommitdiff
path: root/main/taskprocessor.c
diff options
context:
space:
mode:
authorMark Michelson <mmichelson@digium.com>2013-01-15 20:15:00 +0000
committerMark Michelson <mmichelson@digium.com>2013-01-15 20:15:00 +0000
commit03e89247ded06b5320684bfa10a6e5e2fbe57646 (patch)
treef00dbf712892926e00fa98719997354f3794f2d8 /main/taskprocessor.c
parentc6bc51ef28a79303500ab541684e8adc331cc662 (diff)
Address further review feedback from David Lee.
* Clarify some documentation * Change copyright date of taskprocessor files * Address potential issue of creating taskprocessor with listener if taskprocessor with that name exists already git-svn-id: https://origsvn.digium.com/svn/asterisk/team/mmichelson/threadpool@379124 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main/taskprocessor.c')
-rw-r--r--main/taskprocessor.c87
1 files changed, 49 insertions, 38 deletions
diff --git a/main/taskprocessor.c b/main/taskprocessor.c
index 911eb76f8..fae93967a 100644
--- a/main/taskprocessor.c
+++ b/main/taskprocessor.c
@@ -1,7 +1,7 @@
/*
* Asterisk -- An open source telephony toolkit.
*
- * Copyright (C) 2007-2008, Digium, Inc.
+ * Copyright (C) 2007-2013, Digium, Inc.
*
* Dwayne M. Hubbard <dhubbard@digium.com>
*
@@ -487,6 +487,50 @@ static void *default_listener_pvt_alloc(void)
return pvt;
}
+static struct ast_taskprocessor *__allocate_taskprocessor(const char *name, struct ast_taskprocessor_listener *listener)
+{
+ RAII_VAR(struct ast_taskprocessor *, p,
+ ao2_alloc(sizeof(*p), tps_taskprocessor_destroy), ao2_cleanup);
+
+ if (!p) {
+ ast_log(LOG_WARNING, "failed to create taskprocessor '%s'\n", name);
+ return NULL;
+ }
+
+ if (!(p->stats = ast_calloc(1, sizeof(*p->stats)))) {
+ ast_log(LOG_WARNING, "failed to create taskprocessor stats for '%s'\n", name);
+ return NULL;
+ }
+ if (!(p->name = ast_strdup(name))) {
+ ao2_ref(p, -1);
+ return NULL;
+ }
+
+ ao2_ref(listener, +1);
+ p->listener = listener;
+
+ ao2_ref(p, +1);
+ listener->tps = p;
+
+ if (!(ao2_link(tps_singletons, p))) {
+ ast_log(LOG_ERROR, "Failed to add taskprocessor '%s' to container\n", p->name);
+ return NULL;
+ }
+
+ if (p->listener->callbacks->start(p->listener)) {
+ ast_log(LOG_ERROR, "Unable to start taskprocessor listener for taskprocessor %s\n", p->name);
+ ast_taskprocessor_unreference(p);
+ return NULL;
+ }
+
+ /* RAII_VAR will decrement the refcount at the end of the function.
+ * Since we want to pass back a reference to p, we bump the refcount
+ */
+ ao2_ref(p, +1);
+ return p;
+
+}
+
/* Provide a reference to a taskprocessor. Create the taskprocessor if necessary, but don't
* create the taskprocessor if we were told via ast_tps_options to return a reference only
* if it already exists */
@@ -519,7 +563,7 @@ struct ast_taskprocessor *ast_taskprocessor_get(const char *name, enum ast_tps_o
return NULL;
}
- p = ast_taskprocessor_create_with_listener(name, listener);
+ p = __allocate_taskprocessor(name, listener);
if (!p) {
default_listener_pvt_destroy(pvt);
ao2_ref(listener, -1);
@@ -534,46 +578,13 @@ struct ast_taskprocessor *ast_taskprocessor_get(const char *name, enum ast_tps_o
struct ast_taskprocessor *ast_taskprocessor_create_with_listener(const char *name, struct ast_taskprocessor_listener *listener)
{
- RAII_VAR(struct ast_taskprocessor *, p,
- ao2_alloc(sizeof(*p), tps_taskprocessor_destroy),
- ao2_cleanup);
-
- if (!p) {
- ast_log(LOG_WARNING, "failed to create taskprocessor '%s'\n", name);
- return NULL;
- }
-
- if (!(p->stats = ast_calloc(1, sizeof(*p->stats)))) {
- ast_log(LOG_WARNING, "failed to create taskprocessor stats for '%s'\n", name);
- return NULL;
- }
- if (!(p->name = ast_strdup(name))) {
- ao2_ref(p, -1);
- return NULL;
- }
-
- ao2_ref(listener, +1);
- p->listener = listener;
-
- ao2_ref(p, +1);
- listener->tps = p;
-
- if (!(ao2_link(tps_singletons, p))) {
- ast_log(LOG_ERROR, "Failed to add taskprocessor '%s' to container\n", p->name);
- return NULL;
- }
+ struct ast_taskprocessor *p = ao2_find(tps_singletons, name, OBJ_KEY);
- if (p->listener->callbacks->start(p->listener)) {
- ast_log(LOG_ERROR, "Unable to start taskprocessor listener for taskprocessor %s\n", p->name);
+ if (p) {
ast_taskprocessor_unreference(p);
return NULL;
}
-
- /* RAII_VAR will decrement the refcount at the end of the function.
- * Since we want to pass back a reference to p, we bump the refcount
- */
- ao2_ref(p, +1);
- return p;
+ return __allocate_taskprocessor(name, listener);
}
/* decrement the taskprocessor reference count and unlink from the container if necessary */