Add a new API call for creating detached threads. Then, go replace all of the
authorRussell Bryant <russell@russellbryant.com>
Thu, 24 May 2007 18:30:19 +0000 (18:30 +0000)
committerRussell Bryant <russell@russellbryant.com>
Thu, 24 May 2007 18:30:19 +0000 (18:30 +0000)
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

18 files changed:
apps/app_meetme.c
apps/app_mixmonitor.c
apps/app_rpt.c
channels/chan_h323.c
channels/chan_iax2.c
channels/chan_mgcp.c
channels/chan_sip.c
channels/chan_skinny.c
channels/chan_zap.c
include/asterisk/utils.h
main/asterisk.c
main/cdr.c
main/http.c
main/manager.c
main/pbx.c
main/utils.c
pbx/pbx_dundi.c
pbx/pbx_spool.c

index 0e50414..5fffa91 100644 (file)
@@ -1428,10 +1428,7 @@ static int conf_run(struct ast_channel *chan, struct ast_conference *conf, int c
                        ast_hangup(conf->lchan);
                        conf->lchan = NULL;
                } else {
-                       pthread_attr_init(&conf->attr);
-                       pthread_attr_setdetachstate(&conf->attr, PTHREAD_CREATE_DETACHED);
-                       ast_pthread_create_background(&conf->recordthread, &conf->attr, recordthread, conf);
-                       pthread_attr_destroy(&conf->attr);
+                       ast_pthread_create_detached_background(&conf->recordthread, &conf->attr, recordthread, conf);
                }
        }
 
@@ -3458,7 +3455,6 @@ static void sla_handle_dial_state_event(void)
                struct sla_ringing_trunk *ringing_trunk = NULL;
                struct run_station_args args;
                enum ast_dial_result dial_res;
-               pthread_attr_t attr;
                pthread_t dont_care;
                ast_mutex_t cond_lock;
                ast_cond_t cond;
@@ -3499,15 +3495,12 @@ static void sla_handle_dial_state_event(void)
                        free(ringing_station);
                        ast_mutex_init(&cond_lock);
                        ast_cond_init(&cond, NULL);
-                       pthread_attr_init(&attr);
-                       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
                        ast_mutex_lock(&cond_lock);
-                       ast_pthread_create_background(&dont_care, &attr, run_station, &args);
+                       ast_pthread_create_detached_background(&dont_care, NULL, run_station, &args);
                        ast_cond_wait(&cond, &cond_lock);
                        ast_mutex_unlock(&cond_lock);
                        ast_mutex_destroy(&cond_lock);
                        ast_cond_destroy(&cond);
-                       pthread_attr_destroy(&attr);
                        break;
                case AST_DIAL_RESULT_TRYING:
                case AST_DIAL_RESULT_RINGING:
@@ -4237,7 +4230,6 @@ static int sla_station_exec(struct ast_channel *chan, void *data)
                ast_mutex_t cond_lock;
                ast_cond_t cond;
                pthread_t dont_care;
