diff options
author | Bruno Haible <bruno@clisp.org> | 2019-11-27 06:03:21 +0100 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2019-11-27 06:03:21 +0100 |
commit | 721d00b4505054875aa5530c230ca9ede5452d96 (patch) | |
tree | b540f6c499f77f67ccd084610ad321c44409f855 /lib/glthread/thread.c | |
parent | 42a045242f9d156becfb4d3fb63dcc607230369a (diff) | |
download | gnulib-721d00b4505054875aa5530c230ca9ede5452d96.tar.gz |
New options --enable-threads=isoc and --enable-threads=isoc+posix.
* m4/threadlib.m4 (gl_THREADLIB_EARLY_BODY): Accept the options
--enable-threads=isoc and --enable-threads=isoc+posix.
(gl_THREADLIB_BODY): Test whether the ISO C threads API is available.
When both the ISO C and the POSIX threads API are available, choose
USE_ISOC_AND_POSIX_THREADS instead of USE_POSIX_THREADS if
--enable-threads=isoc+posix was specified. When only the ISO C threads
API is available and --enable-threads=iso was specified, choose
USE_ISOC_THREADS.
* lib/glthread/lock.h: Add new code for USE_ISOC_THREADS ||
USE_ISOC_AND_POSIX_THREADS.
* lib/glthread/lock.c: Likewise.
* lib/glthread/cond.h: Likewise.
* lib/glthread/cond.c: Likewise.
* lib/glthread/tls.h: Likewise.
* lib/glthread/tls.c: Likewise.
* lib/glthread/yield.h: Likewise.
* lib/glthread/thread.h: Add new code for USE_ISOC_THREADS. Treat
USE_ISOC_AND_POSIX_THREADS like USE_POSIX_THREADS.
* lib/glthread/thread.c: Likewise.
* lib/glthread/threadlib.c: Likewise.
* tests/test-lock.c: Save and restore the values of USE_ISOC_THREADS and
USE_ISOC_AND_POSIX_THREADS.
* tests/test-cond.c: Consider USE_ISOC_THREADS and
USE_ISOC_AND_POSIX_THREADS.
* tests/test-tls.c: Likewise.
* tests/test-thread_create.c (main): Likewise.
* tests/test-pthread-cond.c: Likewise.
* tests/test-pthread-mutex.c: Likewise.
* tests/test-pthread-once2.c: Likewise.
* tests/test-pthread-rwlock.c: Likewise.
* tests/test-pthread-tss.c: Likewise.
* tests/test-pthread_sigmask2.c: Treat USE_ISOC_AND_POSIX_THREADS like
USE_POSIX_THREADS.
Diffstat (limited to 'lib/glthread/thread.c')
-rw-r--r-- | lib/glthread/thread.c | 158 |
1 files changed, 157 insertions, 1 deletions
diff --git a/lib/glthread/thread.c b/lib/glthread/thread.c index 776bfb6bf4..18e7b237d6 100644 --- a/lib/glthread/thread.c +++ b/lib/glthread/thread.c @@ -28,7 +28,163 @@ /* ========================================================================= */ -#if USE_POSIX_THREADS +#if USE_ISOC_THREADS + +struct thrd_with_exitvalue +{ + thrd_t volatile tid; + void * volatile exitvalue; +}; + +/* The Thread-Specific Storage (TSS) key that allows to access each thread's + 'struct thrd_with_exitvalue *' pointer. */ +static tss_t thrd_with_exitvalue_key; + +/* Initializes thrd_with_exitvalue_key. + This function must only be called once. */ +static void +do_init_thrd_with_exitvalue_key (void) +{ + if (tss_create (&thrd_with_exitvalue_key, NULL) != thrd_success) + abort (); +} + +/* Initializes thrd_with_exitvalue_key. */ +static void +init_thrd_with_exitvalue_key (void) +{ + static once_flag once = ONCE_FLAG_INIT; + call_once (&once, do_init_thrd_with_exitvalue_key); +} + +typedef union + { + struct thrd_with_exitvalue t; + struct + { + thrd_t tid; /* reserve memory for t.tid */ + void *(*mainfunc) (void *); + void *arg; + } a; + } + main_arg_t; + +static int +thrd_main_func (void *pmarg) +{ + /* Unpack the object that combines mainfunc and arg. */ + main_arg_t *main_arg = (main_arg_t *) pmarg; + void *(*mainfunc) (void *) = main_arg->a.mainfunc; + void *arg = main_arg->a.arg; + + if (tss_set (thrd_with_exitvalue_key, &main_arg->t) != thrd_success) + abort (); + + /* Execute mainfunc, with arg as argument. */ + { + void *exitvalue = mainfunc (arg); + /* Store the exitvalue, for use by glthread_join(). */ + main_arg->t.exitvalue = exitvalue; + return 0; + } +} + +int +glthread_create (gl_thread_t *threadp, void *(*mainfunc) (void *), void *arg) +{ + init_thrd_with_exitvalue_key (); + { + /* Combine mainfunc and arg in a single object. + A stack-allocated object does not work, because it would be out of + existence when thrd_create returns before thrd_main_func is + entered. So, allocate it in the heap. */ + main_arg_t *main_arg = (main_arg_t *) malloc (sizeof (main_arg_t)); + if (main_arg == NULL) + return ENOMEM; + main_arg->a.mainfunc = mainfunc; + main_arg->a.arg = arg; + switch (thrd_create ((thrd_t *) &main_arg->t.tid, thrd_main_func, main_arg)) + { + case thrd_success: + break; + case thrd_nomem: + free (main_arg); + return ENOMEM; + default: + free (main_arg); + return EAGAIN; + } + *threadp = &main_arg->t; + return 0; + } +} + +gl_thread_t +gl_thread_self (void) +{ + init_thrd_with_exitvalue_key (); + { + gl_thread_t thread = + (struct thrd_with_exitvalue *) tss_get (thrd_with_exitvalue_key); + if (thread == NULL) + { + /* This happens only in threads that have not been created through + glthread_create(), such as the main thread. */ + for (;;) + { + thread = + (struct thrd_with_exitvalue *) + malloc (sizeof (struct thrd_with_exitvalue)); + if (thread != NULL) + break; + /* Memory allocation failed. There is not much we can do. Have to + busy-loop, waiting for the availability of memory. */ + { + struct timespec ts; + ts.tv_sec = 1; + ts.tv_nsec = 0; + thrd_sleep (&ts, NULL); + } + } + thread->tid = thrd_current (); + thread->exitvalue = NULL; /* just to be deterministic */ + if (tss_set (thrd_with_exitvalue_key, thread) != thrd_success) + abort (); + } + return thread; + } +} + +int +glthread_join (gl_thread_t thread, void **return_value_ptr) +{ + /* On Solaris 11.4, thrd_join crashes when the second argument we pass is + NULL. */ + int dummy; + + if (thread == gl_thread_self ()) + return EINVAL; + if (thrd_join (thread->tid, &dummy) != thrd_success) + return EINVAL; + if (return_value_ptr != NULL) + *return_value_ptr = thread->exitvalue; + free (thread); + return 0; +} + +_Noreturn void +gl_thread_exit (void *return_value) +{ + gl_thread_t thread = gl_thread_self (); + thread->exitvalue = return_value; + thrd_exit (0); +} + +#endif + +/* ========================================================================= */ + +#if USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS #include <pthread.h> |