diff options
author | kpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b> | 2006-10-04 19:47:22 +0000 |
---|---|---|
committer | kpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b> | 2006-10-04 19:47:22 +0000 |
commit | ac1ade7444c3a81d76e879f6d3571aae3c8e7bae (patch) | |
tree | ec12e69e00eece03476c2cb0feca8bc2b7be859b /main | |
parent | c75c68f35a55aa7107e5096bde5dc4d2f0cd4a07 (diff) |
update thread creation code a bit
reduce standard thread stack size slightly to allow the pthreads library to allocate the stack+data and not overflow a power-of-2 allocation in the kernel and waste memory/address space
add a new stack size for 'background' threads (those that don't handle PBX calls) when LOW_MEMORY is defined
git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.4@44378 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main')
-rw-r--r-- | main/asterisk.c | 8 | ||||
-rw-r--r-- | main/autoservice.c | 2 | ||||
-rw-r--r-- | main/cdr.c | 4 | ||||
-rw-r--r-- | main/devicestate.c | 2 | ||||
-rw-r--r-- | main/dnsmgr.c | 2 | ||||
-rw-r--r-- | main/http.c | 4 | ||||
-rw-r--r-- | main/manager.c | 4 | ||||
-rw-r--r-- | main/utils.c | 43 |
8 files changed, 37 insertions, 32 deletions
diff --git a/main/asterisk.c b/main/asterisk.c index 6e3222af5..75426fe4e 100644 --- a/main/asterisk.c +++ b/main/asterisk.c @@ -301,7 +301,7 @@ void ast_register_thread(char *name) if (!new) return; new->id = pthread_self(); - new->name = name; /* this was a copy already */ + new->name = name; /* steal the allocated memory for the thread name */ AST_LIST_LOCK(&thread_list); AST_LIST_INSERT_HEAD(&thread_list, new, list); AST_LIST_UNLOCK(&thread_list); @@ -313,7 +313,7 @@ void ast_unregister_thread(void *id) AST_LIST_LOCK(&thread_list); AST_LIST_TRAVERSE_SAFE_BEGIN(&thread_list, x, list) { - if ((void *)x->id == id) { + if ((void *) x->id == id) { AST_LIST_REMOVE_CURRENT(&thread_list, list); break; } @@ -906,7 +906,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(&consoles[x].t, &attr, netconsole, &consoles[x])) { + if (ast_pthread_create_background(&consoles[x].t, &attr, 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]); @@ -964,7 +964,7 @@ static int ast_makesocket(void) return -1; } ast_register_verbose(network_verboser); - ast_pthread_create(<hread, NULL, listener, NULL); + ast_pthread_create_background(<hread, NULL, listener, NULL); if (!ast_strlen_zero(ast_config_AST_CTL_OWNER)) { struct passwd *pw; diff --git a/main/autoservice.c b/main/autoservice.c index 51a1e1ca6..1d9670794 100644 --- a/main/autoservice.c +++ b/main/autoservice.c @@ -111,7 +111,7 @@ int ast_autoservice_start(struct ast_channel *chan) AST_LIST_INSERT_HEAD(&aslist, as, list); res = 0; if (asthread == AST_PTHREADT_NULL) { /* need start the thread */ - if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) { + if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) { ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n"); /* There will only be a single member in the list at this point, the one we just added. */ diff --git a/main/cdr.c b/main/cdr.c index 4d3ca86bf..fb243ff49 100644 --- a/main/cdr.c +++ b/main/cdr.c @@ -853,7 +853,7 @@ void ast_cdr_submit_batch(int shutdown) } else { pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - if (ast_pthread_create(&batch_post_thread, &attr, do_batch_backend_process, oldbatchitems)) { + if (ast_pthread_create_background(&batch_post_thread, &attr, 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 { @@ -1101,7 +1101,7 @@ static int do_reload(void) if it does not exist */ if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) { ast_cond_init(&cdr_pending_cond, NULL); - if (ast_pthread_create(&cdr_thread, NULL, do_cdr, NULL) < 0) { + if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) { ast_log(LOG_ERROR, "Unable to start CDR thread.\n"); ast_sched_del(sched, cdr_sched); } else { diff --git a/main/devicestate.c b/main/devicestate.c index c434ce9a0..3f38930d1 100644 --- a/main/devicestate.c +++ b/main/devicestate.c @@ -374,7 +374,7 @@ static void *do_devstate_changes(void *data) int ast_device_state_engine_init(void) { ast_cond_init(&change_pending, NULL); - if (ast_pthread_create(&change_thread, NULL, do_devstate_changes, NULL) < 0) { + if (ast_pthread_create_background(&change_thread, NULL, do_devstate_changes, NULL) < 0) { ast_log(LOG_ERROR, "Unable to start device state change thread.\n"); return -1; } diff --git a/main/dnsmgr.c b/main/dnsmgr.c index 0cc0d63f8..3f370e054 100644 --- a/main/dnsmgr.c +++ b/main/dnsmgr.c @@ -390,7 +390,7 @@ static int do_reload(int loading) /* if this reload enabled the manager, create the background thread if it does not exist */ if (enabled && !was_enabled && (refresh_thread == AST_PTHREADT_NULL)) { - if (ast_pthread_create(&refresh_thread, NULL, do_refresh, NULL) < 0) { + if (ast_pthread_create_background(&refresh_thread, NULL, do_refresh, NULL) < 0) { ast_log(LOG_ERROR, "Unable to start refresh thread.\n"); } else { diff --git a/main/http.c b/main/http.c index 9666539c7..e76163105 100644 --- a/main/http.c +++ b/main/http.c @@ -510,7 +510,7 @@ static void *http_root(void *data) pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - if (ast_pthread_create(&launched, &attr, ast_httpd_helper_thread, ser)) { + if (ast_pthread_create_background(&launched, &attr, ast_httpd_helper_thread, ser)) { ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno)); fclose(ser->f); free(ser); @@ -588,7 +588,7 @@ static void http_server_start(struct sockaddr_in *sin) } flags = fcntl(httpfd, F_GETFL); fcntl(httpfd, F_SETFL, flags | O_NONBLOCK); - if (ast_pthread_create(&master, NULL, http_root, NULL)) { + if (ast_pthread_create_background(&master, NULL, http_root, NULL)) { ast_log(LOG_NOTICE, "Unable to launch http server on %s:%d: %s\n", ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), strerror(errno)); diff --git a/main/manager.c b/main/manager.c index 543784c13..ca2bd68fb 100644 --- a/main/manager.c +++ b/main/manager.c @@ -2194,7 +2194,7 @@ static void *accept_thread(void *ignore) s->eventq = s->eventq->next; AST_LIST_UNLOCK(&sessions); ast_atomic_fetchadd_int(&s->eventq->usecount, 1); - if (ast_pthread_create(&s->t, &attr, session_do, s)) + if (ast_pthread_create_background(&s->t, &attr, session_do, s)) destroy_session(s); } pthread_attr_destroy(&attr); @@ -2805,7 +2805,7 @@ int init_manager(void) fcntl(asock, F_SETFL, flags | O_NONBLOCK); if (option_verbose) ast_verbose("Asterisk Management interface listening on port %d\n", portno); - ast_pthread_create(&t, NULL, accept_thread, NULL); + ast_pthread_create_background(&t, NULL, accept_thread, NULL); } return 0; } diff --git a/main/utils.c b/main/utils.c index 22e5027a8..3f44fe777 100644 --- a/main/utils.c +++ b/main/utils.c @@ -525,31 +525,38 @@ struct thr_arg { * are odd macros which start and end a block, so they _must_ be * used in pairs (the latter with a '1' argument to call the * handler on exit. - * On BSD we don't need this, but we keep it for compatibility with the MAC. + * On BSD we don't need this, but we keep it for compatibility. */ static void *dummy_start(void *data) { void *ret; - struct thr_arg a = *((struct thr_arg *)data); /* make a local copy */ + struct thr_arg a = *((struct thr_arg *) data); /* make a local copy */ + /* note that even though data->name is a pointer to allocated memory, + we are not freeing it here because ast_register_thread is going to + keep a copy of the pointer and then ast_unregister_thread will + free the memory + */ free(data); ast_register_thread(a.name); - pthread_cleanup_push(ast_unregister_thread, (void *)pthread_self()); /* on unregister */ + pthread_cleanup_push(ast_unregister_thread, (void *) pthread_self()); ret = a.start_routine(a.data); pthread_cleanup_pop(1); + return ret; } -int ast_pthread_create_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) +int ast_pthread_create_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) { struct thr_arg *a; - pthread_attr_t lattr; if (!attr) { - pthread_attr_init(&lattr); - attr = &lattr; + attr = alloca(sizeof(*attr)); + pthread_attr_init(attr); } + #ifdef __linux__ /* On Linux, pthread_attr_init() defaults to PTHREAD_EXPLICIT_SCHED, which is kind of useless. Change this here to @@ -558,27 +565,25 @@ int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*st This does mean that callers cannot set a different priority using PTHREAD_EXPLICIT_SCHED in the attr argument; instead they must set the priority afterwards with pthread_setschedparam(). */ - errno = pthread_attr_setinheritsched(attr, PTHREAD_INHERIT_SCHED); - if (errno) - ast_log(LOG_WARNING, "pthread_attr_setinheritsched returned non-zero: %s\n", strerror(errno)); + if ((errno = pthread_attr_setinheritsched(attr, PTHREAD_INHERIT_SCHED))) + ast_log(LOG_WARNING, "pthread_attr_setinheritsched: %s\n", strerror(errno)); #endif if (!stacksize) stacksize = AST_STACKSIZE; - errno = pthread_attr_setstacksize(attr, stacksize); - if (errno) - ast_log(LOG_WARNING, "pthread_attr_setstacksize returned non-zero: %s\n", strerror(errno)); - a = ast_malloc(sizeof(*a)); - if (!a) - ast_log(LOG_WARNING, "no memory, thread %s will not be listed\n", start_fn); - else { /* remap parameters */ + + if ((errno = pthread_attr_setstacksize(attr, stacksize ? stacksize : AST_STACKSIZE))) + ast_log(LOG_WARNING, "pthread_attr_setstacksize: %s\n", strerror(errno)); + + if ((a = ast_malloc(sizeof(*a)))) { a->start_routine = start_routine; a->data = data; start_routine = dummy_start; asprintf(&a->name, "%-20s started at [%5d] %s %s()", - start_fn, line, file, caller); + start_fn, line, file, caller); data = a; } + return pthread_create(thread, attr, start_routine, data); /* We're in ast_pthread_create, so it's okay */ } |