summaryrefslogtreecommitdiff
path: root/threadproc/netware
diff options
context:
space:
mode:
authorYann Ylavic <ylavic@apache.org>2022-01-19 14:15:49 +0000
committerYann Ylavic <ylavic@apache.org>2022-01-19 14:15:49 +0000
commitd77a576daf76df4ea5c175ea156ade0ed7f844f1 (patch)
tree652d76b9f45f71eef7e07f311304755e1ffd4858 /threadproc/netware
parent9f9375278461a2c7675d66411412af65b08c0e6c (diff)
downloadapr-d77a576daf76df4ea5c175ea156ade0ed7f844f1.tar.gz
apr_thread: Use compiler's TLS to track the current apr_thread_t's pointer.
All modern compilers provide a Thread Local Storage keyword that allows to store per-thread data efficiently (C++11 's thread_local, C11's _Thread_local, gcc/clang's __thread or MSVC's __declspec(thread)). Use that to have an apr_thread_t pointer associated with each thread created by apr_thread_create() or any native thread (like the process' initial thread) that registered itself with the new apr_thread_current_create() function. This mechanism allows to implement apr_thread_current() quite efficiently, if available, otherwise the function returns NULL. If available APR_HAS_THREAD_LOCAL is #define'd to 1 and the APR_THREAD_LOCAL macro is the keyword usable to register TLS variables natively. Both APR_HAS_THREAD_LOCAL and APR_THREAD_LOCAL are #undef'ined if the compiler does not provide the mechanism. This allows to test for the functionality at compile time. When APR_HAS_THREAD_LOCAL, the user can load his/her own TLS data with: apr_thread_data_get(&my_data, my_key, apr_thread_current()); and store them with: apr_thread_data_set(my_data, my_key, my_data_cleanup, apr_thread_current()); which can be nice to avoid the proliferation of native TLS keys. git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1897207 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'threadproc/netware')
-rw-r--r--threadproc/netware/thread.c106
1 files changed, 82 insertions, 24 deletions
diff --git a/threadproc/netware/thread.c b/threadproc/netware/thread.c
index a661ef886..993d2c60d 100644
--- a/threadproc/netware/thread.c
+++ b/threadproc/netware/thread.c
@@ -64,35 +64,44 @@ APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
return APR_ENOTIMPL;
}
+#ifdef APR_HAS_THREAD_LOCAL
+static APR_THREAD_LOCAL apr_thread_t *current_thread;
+#endif
+
static void *dummy_worker(void *opaque)
{
apr_thread_t *thd = (apr_thread_t *)opaque;
void *ret;
+#ifdef APR_HAS_THREAD_LOCAL
+ current_thread = thd;
+#endif
+
apr_pool_owner_set(thd->pool, 0);
ret = thd->func(thd, thd->data);
if (thd->detached) {
apr_pool_destroy(thd->pool);
}
+
+#ifdef APR_HAS_THREAD_LOCAL
+ current_thread = NULL;
+#endif
return ret;
}
-apr_status_t apr_thread_create(apr_thread_t **new,
- apr_threadattr_t *attr,
- apr_thread_start_t func,
- void *data,
- apr_pool_t *pool)
+static apr_status_t alloc_thread(apr_thread_t **new,
+ apr_threadattr_t *attr,
+ apr_thread_start_t func, void *data,
+ apr_pool_t *pool)
{
apr_status_t stat;
- unsigned long flags = NX_THR_BIND_CONTEXT;
- size_t stack_size = APR_DEFAULT_STACK_SIZE;
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
+ * exits. The allocator needs no mutex obviously since the pool should
* not be used nor create children pools outside the thread.
*/
stat = apr_allocator_create(&allocator);
@@ -131,6 +140,24 @@ apr_status_t apr_thread_create(apr_thread_t **new,
return APR_ENOMEM;
}
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new,
+ apr_threadattr_t *attr,
+ apr_thread_start_t func,
+ void *data,
+ apr_pool_t *pool)
+{
+ apr_status_t stat;
+ unsigned long flags = NX_THR_BIND_CONTEXT;
+ size_t stack_size = APR_DEFAULT_STACK_SIZE;
+
+ stat = alloc_thread(new, attr, func, data, pool);
+ if (stat != APR_SUCCESS) {
+ return stat;
+ }
+
/* An original stack size of 0 will allow NXCreateThread() to
* assign a default system stack size. An original stack
* size of less than 0 will assign the APR default stack size.
@@ -139,11 +166,11 @@ apr_status_t apr_thread_create(apr_thread_t **new,
if (attr && (attr->stack_size >= 0)) {
stack_size = attr->stack_size;
}
-
+
if (attr && attr->detach) {
flags |= NX_THR_DETACHED;
}
-
+
(*new)->ctx = NXContextAlloc(
/* void(*start_routine)(void *arg) */ (void (*)(void *)) dummy_worker,
/* void *arg */ (*new),
@@ -151,7 +178,7 @@ apr_status_t apr_thread_create(apr_thread_t **new,
/* size_t stackSize */ stack_size,
/* unsigned long flags */ NX_CTX_NORMAL,
/* int *error */ &stat);
-
+
(void) NXContextSetName(
/* NXContext_t ctx */ (*new)->ctx,
/* const char *name */ (*new)->thread_name);
@@ -162,13 +189,46 @@ apr_status_t apr_thread_create(apr_thread_t **new,
/* NXThreadId_t *thread_id */ &(*new)->td);
if (stat) {
- apr_pool_destroy(p);
+ apr_pool_destroy((*new)->pool);
+ return stat;
+ }
+
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_thread_current_create(apr_thread_t **current,
+ apr_threadattr_t *attr,
+ apr_pool_t *pool)
+{
+ apr_status_t stat;
+
+ *current = apr_thread_current();
+ if (*current) {
+ return APR_EEXIST;
+ }
+
+ stat = alloc_thread(current, attr, NULL, NULL, pool);
+ if (stat != APR_SUCCESS) {
return stat;
}
-
+
+ (*current)->td = apr_os_thread_current();
+
+#ifdef APR_HAS_THREAD_LOCAL
+ current_thread = *current;
+#endif
return APR_SUCCESS;
}
+APR_DECLARE(apr_thread_t *) apr_thread_current(void)
+{
+#ifdef APR_HAS_THREAD_LOCAL
+ return current_thread;
+#else
+ return NULL;
+#endif
+}
+
apr_os_thread_t apr_os_thread_current()
{
return NXThreadGetId();
@@ -190,6 +250,9 @@ void apr_thread_exit(apr_thread_t *thd, apr_status_t retval)
if (thd->detached) {
apr_pool_destroy(thd->pool);
}
+#ifdef APR_HAS_THREAD_LOCAL
+ current_thread = NULL;
+#endif
NXThreadExit(NULL);
}
@@ -227,26 +290,21 @@ apr_status_t apr_thread_detach(apr_thread_t *thd)
apr_status_t apr_thread_data_get(void **data, const char *key,
apr_thread_t *thread)
{
- if (thread != NULL) {
- return apr_pool_userdata_get(data, key, thread->pool);
- }
- else {
- data = NULL;
+ if (thread == NULL) {
+ *data = NULL;
return APR_ENOTHREAD;
}
+ return apr_pool_userdata_get(data, key, thread->pool);
}
apr_status_t apr_thread_data_set(void *data, const char *key,
apr_status_t (*cleanup) (void *),
apr_thread_t *thread)
{
- if (thread != NULL) {
- return apr_pool_userdata_set(data, key, cleanup, thread->pool);
- }
- else {
- data = NULL;
+ if (thread == NULL) {
return APR_ENOTHREAD;
}
+ return apr_pool_userdata_set(data, key, cleanup, thread->pool);
}
APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd,