summaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorRussell Bryant <russell@russellbryant.com>2007-05-24 18:30:19 +0000
committerRussell Bryant <russell@russellbryant.com>2007-05-24 18:30:19 +0000
commit4b3a3fb14c298512ef69e17a710e210de14914fb (patch)
tree86895556d8f221a39105f4398a54d612fbbd3da8 /main
parentbcd2bd8294408ca2f432747ef2e3073edecec4c1 (diff)
Add a new API call for creating detached threads. Then, go replace all of the
places in the code where the same block of code for creating detached threads was replicated. (patch from bbryant) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@65968 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main')
-rw-r--r--main/asterisk.c11
-rw-r--r--main/cdr.c6
-rw-r--r--main/http.c7
-rw-r--r--main/manager.c6
-rw-r--r--main/pbx.c26
-rw-r--r--main/utils.c26
6 files changed, 35 insertions, 47 deletions
diff --git a/main/asterisk.c b/main/asterisk.c
index e95736061..a2f9efb33 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -932,9 +932,6 @@ static void *listener(void *unused)
int x;
int flags;
struct pollfd fds[1];
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
for (;;) {
if (ast_socket < 0)
return NULL;
@@ -966,7 +963,7 @@ static void *listener(void *unused)
fcntl(consoles[x].p[1], F_SETFL, flags | O_NONBLOCK);
consoles[x].fd = s;
consoles[x].mute = ast_opt_mute;
- if (ast_pthread_create_background(&consoles[x].t, &attr, netconsole, &consoles[x])) {
+ if (ast_pthread_create_detached_background(&consoles[x].t, NULL, netconsole, &consoles[x])) {
ast_log(LOG_ERROR, "Unable to spawn thread to handle connection: %s\n", strerror(errno));
close(consoles[x].p[0]);
close(consoles[x].p[1]);
@@ -3029,13 +3026,9 @@ int main(int argc, char *argv[])
/* Console stuff now... */
/* Register our quit function */
char title[256];
- pthread_attr_t attr;
pthread_t dont_care;
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- ast_pthread_create(&dont_care, &attr, monitor_sig_flags, NULL);
- pthread_attr_destroy(&attr);
+ ast_pthread_create_detached(&dont_care, NULL, monitor_sig_flags, NULL);
set_icon("Asterisk");
snprintf(title, sizeof(title), "Asterisk Console on '%s' (pid %ld)", hostname, (long)ast_mainpid);
diff --git a/main/cdr.c b/main/cdr.c
index 5886aab07..fb1ce177e 100644
--- a/main/cdr.c
+++ b/main/cdr.c
@@ -1033,7 +1033,6 @@ static void *do_batch_backend_process(void *data)
void ast_cdr_submit_batch(int shutdown)
{
struct ast_cdr_batch_item *oldbatchitems = NULL;
- pthread_attr_t attr;
pthread_t batch_post_thread = AST_PTHREADT_NULL;
/* if there's no batch, or no CDRs in the batch, then there's nothing to do */
@@ -1053,16 +1052,13 @@ void ast_cdr_submit_batch(int shutdown)
ast_log(LOG_DEBUG, "CDR single-threaded batch processing begins now\n");
do_batch_backend_process(oldbatchitems);
} else {
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- if (ast_pthread_create_background(&batch_post_thread, &attr, do_batch_backend_process, oldbatchitems)) {
+ if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
do_batch_backend_process(oldbatchitems);
} else {
if (option_debug)
ast_log(LOG_DEBUG, "CDR multi-threaded batch processing begins now\n");
}
- pthread_attr_destroy(&attr);
}
}
diff --git a/main/http.c b/main/http.c
index c69ac72ff..5d652b8dc 100644
--- a/main/http.c
+++ b/main/http.c
@@ -907,7 +907,6 @@ void *server_root(void *data)
socklen_t sinlen;
struct server_instance *ser;
pthread_t launched;
- pthread_attr_t attr;
for (;;) {
int i, flags;
@@ -935,17 +934,13 @@ void *server_root(void *data)
ser->fd = fd;
ser->parent = desc;
memcpy(&ser->requestor, &sin, sizeof(ser->requestor));
-
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- if (ast_pthread_create_background(&launched, &attr, make_file_from_fd, ser)) {
+ if (ast_pthread_create_detached_background(&launched, NULL, make_file_from_fd, ser)) {
ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno));
close(ser->fd);
free(ser);
}
- pthread_attr_destroy(&attr);
}
return NULL;
}
diff --git a/main/manager.c b/main/manager.c
index 43dd43af5..3d3c06ab0 100644
--- a/main/manager.c
+++ b/main/manager.c
@@ -1933,7 +1933,6 @@ static int action_originate(struct mansession *s, const struct message *m)
char tmp2[256];
pthread_t th;
- pthread_attr_t attr;
if (!name) {
astman_send_error(s, m, "Channel not specified");
return 0;
@@ -1988,14 +1987,11 @@ static int action_originate(struct mansession *s, const struct message *m)
ast_copy_string(fast->account, account, sizeof(fast->account));
fast->timeout = to;
fast->priority = pi;
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- if (ast_pthread_create(&th, &attr, fast_originate, fast)) {
+ if (ast_pthread_create_detached(&th, NULL, fast_originate, fast)) {
res = -1;
} else {
res = 0;
}
- pthread_attr_destroy(&attr);
}
} else if (!ast_strlen_zero(app)) {
res = ast_pbx_outgoing_app(tech, AST_FORMAT_SLINEAR, data, to, app, appdata, &reason, 1, l, n, vars, account, NULL);
diff --git a/main/pbx.c b/main/pbx.c
index c294501a9..cf27f905b 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -2551,7 +2551,6 @@ static void *pbx_thread(void *data)
enum ast_pbx_result ast_pbx_start(struct ast_channel *c)
{
pthread_t t;
- pthread_attr_t attr;
if (!c) {
ast_log(LOG_WARNING, "Asked to start thread on NULL channel?\n");
@@ -2562,14 +2561,10 @@ enum ast_pbx_result ast_pbx_start(struct ast_channel *c)
return AST_PBX_CALL_LIMIT;
/* Start a new thread, and get something handling this channel. */
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- if (ast_pthread_create(&t, &attr, pbx_thread, c)) {
+ if (ast_pthread_create_detached(&t, NULL, pbx_thread, c)) {
ast_log(LOG_WARNING, "Failed to create new channel thread\n");
- pthread_attr_destroy(&attr);
return AST_PBX_FAILED;
}
- pthread_attr_destroy(&attr);
return AST_PBX_SUCCESS;
}
@@ -4999,7 +4994,6 @@ int ast_pbx_outgoing_exten(const char *type, int format, void *data, int timeout
struct async_stat *as;
int res = -1, cdr_res = -1;
struct outgoing_helper oh;
- pthread_attr_t attr;
if (sync) {
oh.context = context;
@@ -5111,9 +5105,7 @@ int ast_pbx_outgoing_exten(const char *type, int format, void *data, int timeout
ast_set_variables(chan, vars);
if (account)
ast_cdr_setaccount(chan, account);
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- if (ast_pthread_create(&as->p, &attr, async_wait, as)) {
+ if (ast_pthread_create_detached(&as->p, NULL, async_wait, as)) {
ast_log(LOG_WARNING, "Failed to start async wait\n");
free(as);
if (channel) {
@@ -5122,10 +5114,8 @@ int ast_pbx_outgoing_exten(const char *type, int format, void *data, int timeout
}
ast_hangup(chan);
res = -1;
- pthread_attr_destroy(&attr);
goto outgoing_exten_cleanup;
}
- pthread_attr_destroy(&attr);
res = 0;
}
outgoing_exten_cleanup:
@@ -5163,7 +5153,6 @@ int ast_pbx_outgoing_app(const char *type, int format, void *data, int timeout,
struct app_tmp *tmp;
int res = -1, cdr_res = -1;
struct outgoing_helper oh;
- pthread_attr_t attr;
memset(&oh, 0, sizeof(oh));
oh.vars = vars;
@@ -5212,11 +5201,9 @@ int ast_pbx_outgoing_app(const char *type, int format, void *data, int timeout,
ast_channel_unlock(chan);
ast_pbx_run_app(tmp);
} else {
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (locked_channel)
ast_channel_lock(chan);
- if (ast_pthread_create(&tmp->t, &attr, ast_pbx_run_app, tmp)) {
+ if (ast_pthread_create_detached(&tmp->t, NULL, ast_pbx_run_app, tmp)) {
ast_log(LOG_WARNING, "Unable to spawn execute thread on %s: %s\n", chan->name, strerror(errno));
free(tmp);
if (locked_channel)
@@ -5227,7 +5214,6 @@ int ast_pbx_outgoing_app(const char *type, int format, void *data, int timeout,
if (locked_channel)
*locked_channel = chan;
}
- pthread_attr_destroy(&attr);
}
}
} else {
@@ -5275,24 +5261,20 @@ int ast_pbx_outgoing_app(const char *type, int format, void *data, int timeout,
if (account)
ast_cdr_setaccount(chan, account);
/* Start a new thread, and get something handling this channel. */
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (locked_channel)
ast_channel_lock(chan);
- if (ast_pthread_create(&as->p, &attr, async_wait, as)) {
+ if (ast_pthread_create_detached(&as->p, NULL, async_wait, as)) {
ast_log(LOG_WARNING, "Failed to start async wait\n");
free(as);
if (locked_channel)
ast_channel_unlock(chan);
ast_hangup(chan);
res = -1;
- pthread_attr_destroy(&attr);
goto outgoing_app_cleanup;
} else {
if (locked_channel)
*locked_channel = chan;
}
- pthread_attr_destroy(&attr);
res = 0;
}
outgoing_app_cleanup:
diff --git a/main/utils.c b/main/utils.c
index cdb87eb57..498184a41 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -595,6 +595,32 @@ int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*st
return pthread_create(thread, attr, start_routine, data); /* We're in ast_pthread_create, so it's okay */
}
+
+int ast_pthread_create_detached_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *),
+ void *data, size_t stacksize, const char *file, const char *caller,
+ int line, const char *start_fn)
+{
+ unsigned char attr_destroy = 0;
+ int res;
+
+ if (!attr) {
+ attr = alloca(sizeof(*attr));
+ pthread_attr_init(attr);
+ attr_destroy = 1;
+ }
+
+ if ((errno = pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED)))
+ ast_log(LOG_WARNING, "pthread_attr_setdetachstate: %s\n", strerror(errno));
+
+ res = ast_pthread_create_stack(thread, attr, start_routine, data,
+ stacksize, file, caller, line, start_fn);
+
+ if (attr_destroy)
+ pthread_attr_destroy(attr);
+
+ return res;
+}
+
int ast_wait_for_input(int fd, int ms)
{
struct pollfd pfd[1];