diff options
author | Jörg Frings-Fürst <debian@jff.email> | 2022-01-08 11:51:07 +0100 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff.email> | 2022-01-08 11:51:07 +0100 |
commit | be8efac78d067c138ad8dda03df4336e73f94887 (patch) | |
tree | 5f5254a628ba0ef72065b93d949d1c985742ea8e /tests/glthread/thread.h | |
parent | 7b65dbd4ebade81d504cfe5e681292a58ad1fdf0 (diff) |
New upstream version 1.0upstream/1.0
Diffstat (limited to 'tests/glthread/thread.h')
-rw-r--r-- | tests/glthread/thread.h | 229 |
1 files changed, 66 insertions, 163 deletions
diff --git a/tests/glthread/thread.h b/tests/glthread/thread.h index 1e266bd..6d98f2d 100644 --- a/tests/glthread/thread.h +++ b/tests/glthread/thread.h @@ -1,22 +1,21 @@ /* Creating and controlling threads. - Copyright (C) 2005-2018 Free Software Foundation, Inc. + Copyright (C) 2005-2022 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3, or (at your option) - any later version. + This file is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. - This program is distributed in the hope that it will be useful, + This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + GNU Lesser General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, see <https://www.gnu.org/licenses/>. */ + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. */ /* Written by Bruno Haible <bruno@clisp.org>, 2005. - Based on GCC's gthr-posix.h, gthr-posix95.h, gthr-solaris.h, - gthr-win32.h. */ + Based on GCC's gthr-posix.h, gthr-posix95.h, gthr-win32.h. */ /* This file contains primitives for creating and controlling threads. @@ -74,17 +73,53 @@ #include <errno.h> #include <stdlib.h> -#ifndef _GL_INLINE_HEADER_BEGIN - #error "Please include config.h first." +#if !defined c11_threads_in_use +# if HAVE_THREADS_H && USE_POSIX_THREADS_FROM_LIBC +# define c11_threads_in_use() 1 +# elif HAVE_THREADS_H && USE_POSIX_THREADS_WEAK +# include <threads.h> +# pragma weak thrd_exit +# define c11_threads_in_use() (thrd_exit != NULL) +# else +# define c11_threads_in_use() 0 +# endif #endif -_GL_INLINE_HEADER_BEGIN -#ifndef _GLTHREAD_THREAD_INLINE -# define _GLTHREAD_THREAD_INLINE _GL_INLINE + +/* ========================================================================= */ + +#if USE_ISOC_THREADS + +/* Use the ISO C threads library. */ + +# include <threads.h> + +# ifdef __cplusplus +extern "C" { +# endif + +/* -------------------------- gl_thread_t datatype -------------------------- */ + +typedef struct thrd_with_exitvalue *gl_thread_t; +extern int glthread_create (gl_thread_t *threadp, + void *(*func) (void *), void *arg); +# define glthread_sigmask(HOW, SET, OSET) \ + pthread_sigmask (HOW, SET, OSET) +extern int glthread_join (gl_thread_t thread, void **return_value_ptr); +extern gl_thread_t gl_thread_self (void); +# define gl_thread_self_pointer() \ + (void *) gl_thread_self () +extern _Noreturn void gl_thread_exit (void *return_value); +# define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0 + +# ifdef __cplusplus +} +# endif + #endif /* ========================================================================= */ -#if USE_POSIX_THREADS +#if USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS /* Use the POSIX threads library. */ @@ -133,8 +168,6 @@ extern int glthread_in_use (void); call to foo(...) in the same function. To avoid this, we test the address of a function in libpthread that we don't use. */ -# pragma weak pthread_create - # ifndef pthread_sigmask /* Do not declare rpl_pthread_sigmask weak. */ # pragma weak pthread_sigmask # endif @@ -150,7 +183,8 @@ extern int glthread_in_use (void); # if !PTHREAD_IN_USE_DETECTION_HARD # pragma weak pthread_mutexattr_gettype -# define pthread_in_use() (pthread_mutexattr_gettype != NULL) +# define pthread_in_use() \ + (pthread_mutexattr_gettype != NULL || c11_threads_in_use ()) # endif # else @@ -192,12 +226,12 @@ extern const gl_thread_t gl_null_thread; extern const gl_thread_t gl_null_thread; # else # define gl_thread_self() \ - (pthread_in_use () ? pthread_self () : (pthread_t) NULL) + (pthread_in_use () ? pthread_self () : (pthread_t) 0) # define gl_thread_self_pointer() \ (pthread_in_use () ? (void *) pthread_self () : NULL) # endif # define gl_thread_exit(RETVAL) \ - (pthread_in_use () ? pthread_exit (RETVAL) : 0) + (void) (pthread_in_use () ? (pthread_exit (RETVAL), 0) : 0) # if HAVE_PTHREAD_ATFORK # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) \ @@ -214,152 +248,33 @@ extern const gl_thread_t gl_null_thread; /* ========================================================================= */ -#if USE_PTH_THREADS - -/* Use the GNU Pth threads library. */ - -# include <pth.h> - -# ifdef __cplusplus -extern "C" { -# endif - -# if USE_PTH_THREADS_WEAK - -/* Use weak references to the GNU Pth threads library. */ - -# pragma weak pth_init -# pragma weak pth_spawn -# pragma weak pth_sigmask -# pragma weak pth_join -# pragma weak pth_self -# pragma weak pth_exit - -# pragma weak pth_cancel -# define pth_in_use() (pth_cancel != NULL) - -# else - -# define pth_in_use() 1 - -# endif -/* -------------------------- gl_thread_t datatype -------------------------- */ - -typedef pth_t gl_thread_t; -# define glthread_create(THREADP, FUNC, ARG) \ - (pth_in_use () ? (pth_init (), ((*(THREADP) = pth_spawn (NULL, FUNC, ARG)) ? 0 : errno)) : 0) -# define glthread_sigmask(HOW, SET, OSET) \ - (pth_in_use () ? (pth_init (), (pth_sigmask (HOW, SET, OSET) ? 0 : errno)) : 0) -# define glthread_join(THREAD, RETVALP) \ - (pth_in_use () ? (pth_init (), (pth_join (THREAD, RETVALP) ? 0 : errno)) : 0) -# define gl_thread_self() \ - (pth_in_use () ? (pth_init (), (void *) pth_self ()) : NULL) -# define gl_thread_self_pointer() \ - gl_thread_self () -# define gl_thread_exit(RETVAL) \ - (pth_in_use () ? (pth_init (), pth_exit (RETVAL)) : 0) -# define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0 - -# ifdef __cplusplus -} -# endif - -#endif - -/* ========================================================================= */ - -#if USE_SOLARIS_THREADS - -/* Use the old Solaris threads library. */ - -# include <thread.h> -# include <synch.h> - -# ifdef __cplusplus -extern "C" { -# endif - -# if USE_SOLARIS_THREADS_WEAK - -/* Use weak references to the old Solaris threads library. */ - -# pragma weak thr_create -# pragma weak thr_join -# pragma weak thr_self -# pragma weak thr_exit - -# pragma weak thr_suspend -# define thread_in_use() (thr_suspend != NULL) - -# else - -# define thread_in_use() 1 - -# endif - -/* -------------------------- gl_thread_t datatype -------------------------- */ - -typedef thread_t gl_thread_t; -# define glthread_create(THREADP, FUNC, ARG) \ - (thread_in_use () ? thr_create (NULL, 0, FUNC, ARG, 0, THREADP) : 0) -# define glthread_sigmask(HOW, SET, OSET) \ - (thread_in_use () ? sigprocmask (HOW, SET, OSET) : 0) -# define glthread_join(THREAD, RETVALP) \ - (thread_in_use () ? thr_join (THREAD, NULL, RETVALP) : 0) -# define gl_thread_self() \ - (thread_in_use () ? (void *) thr_self () : NULL) -# define gl_thread_self_pointer() \ - gl_thread_self () -# define gl_thread_exit(RETVAL) \ - (thread_in_use () ? thr_exit (RETVAL) : 0) -# define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0 - -# ifdef __cplusplus -} -# endif - -#endif - -/* ========================================================================= */ - #if USE_WINDOWS_THREADS # define WIN32_LEAN_AND_MEAN /* avoid including junk */ # include <windows.h> +# include "windows-thread.h" + # ifdef __cplusplus extern "C" { # endif /* -------------------------- gl_thread_t datatype -------------------------- */ -/* The gl_thread_t is a pointer to a structure in memory. - Why not the thread handle? If it were the thread handle, it would be hard - to implement gl_thread_self() (since GetCurrentThread () returns a pseudo- - handle, DuplicateHandle (GetCurrentThread ()) returns a handle that must be - closed afterwards, and there is no function for quickly retrieving a thread - handle from its id). - Why not the thread id? I tried it. It did not work: Sometimes ids appeared - that did not belong to running threads, and glthread_join failed with ESRCH. - */ -typedef struct gl_thread_struct *gl_thread_t; +typedef glwthread_thread_t gl_thread_t; # define glthread_create(THREADP, FUNC, ARG) \ - glthread_create_func (THREADP, FUNC, ARG) + glwthread_thread_create (THREADP, 0, FUNC, ARG) # define glthread_sigmask(HOW, SET, OSET) \ /* unsupported */ 0 # define glthread_join(THREAD, RETVALP) \ - glthread_join_func (THREAD, RETVALP) + glwthread_thread_join (THREAD, RETVALP) # define gl_thread_self() \ - gl_thread_self_func () + glwthread_thread_self () # define gl_thread_self_pointer() \ gl_thread_self () # define gl_thread_exit(RETVAL) \ - gl_thread_exit_func (RETVAL) + glwthread_thread_exit (RETVAL) # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0 -extern int glthread_create_func (gl_thread_t *threadp, void * (*func) (void *), void *arg); -extern int glthread_join_func (gl_thread_t thread, void **retvalp); -extern gl_thread_t gl_thread_self_func (void); -extern int gl_thread_exit_func (void *retval); # ifdef __cplusplus } @@ -369,7 +284,7 @@ extern int gl_thread_exit_func (void *retval); /* ========================================================================= */ -#if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WINDOWS_THREADS) +#if !(USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS) /* Provide dummy implementation if threads are not supported. */ @@ -380,7 +295,7 @@ typedef int gl_thread_t; # define gl_thread_self() 0 # define gl_thread_self_pointer() \ ((void *) gl_thread_self ()) -# define gl_thread_exit(RETVAL) 0 +# define gl_thread_exit(RETVAL) (void)0 # define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0 #endif @@ -393,17 +308,7 @@ typedef int gl_thread_t; extern "C" { #endif -_GLTHREAD_THREAD_INLINE gl_thread_t -gl_thread_create (void *(*func) (void *arg), void *arg) -{ - gl_thread_t thread; - int ret; - - ret = glthread_create (&thread, func, arg); - if (ret != 0) - abort (); - return thread; -} +extern gl_thread_t gl_thread_create (void *(*func) (void *arg), void *arg); #define gl_thread_sigmask(HOW, SET, OSET) \ do \ { \ @@ -430,6 +335,4 @@ gl_thread_create (void *(*func) (void *arg), void *arg) } #endif -_GL_INLINE_HEADER_END - #endif /* _GLTHREAD_THREAD_H */ |