summaryrefslogtreecommitdiff
path: root/threadproc
diff options
context:
space:
mode:
authorYann Ylavic <ylavic@apache.org>2022-07-14 10:31:22 +0000
committerYann Ylavic <ylavic@apache.org>2022-07-14 10:31:22 +0000
commitab59882d46f992fe13e696e0275a244123d51349 (patch)
treebc3ca68e32583a248001d06e307eb22b5a53a744 /threadproc
parent4bcb664471a5e05a290dc57bb67fa22d3323b207 (diff)
downloadapr-ab59882d46f992fe13e696e0275a244123d51349.tar.gz
apr_thread: Provide apr_threadattr_max_free_set().
When creating a thread, this allows to specify the "max_free" of its pool allocator (i.e. apr_allocator_max_free_set), so that one can create thread local subpools and have their memory usage regulated on cleanup/destroy. One could achieve that already with: apr_allocator_max_free_set(apr_thread_pool_get(thread), max_free); in the thread startup function, but it's more convenient, simpler and race free to handle that in the thread attribute itself at creation time. git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1902715 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'threadproc')
-rw-r--r--threadproc/beos/thread.c22
-rw-r--r--threadproc/netware/thread.c22
-rw-r--r--threadproc/os2/thread.c22
-rw-r--r--threadproc/unix/thread.c22
-rw-r--r--threadproc/win32/thread.c22
5 files changed, 60 insertions, 50 deletions
diff --git a/threadproc/beos/thread.c b/threadproc/beos/thread.c
index f14ef076e..0d59a58f7 100644
--- a/threadproc/beos/thread.c
+++ b/threadproc/beos/thread.c
@@ -62,6 +62,13 @@ APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
return APR_ENOTIMPL;
}
+APR_DECLARE(apr_status_t) apr_threadattr_max_free_set(apr_threadattr_t *attr,
+ apr_size_t size)
+{
+ attr->max_free = size;
+ return APR_SUCCESS;
+}
+
#if APR_HAS_THREAD_LOCAL
static APR_THREAD_LOCAL apr_thread_t *current_thread = NULL;
#endif
@@ -91,27 +98,22 @@ static apr_status_t alloc_thread(apr_thread_t **new,
{
apr_status_t stat;
apr_abortfunc_t abort_fn = apr_pool_abort_get(pool);
- apr_allocator_t *allocator;
apr_pool_t *p;
/* The thread can be detached anytime (from the creation or later with
* apr_thread_detach), so it needs its own pool and allocator to not
* depend on a parent pool which could be destroyed before the thread
* exits. The allocator needs no mutex obviously since the pool should
- * not be used nor create children pools outside the thread.
+ * not be used nor create children pools outside the thread. Passing
+ * NULL allocator will create one like that.
*/
- stat = apr_allocator_create(&allocator);
+ stat = apr_pool_create_unmanaged_ex(&p, abort_fn, NULL);
if (stat != APR_SUCCESS) {
- if (abort_fn)
- abort_fn(stat);
return stat;
}
- stat = apr_pool_create_unmanaged_ex(&p, abort_fn, allocator);
- if (stat != APR_SUCCESS) {
- apr_allocator_destroy(allocator);
- return stat;
+ if (attr && attr->max_free) {
+ apr_allocator_max_free_set(apr_pool_allocator_get(p), attr->max_free);
}
- apr_allocator_owner_set(allocator, p);
(*new) = (apr_thread_t *)apr_pcalloc(p, sizeof(apr_thread_t));
if ((*new) == NULL) {
diff --git a/threadproc/netware/thread.c b/threadproc/netware/thread.c
index 24122119a..f91957b2f 100644
--- a/threadproc/netware/thread.c
+++ b/threadproc/netware/thread.c
@@ -64,6 +64,13 @@ APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
return APR_ENOTIMPL;
}
+APR_DECLARE(apr_status_t) apr_threadattr_max_free_set(apr_threadattr_t *attr,
+ apr_size_t size)
+{
+ attr->max_free = size;
+ return APR_SUCCESS;
+}
+
#if APR_HAS_THREAD_LOCAL
static APR_THREAD_LOCAL apr_thread_t *current_thread = NULL;
#endif
@@ -93,27 +100,22 @@ static apr_status_t alloc_thread(apr_thread_t **new,
{
apr_status_t stat;
apr_abortfunc_t abort_fn = apr_pool_abort_get(pool);
- apr_allocator_t *allocator;
apr_pool_t *p;
/* The thread can be detached anytime (from the creation or later with
* apr_thread_detach), so it needs its own pool and allocator to not
* depend on a parent pool which could be destroyed before the thread
* exits. The allocator needs no mutex obviously since the pool should
- * not be used nor create children pools outside the thread.
+ * not be used nor create children pools outside the thread. Passing
+ * NULL allocator will create one like that.
*/
- stat = apr_allocator_create(&allocator);
+ stat = apr_pool_create_unmanaged_ex(&p, abort_fn, NULL);
if (stat != APR_SUCCESS) {
- if (abort_fn)
- abort_fn(stat);
return stat;
}
- stat = apr_pool_create_unmanaged_ex(&p, abort_fn, allocator);
- if (stat != APR_SUCCESS) {
- apr_allocator_destroy(allocator);
- return stat;
+ if (attr && attr->max_free) {
+ apr_allocator_max_free_set(apr_pool_allocator_get(p), attr->max_free);
}
- apr_allocator_owner_set(allocator, p);
(*new) = (apr_thread_t *)apr_pcalloc(p, sizeof(apr_thread_t));
if ((*new) == NULL) {
diff --git a/threadproc/os2/thread.c b/threadproc/os2/thread.c
index e0540188d..2756baf28 100644
--- a/threadproc/os2/thread.c
+++ b/threadproc/os2/thread.c
@@ -68,6 +68,13 @@ APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
return APR_ENOTIMPL;
}
+APR_DECLARE(apr_status_t) apr_threadattr_max_free_set(apr_threadattr_t *attr,
+ apr_size_t size)
+{
+ attr->max_free = size;
+ return APR_SUCCESS;
+}
+
#if APR_HAS_THREAD_LOCAL
static APR_THREAD_LOCAL apr_thread_t *current_thread = NULL;
#endif
@@ -95,27 +102,22 @@ static apr_status_t alloc_thread(apr_thread_t **new,
{
apr_status_t stat;
apr_abortfunc_t abort_fn = apr_pool_abort_get(pool);
- apr_allocator_t *allocator;
apr_pool_t *p;
/* The thread can be detached anytime (from the creation or later with
* apr_thread_detach), so it needs its own pool and allocator to not
* depend on a parent pool which could be destroyed before the thread
* exits. The allocator needs no mutex obviously since the pool should
- * not be used nor create children pools outside the thread.
+ * not be used nor create children pools outside the thread. Passing
+ * NULL allocator will create one like that.
*/
- stat = apr_allocator_create(&allocator);
+ stat = apr_pool_create_unmanaged_ex(&p, abort_fn, NULL);
if (stat != APR_SUCCESS) {
- if (abort_fn)
- abort_fn(stat);
return stat;
}
- stat = apr_pool_create_unmanaged_ex(&p, abort_fn, allocator);
- if (stat != APR_SUCCESS) {
- apr_allocator_destroy(allocator);
- return stat;
+ if (attr && attr->max_free) {
+ apr_allocator_max_free_set(apr_pool_allocator_get(p), attr->max_free);
}
- apr_allocator_owner_set(allocator, p);
(*new) = (apr_thread_t *)apr_pcalloc(p, sizeof(apr_thread_t));
if ((*new) == NULL) {
diff --git a/threadproc/unix/thread.c b/threadproc/unix/thread.c
index 209fe7c86..68b14db3c 100644
--- a/threadproc/unix/thread.c
+++ b/threadproc/unix/thread.c
@@ -136,6 +136,13 @@ APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
#endif
}
+APR_DECLARE(apr_status_t) apr_threadattr_max_free_set(apr_threadattr_t *attr,
+ apr_size_t size)
+{
+ attr->max_free = size;
+ return APR_SUCCESS;
+}
+
#if APR_HAS_THREAD_LOCAL
static APR_THREAD_LOCAL apr_thread_t *current_thread = NULL;
#endif
@@ -165,27 +172,22 @@ static apr_status_t alloc_thread(apr_thread_t **new,
{
apr_status_t stat;
apr_abortfunc_t abort_fn = apr_pool_abort_get(pool);
- apr_allocator_t *allocator;
apr_pool_t *p;
/* The thread can be detached anytime (from the creation or later with
* apr_thread_detach), so it needs its own pool and allocator to not
* depend on a parent pool which could be destroyed before the thread
* exits. The allocator needs no mutex obviously since the pool should
- * not be used nor create children pools outside the thread.
+ * not be used nor create children pools outside the thread. Passing
+ * NULL allocator will create one like that.
*/
- stat = apr_allocator_create(&allocator);
+ stat = apr_pool_create_unmanaged_ex(&p, abort_fn, NULL);
if (stat != APR_SUCCESS) {
- if (abort_fn)
- abort_fn(stat);
return stat;
}
- stat = apr_pool_create_unmanaged_ex(&p, abort_fn, allocator);
- if (stat != APR_SUCCESS) {
- apr_allocator_destroy(allocator);
- return stat;
+ if (attr && attr->max_free) {
+ apr_allocator_max_free_set(apr_pool_allocator_get(p), attr->max_free);
}
- apr_allocator_owner_set(allocator, p);
(*new) = (apr_thread_t *)apr_pcalloc(p, sizeof(apr_thread_t));
if ((*new) == NULL) {
diff --git a/threadproc/win32/thread.c b/threadproc/win32/thread.c
index 0b672e698..7ae7ca062 100644
--- a/threadproc/win32/thread.c
+++ b/threadproc/win32/thread.c
@@ -72,6 +72,13 @@ APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
return APR_ENOTIMPL;
}
+APR_DECLARE(apr_status_t) apr_threadattr_max_free_set(apr_threadattr_t *attr,
+ apr_size_t size)
+{
+ attr->max_free = size;
+ return APR_SUCCESS;
+}
+
#if APR_HAS_THREAD_LOCAL
static APR_THREAD_LOCAL apr_thread_t *current_thread = NULL;
#endif
@@ -102,27 +109,22 @@ static apr_status_t alloc_thread(apr_thread_t **new,
{
apr_status_t stat;
apr_abortfunc_t abort_fn = apr_pool_abort_get(pool);
- apr_allocator_t *allocator;
apr_pool_t *p;
/* The thread can be detached anytime (from the creation or later with
* apr_thread_detach), so it needs its own pool and allocator to not
* depend on a parent pool which could be destroyed before the thread
* exits. The allocator needs no mutex obviously since the pool should
- * not be used nor create children pools outside the thread.
+ * not be used nor create children pools outside the thread. Passing
+ * NULL allocator will create one like that.
*/
- stat = apr_allocator_create(&allocator);
+ stat = apr_pool_create_unmanaged_ex(&p, abort_fn, NULL);
if (stat != APR_SUCCESS) {
- if (abort_fn)
- abort_fn(stat);
return stat;
}
- stat = apr_pool_create_unmanaged_ex(&p, abort_fn, allocator);
- if (stat != APR_SUCCESS) {
- apr_allocator_destroy(allocator);
- return stat;
+ if (attr && attr->max_free) {
+ apr_allocator_max_free_set(apr_pool_allocator_get(p), attr->max_free);
}
- apr_allocator_owner_set(allocator, p);
(*new) = (apr_thread_t *)apr_pcalloc(p, sizeof(apr_thread_t));
if ((*new) == NULL) {