-               pthread_attr_t attr;
                struct dial_trunk_args args = {
                        .trunk_ref = trunk_ref,
                        .station = station,
@@ -4251,15 +4243,12 @@ static int sla_station_exec(struct ast_channel *chan, void *data)
                ast_autoservice_start(chan);
                ast_mutex_init(&cond_lock);
                ast_cond_init(&cond, NULL);
-               pthread_attr_init(&attr);
-               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
                ast_mutex_lock(&cond_lock);
-               ast_pthread_create_background(&dont_care, &attr, dial_trunk, &args);
+               ast_pthread_create_detached_background(&dont_care, NULL, dial_trunk, &args);
                ast_cond_wait(&cond, &cond_lock);
                ast_mutex_unlock(&cond_lock);
                ast_mutex_destroy(&cond_lock);
                ast_cond_destroy(&cond);
-               pthread_attr_destroy(&attr);
                ast_autoservice_stop(chan);
                if (!trunk_ref->trunk->chan) {
                        ast_log(LOG_DEBUG, "Trunk didn't get created. chan: %lx\n", (long) trunk_ref->trunk->chan);
index 40faee2..f293912 100644 (file)
@@ -229,7 +229,6 @@ static void *mixmonitor_thread(void *obj)
 static void launch_monitor_thread(struct ast_channel *chan, const char *filename, unsigned int flags,
                                  int readvol, int writevol, const char *post_process) 
 {
-       pthread_attr_t attr;
        pthread_t thread;
        struct mixmonitor *mixmonitor;
        char postprocess2[1024] = "";
@@ -296,10 +295,7 @@ static void launch_monitor_thread(struct ast_channel *chan, const char *filename
                return;
        }
 
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-       ast_pthread_create_background(&thread, &attr, mixmonitor_thread, mixmonitor);
-       pthread_attr_destroy(&attr);
+       ast_pthread_create_detached_background(&thread, NULL, mixmonitor_thread, mixmonitor);
 
 }
 
index 73a817a..e59a7bb 100644 (file)
@@ -2452,7 +2452,6 @@ static void rpt_telemetry(struct rpt *myrpt, int mode, void *data)
        struct rpt_tele *tele;
        struct rpt_link *mylink = (struct rpt_link *) data;
        int res;
-       pthread_attr_t attr;
 
        tele = ast_calloc(1, sizeof(struct rpt_tele));
        if (!tele) {
@@ -2471,10 +2470,7 @@ static void rpt_telemetry(struct rpt *myrpt, int mode, void *data)
        }
        insque((struct qelem *)tele, (struct qelem *)myrpt->tele.next);
        rpt_mutex_unlock(&myrpt->lock);
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-       res = ast_pthread_create(&tele->threadid, &attr, rpt_tele_thread, (void *) tele);
-       pthread_attr_destroy(&attr);
+       res = ast_pthread_create_detached(&tele->threadid, NULL, rpt_tele_thread, (void *) tele);
        if (res != 0) {
                rpt_mutex_lock(&myrpt->lock);
                remque((struct qlem *) tele); /* We don't like stuck transmitters, remove it from the queue */
@@ -3108,7 +3104,6 @@ static int function_ilink(struct rpt *myrpt, char *param, char *digits, int comm
 
 static int function_autopatchup(struct rpt *myrpt, char *param, char *digitbuf, int command_source, struct rpt_link *mylink)
 {
-       pthread_attr_t attr;
        int i, index;
        char *value = NULL;
        AST_DECLARE_APP_ARGS(params,
@@ -3184,10 +3179,7 @@ static int function_autopatchup(struct rpt *myrpt, char *param, char *digitbuf,
        myrpt->cidx = 0;
        myrpt->exten[myrpt->cidx] = 0;
        rpt_mutex_unlock(&myrpt->lock);
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-       ast_pthread_create(&myrpt->rpt_call_thread, &attr, rpt_call, (void *) myrpt);
-       pthread_attr_destroy(&attr);
+       ast_pthread_create_detached(&myrpt->rpt_call_thread, NULL, rpt_call, (void *) myrpt);
        return DC_COMPLETE;
 }
 
@@ -5448,7 +5440,6 @@ static int attempt_reconnect(struct rpt *myrpt, struct rpt_link *l)
 static void local_dtmf_helper(struct rpt *myrpt, char c)
 {
        int     res;
-       pthread_attr_t attr;
        char cmd[MAXDTMF+1] = "";
 
        if (c == myrpt->p.endchar) {
@@ -5537,10 +5528,7 @@ static void local_dtmf_helper(struct rpt *myrpt, char c)
                        myrpt->cidx = 0;
                        myrpt->exten[myrpt->cidx] = 0;
                        rpt_mutex_unlock(&myrpt->lock);
-                       pthread_attr_init(&attr);
-                       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-                       ast_pthread_create(&myrpt->rpt_call_thread, &attr, rpt_call, (void *)myrpt);
-                       pthread_attr_destroy(&attr);
+                       ast_pthread_create_detached(&myrpt->rpt_call_thread, NULL, rpt_call, (void *)myrpt);
                        return;
                }
        }
@@ -6668,7 +6656,6 @@ static void *rpt(void *this)
 static void *rpt_master(void *config)
 {
        int     i, n;
-       pthread_attr_t attr;
        struct ast_config *cfg;
        char *this;
        const char *val;
@@ -6732,10 +6719,7 @@ static void *rpt_master(void *config)
                        ast_config_destroy(cfg);
                        pthread_exit(NULL);
                }
-               pthread_attr_init(&attr);
-               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-               ast_pthread_create(&rpt_vars[i].rpt_thread, &attr, rpt, (void *) &rpt_vars[i]);
-               pthread_attr_destroy(&attr);
+               ast_pthread_create_detached(&rpt_vars[i].rpt_thread, NULL, rpt, (void *) &rpt_vars[i]);
        }
        usleep(500000);
        for (;;) {
@@ -6761,10 +6745,7 @@ static void *rpt_master(void *config)
                                        rpt_vars[i].threadrestarts = 0;
 
                                rpt_vars[i].lastthreadrestarttime = time(NULL);
-                               pthread_attr_init(&attr);
-                               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-                               ast_pthread_create(&rpt_vars[i].rpt_thread, &attr, rpt, (void *) &rpt_vars[i]);
-                               pthread_attr_destroy(&attr);
+                               ast_pthread_create_detached(&rpt_vars[i].rpt_thread, NULL, rpt, (void *) &rpt_vars[i]);
                                ast_log(LOG_WARNING, "rpt_thread restarted on node %s\n", rpt_vars[i].name);
                        }
 
index 65154b6..e8d0915 100644 (file)
@@ -2601,7 +2601,6 @@ restartsearch:
 
 static int restart_monitor(void)
 {
-       pthread_attr_t attr;
        /* If we're supposed to be stopped -- stay stopped */
        if (ast_mutex_lock(&monlock)) {
                ast_log(LOG_WARNING, "Unable to lock monitor\n");
@@ -2620,17 +2619,13 @@ static int restart_monitor(void)
                /* Wake up the thread */
                pthread_kill(monitor_thread, SIGURG);
        } else {
-               pthread_attr_init(&attr);
-               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
                /* Start a new monitor */
-               if (ast_pthread_create_background(&monitor_thread, &attr, do_monitor, NULL) < 0) {
+               if (ast_pthread_create_detached_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
                        monitor_thread = AST_PTHREADT_NULL;
                        ast_mutex_unlock(&monlock);
                        ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
-                       pthread_attr_destroy(&attr);
                        return -1;
                }
-               pthread_attr_destroy(&attr);
        }
        ast_mutex_unlock(&monlock);
        return 0;
index b2c2a4c..a62d9fc 100644 (file)
@@ -926,7 +926,6 @@ static void insert_idle_thread(struct iax2_thread *thread)
 
 static struct iax2_thread *find_idle_thread(void)
 {
-       pthread_attr_t attr;
        struct iax2_thread *thread = NULL;
 
        /* Pop the head of the idle list off */
@@ -960,9 +959,7 @@ static struct iax2_thread *find_idle_thread(void)
        ast_cond_init(&thread->cond, NULL);
 
        /* Create thread and send it on it's way */
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);    
-       if (ast_pthread_create_background(&thread->threadid, &attr, iax2_process_thread, thread)) {
+       if (ast_pthread_create_detached_background(&thread->threadid, NULL, iax2_process_thread, thread)) {
                ast_cond_destroy(&thread->cond);
                ast_mutex_destroy(&thread->lock);
                free(thread);
@@ -6267,24 +6264,18 @@ static void spawn_dp_lookup(int callno, const char *context, const char *calledn
 {
        pthread_t newthread;
        struct dpreq_data *dpr;
-       pthread_attr_t attr;
        
        if (!(dpr = ast_calloc(1, sizeof(*dpr))))
                return;
 
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);    
-
        dpr->callno = callno;
        ast_copy_string(dpr->context, context, sizeof(dpr->context));
        ast_copy_string(dpr->callednum, callednum, sizeof(dpr->callednum));
        if (callerid)
                dpr->callerid = ast_strdup(callerid);
-       if (ast_pthread_create(&newthread, &attr, dp_lookup_thread, dpr)) {
+       if (ast_pthread_create_detached(&newthread, NULL, dp_lookup_thread, dpr)) {
                ast_log(LOG_WARNING, "Unable to start lookup thread!\n");
        }
-
-       pthread_attr_destroy(&attr);
 }
 
 struct iax_dual {
@@ -6352,18 +6343,11 @@ static int iax_park(struct ast_channel *chan1, struct ast_channel *chan2)
                return -1;
        }
        if ((d = ast_calloc(1, sizeof(*d)))) {
-               pthread_attr_t attr;
-
-               pthread_attr_init(&attr);
-               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
                d->chan1 = chan1m;
                d->chan2 = chan2m;
-               if (!ast_pthread_create_background(&th, &attr, iax_park_thread, d)) {
-                       pthread_attr_destroy(&attr);
+               if (!ast_pthread_create_detached_background(&th, NULL, iax_park_thread, d)) {
                        return 0;
                }
-               pthread_attr_destroy(&attr);
                free(d);
        }
        return -1;
@@ -8467,7 +8451,6 @@ static void *network_thread(void *ignore)
 
 static int start_network_thread(void)
 {
-       pthread_attr_t attr;
        int threadcount = 0;
        int x;
        for (x = 0; x < iaxthreadcount; x++) {
@@ -8477,9 +8460,7 @@ static int start_network_thread(void)
                        thread->threadnum = ++threadcount;
                        ast_mutex_init(&thread->lock);
                        ast_cond_init(&thread->cond, NULL);
-                       pthread_attr_init(&attr);
-                       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);    
-                       if (ast_pthread_create(&thread->threadid, &attr, iax2_process_thread, thread)) {
+                       if (ast_pthread_create_detached(&thread->threadid, NULL, iax2_process_thread, thread)) {
                                ast_log(LOG_WARNING, "Failed to create new thread!\n");
                                free(thread);
                                thread = NULL;
index f9caf58..3895330 100644 (file)
@@ -2956,9 +2956,6 @@ static void handle_hd_hf(struct mgcp_subchannel *sub, char *ev)
        struct mgcp_endpoint *p = sub->parent;
        struct ast_channel *c;
        pthread_t t;
-       pthread_attr_t attr;
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);    
 
        /* Off hook / answer */
        if (sub->outgoing) {
@@ -3006,7 +3003,7 @@ static void handle_hd_hf(struct mgcp_subchannel *sub, char *ev)
                                }
                                c = mgcp_new(sub, AST_STATE_DOWN);
                                if (c) {
-                                       if (ast_pthread_create(&t, &attr, mgcp_ss, c)) {
+                                       if (ast_pthread_create_detached(&t, NULL, mgcp_ss, c)) {
                                                ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
                                                ast_hangup(c);
                                        }
@@ -3034,7 +3031,6 @@ static void handle_hd_hf(struct mgcp_subchannel *sub, char *ev)
                        /*ast_queue_control(sub->owner, AST_CONTROL_ANSWER);*/
                }
        }
-       pthread_attr_destroy(&attr);
 }
 
 static int handle_request(struct mgcp_subchannel *sub, struct mgcp_request *req, struct sockaddr_in *sin)
index ac72c11..4c63ffe 100644 (file)
@@ -13640,24 +13640,18 @@ static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct
                return -1;
        }
        if ((d = ast_calloc(1, sizeof(*d)))) {
-               pthread_attr_t attr;
-
-               pthread_attr_init(&attr);
-               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);    
 
                /* Save original request for followup */
                copy_request(&d->req, req);
                d->chan1 = transferee;  /* Transferee */
                d->chan2 = transferer;  /* Transferer */
                d->seqno = seqno;
-               if (ast_pthread_create_background(&th, &attr, sip_park_thread, d) < 0) {
+               if (ast_pthread_create_detached_background(&th, NULL, sip_park_thread, d) < 0) {
                        /* Could not start thread */
                        free(d);        /* We don't need it anymore. If thread is created, d will be free'd
                                           by sip_park_thread() */
-                       pthread_attr_destroy(&attr);
                        return 0;
                }
-               pthread_attr_destroy(&attr);
        } 
        return -1;
 }
index e505120..72ca801 100644 (file)
@@ -4514,12 +4514,8 @@ static void *accept_thread(void *ignore)
        struct skinnysession *s;
        struct protoent *p;
        int arg = 1;
-       pthread_attr_t attr;
        pthread_t tcp_thread;
 
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
        for (;;) {
                sinlen = sizeof(sin);
                as = accept(skinnysock, (struct sockaddr *)&sin, &sinlen);
@@ -4544,14 +4540,13 @@ static void *accept_thread(void *ignore)
                sessions = s;
                ast_mutex_unlock(&sessionlock);
 
-               if (ast_pthread_create(&tcp_thread, &attr, skinny_session, s)) {
+               if (ast_pthread_create_detached(&tcp_thread, NULL, skinny_session, s)) {
                        destroy_session(s);
                }
        }
        if (skinnydebug)
                ast_verbose("killing accept thread\n");
        close(as);
-       pthread_attr_destroy(&attr);
        return 0;
 }
 
index 2359fde..d1a6032 100644 (file)
@@ -3946,7 +3946,6 @@ static struct ast_frame *zt_handle_event(struct ast_channel *ast)
        char *c;
        struct zt_pvt *p = ast->tech_pvt;
        pthread_t threadid;
-       pthread_attr_t attr;
        struct ast_channel *chan;
        struct ast_frame *f;
 
@@ -4549,11 +4548,9 @@ static struct ast_frame *zt_handle_event(struct ast_channel *ast)
                                                if (res)
                                                        ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
                                                p->owner = chan;
-                                               pthread_attr_init(&attr);
-                                               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
                                                if (!chan) {
                                                        ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", p->channel);
-                                               } else if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
+                                               } else if (ast_pthread_create_detached(&threadid, NULL, ss_thread, chan)) {
                                                        ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
                                                        res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
                                                        zt_enable_ec(p);
@@ -4569,7 +4566,6 @@ static struct ast_frame *zt_handle_event(struct ast_channel *ast)
                                                        }
                                                        p->subs[SUB_THREEWAY].needhold = 1;
                                                }
-                                               pthread_attr_destroy(&attr);
                                        }
                                } else {
                                        /* Already have a 3 way call */
@@ -6941,8 +6937,7 @@ static int handle_init_event(struct zt_pvt *i, int event)
        pthread_t threadid;
        pthread_attr_t attr;
        struct ast_channel *chan;
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+
        /* Handle an event on a given channel for the monitor thread. */
        switch (event) {
        case ZT_EVENT_NONE:
@@ -6986,7 +6981,7 @@ static int handle_init_event(struct zt_pvt *i, int event)
                                                res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_DIALTONE);
                                        if (res < 0) 
                                                ast_log(LOG_WARNING, "Unable to play dialtone on channel %d\n", i->channel);
-                                       if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
+                                       if (ast_pthread_create_detached(&threadid, NULL, ss_thread, chan)) {
                                                ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
                                                res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
                                                if (res < 0)
@@ -7133,7 +7128,6 @@ static int handle_init_event(struct zt_pvt *i, int event)
                zap_destroy_channel_bynum(i->channel);
                break;
        }
-       pthread_attr_destroy(&attr);
        return 0;
 }
 
@@ -7332,9 +7326,6 @@ static void *do_monitor(void *data)
 
 static int restart_monitor(void)
 {
-       pthread_attr_t attr;
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        /* If we're supposed to be stopped -- stay stopped */
        if (monitor_thread == AST_PTHREADT_STOP)
                return 0;
@@ -7349,15 +7340,13 @@ static int restart_monitor(void)
                pthread_kill(monitor_thread, SIGURG);
        } else {
                /* Start a new monitor */
-               if (ast_pthread_create_background(&monitor_thread, &attr, do_monitor, NULL) < 0) {
+               if (ast_pthread_create_detached_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
                        ast_mutex_unlock(&monlock);
                        ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
-                       pthread_attr_destroy(&attr);
                        return -1;
                }
        }
        ast_mutex_unlock(&monlock);
-       pthread_attr_destroy(&attr);
        return 0;
 }
 
@@ -8521,15 +8510,11 @@ static void *ss7_linkset(void *data)
        ss7_event *e = NULL;
        struct zt_pvt *p;
        int chanpos;
-       pthread_attr_t attr;
        struct pollfd pollers[NUM_DCHANS];
        int cic;
        unsigned int dpc;
        int nextms = 0;
 
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
        ss7_start(ss7);
 
        while(1) {
@@ -9300,7 +9285,6 @@ static void *pri_dchannel(void *vpri)
        int cause=0;
        struct zt_pvt *crv;
        pthread_t threadid;
-       pthread_attr_t attr;
        char ani2str[6];
        char plancallingnum[256];
        char plancallingani[256];
@@ -9799,9 +9783,7 @@ static void *pri_dchannel(void *vpri)
                                                                pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
                                                        
                                                        ast_mutex_lock(&pri->lock);
-                                                       pthread_attr_init(&attr);
-                                                       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-                                                       if (c && !ast_pthread_create(&threadid, &attr, ss_thread, c)) {
+                                                       if (c && !ast_pthread_create_detached(&threadid, NULL, ss_thread, c)) {
                                                                if (option_verbose > 2)
                                                                        ast_verbose(VERBOSE_PREFIX_3 "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
                                                                                plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
@@ -9816,7 +9798,6 @@ static void *pri_dchannel(void *vpri)
                                                                        pri->pvts[chanpos]->call = NULL;
                                                                }
                                                        }
-                                                       pthread_attr_destroy(&attr);
                                                } else  {
                                                        ast_mutex_unlock(&pri->lock);
                                                        /* Release PRI lock while we create the channel */
index 5c68a27..4b523da 100644 (file)
@@ -291,16 +291,29 @@ int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*st
                             void *data, size_t stacksize, const char *file, const char *caller,
                             int line, const char *start_fn);
 
+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);
+
 #define ast_pthread_create(a, b, c, d) ast_pthread_create_stack(a, b, c, d,                    \
                                                                0,                              \
                                                                __FILE__, __FUNCTION__,         \
                                                                __LINE__, #c)
+#define ast_pthread_create_detached(a, b, c, d) ast_pthread_create_detached_stack(a, b, c, d, \
+                                                                       0, \
+                                                                       __FILE__, __FUNCTION__, \
+                                                                       __LINE__, #c)
 
 #define ast_pthread_create_background(a, b, c, d) ast_pthread_create_stack(a, b, c, d,                 \
                                                                           AST_BACKGROUND_STACKSIZE,    \
                                                                           __FILE__, __FUNCTION__,      \
                                                                           __LINE__, #c)
 
+#define ast_pthread_create_detached_background(a, b, c, d) ast_pthread_create_detached_stack(a, b, c, d, \
+                                                                       AST_BACKGROUND_STACKSIZE, \
+                                                                       __FILE__, __FUNCTION__, \
+                                                                       __LINE__, #c)
+
 /*!
        \brief Process a string to find and replace characters
        \param start The string to analyze
index e957360..a2f9efb 100644 (file)
@@ -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);
index 5886aab..fb1ce17 100644 (file)
@@ -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);
        }
 }
 
index c69ac72..5d652b8 100644 (file)
@@ -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;
 }
index 43dd43a..3d3c06a 100644 (file)
@@ -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);
index c294501..cf27f90 100644 (file)
@@ -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:
index cdb87eb..498184a 100644 (file)
@@ -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];
index 433558d..6857111 100644 (file)
@@ -771,7 +771,7 @@ static int dundi_answer_entity(struct dundi_transaction *trans, struct dundi_ies
        char eid_str[20];
        char *s;
        pthread_t lookupthread;
-       pthread_attr_t attr;
+
        if (ies->eidcount > 1) {
                /* Since it is a requirement that the first EID is the authenticating host
                   and the last EID is the root, it is permissible that the first and last EID
@@ -798,20 +798,17 @@ static int dundi_answer_entity(struct dundi_transaction *trans, struct dundi_ies
                }
                if (option_debug)
                        ast_log(LOG_DEBUG, "Answering EID query for '%s@%s'!\n", dundi_eid_to_str(eid_str, sizeof(eid_str), ies->reqeid), ies->called_context);
-               pthread_attr_init(&attr);
-               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+
                trans->thread = 1;
-               if (ast_pthread_create(&lookupthread, &attr, dundi_query_thread, st)) {
+               if (ast_pthread_create_detached(&lookupthread, NULL, dundi_query_thread, st)) {
                        trans->thread = 0;
                        ast_log(LOG_WARNING, "Unable to create thread!\n");
                        free(st);
                        memset(&ied, 0, sizeof(ied));
                        dundi_ie_append_cause(&ied, DUNDI_IE_CAUSE, DUNDI_CAUSE_GENERAL, "Out of threads");
                        dundi_send(trans, DUNDI_COMMAND_EIDRESPONSE, 0, 1, &ied);
-                       pthread_attr_destroy(&attr);
                        return -1;
                }
-               pthread_attr_destroy(&attr);
        } else {
                ast_log(LOG_WARNING, "Out of memory!\n");
                memset(&ied, 0, sizeof(ied));
@@ -916,7 +913,6 @@ static int dundi_prop_precache(struct dundi_transaction *trans, struct dundi_ies
        int skipfirst = 0;
        
        pthread_t lookupthread;
-       pthread_attr_t attr;
 
        memset(&dr2, 0, sizeof(dr2));
        memset(&dr, 0, sizeof(dr));
@@ -1029,20 +1025,16 @@ static int dundi_prop_precache(struct dundi_transaction *trans, struct dundi_ies
                st->nummaps = mapcount;
                if (option_debug)
                        ast_log(LOG_DEBUG, "Forwarding precache for '%s@%s'!\n", ies->called_number, ies->called_context);
-               pthread_attr_init(&attr);
-               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
                trans->thread = 1;
-               if (ast_pthread_create(&lookupthread, &attr, dundi_precache_thread, st)) {
+               if (ast_pthread_create_detached(&lookupthread, NULL, dundi_precache_thread, st)) {
                        trans->thread = 0;
                        ast_log(LOG_WARNING, "Unable to create thread!\n");
                        free(st);
                        memset(&ied, 0, sizeof(ied));
                        dundi_ie_append_cause(&ied, DUNDI_IE_CAUSE, DUNDI_CAUSE_GENERAL, "Out of threads");
                        dundi_send(trans, DUNDI_COMMAND_PRECACHERP, 0, 1, &ied);
-                       pthread_attr_destroy(&attr);
                        return -1;
                }
-               pthread_attr_destroy(&attr);
        } else {
                ast_log(LOG_WARNING, "Out of memory!\n");
                memset(&ied, 0, sizeof(ied));
@@ -1065,7 +1057,6 @@ static int dundi_answer_query(struct dundi_transaction *trans, struct dundi_ies
        int skipfirst = 0;
        
        pthread_t lookupthread;
-       pthread_attr_t attr;
        totallen = sizeof(struct dundi_query_state);
        /* Count matching map entries */
        AST_LIST_TRAVERSE(&mappings, cur, list) {
@@ -1118,20 +1109,16 @@ static int dundi_answer_query(struct dundi_transaction *trans, struct dundi_ies
                st->nummaps = mapcount;
                if (option_debug)
                        ast_log(LOG_DEBUG, "Answering query for '%s@%s'!\n", ies->called_number, ies->called_context);
-               pthread_attr_init(&attr);
-               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
                trans->thread = 1;
-               if (ast_pthread_create(&lookupthread, &attr, dundi_lookup_thread, st)) {
+               if (ast_pthread_create_detached(&lookupthread, NULL, dundi_lookup_thread, st)) {
                        trans->thread = 0;
                        ast_log(LOG_WARNING, "Unable to create thread!\n");
                        free(st);
                        memset(&ied, 0, sizeof(ied));
                        dundi_ie_append_cause(&ied, DUNDI_IE_CAUSE, DUNDI_CAUSE_GENERAL, "Out of threads");
                        dundi_send(trans, DUNDI_COMMAND_DPRESPONSE, 0, 1, &ied);
-                       pthread_attr_destroy(&attr);
                        return -1;
                }
-               pthread_attr_destroy(&attr);
        } else {
                ast_log(LOG_WARNING, "Out of memory!\n");
                memset(&ied, 0, sizeof(ied));
index e84ebaa..aaefef5 100644 (file)
@@ -359,15 +359,12 @@ static void *attempt_thread(void *data)
 static void launch_service(struct outgoing *o)
 {
        pthread_t t;
-       pthread_attr_t attr;
        int ret;
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-       if ((ret = ast_pthread_create(&t,&attr,attempt_thread, o)) != 0) {
+
+       if ((ret = ast_pthread_create_detached(&t, NULL, attempt_thread, o))) {
                ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
                free_outgoing(o);
        }
-       pthread_attr_destroy(&attr);
 }
 
 static int scan_service(char *fn, time_t now, time_t atime)
@@ -486,7 +483,6 @@ static int unload_module(void)
 static int load_module(void)
 {
        pthread_t thread;
-       pthread_attr_t attr;
        int ret;
        snprintf(qdir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing");
        if (mkdir(qdir, 0700) && (errno != EEXIST)) {
@@ -494,13 +490,12 @@ static int load_module(void)
                return 0;
        }
        snprintf(qdonedir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing_done");
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-       if ((ret = ast_pthread_create_background(&thread,&attr,scan_thread, NULL)) != 0) {
+
+       if ((ret = ast_pthread_create_detached_background(&thread, NULL, scan_thread, NULL))) {
                ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
                return -1;
        }
-       pthread_attr_destroy(&attr);
+
        return 0;
 }