diff options
author | Yann Ylavic <ylavic@apache.org> | 2020-12-03 20:19:40 +0000 |
---|---|---|
committer | Yann Ylavic <ylavic@apache.org> | 2020-12-03 20:19:40 +0000 |
commit | ee6a4a8ba864740c7f3fbc331d197d260e1504a5 (patch) | |
tree | 46cb4047baea2814e134333c6a710b9fd1aed60d /threadproc/beos | |
parent | 150cc61d491d51c8fe3b5c2eb7b3db74efbdb3fa (diff) | |
download | apr-ee6a4a8ba864740c7f3fbc331d197d260e1504a5.tar.gz |
apr_thread: destroy the thread's pool at _join() time, unless _detach()ed.
Destroying a joinable thread pool from apr_thread_exit() or when the thread
function returns, i.e. from inside the thread itself, is racy or deadlocky
with APR_POOL_DEBUG, with the parent pool being destroyed.
This commit adds a ->detached flag in each arch's apr_thread_t struct to track
whether a thread is detached (either at _create() or _detach() time). If
detached, the pool is destroyed when the thread exits, otherwise when the
thread is joined with apr_thread_join().
git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1884077 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'threadproc/beos')
-rw-r--r-- | threadproc/beos/thread.c | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/threadproc/beos/thread.c b/threadproc/beos/thread.c index c372f135e..61d7fd0d7 100644 --- a/threadproc/beos/thread.c +++ b/threadproc/beos/thread.c @@ -69,7 +69,9 @@ static void *dummy_worker(void *opaque) apr_pool_owner_set(thd->pool, 0); ret = thd->func(thd, thd->data); - apr_pool_destroy(thd->pool); + if (thd->detached) { + apr_pool_destroy(thd->pool); + } return ret; } @@ -80,7 +82,7 @@ APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new, apr_threadattr_t int32 temp; apr_status_t stat; - (*new) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t)); + (*new) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t)); if ((*new) == NULL) { return APR_ENOMEM; } @@ -88,6 +90,7 @@ APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new, apr_threadattr_t (*new)->data = data; (*new)->func = func; (*new)->exitval = -1; + (*new)->detached = (attr && apr_threadattr_detach_get(attr) == APR_DETACH); /* First we create the new thread...*/ if (attr) @@ -126,14 +129,21 @@ int apr_os_thread_equal(apr_os_thread_t tid1, apr_os_thread_t tid2) APR_DECLARE(void) apr_thread_exit(apr_thread_t *thd, apr_status_t retval) { - apr_pool_destroy(thd->pool); thd->exitval = retval; + if (thd->detached) { + apr_pool_destroy(thd->pool); + } exit_thread ((status_t)(retval)); } APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval, apr_thread_t *thd) { status_t rv = 0, ret; + + if (thd->detached) { + return APR_EINVAL; + } + ret = wait_for_thread(thd->td, &rv); if (ret == B_NO_ERROR) { *retval = rv; @@ -145,6 +155,7 @@ APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval, apr_thread_t *th */ if (thd->exitval != -1) { *retval = thd->exitval; + apr_pool_destroy(thd->pool); return APR_SUCCESS; } else return ret; @@ -153,7 +164,12 @@ APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval, apr_thread_t *th APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd) { - if (suspend_thread(thd->td) == B_NO_ERROR){ + if (thd->detached) { + return APR_EINVAL; + } + + if (suspend_thread(thd->td) == B_NO_ERROR) { + thd->detached = 1; return APR_SUCCESS; } else { |