This commit is contained in:
2024-12-14 10:43:33 +03:00
parent 7f8090bb1f
commit bbf76e4925
1292 changed files with 2823 additions and 500876 deletions

View File

@@ -1,344 +0,0 @@
/* Condition variables for multithreading.
Copyright (C) 2005-2024 Free Software Foundation, Inc.
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 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 Lesser General Public License for more details.
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 Yoann Vandoorselaere <yoann@prelude-ids.org>, 2008.
Based on Bruno Haible <bruno@clisp.org> lock.h */
/*
Condition variables can be used for waiting until a condition
becomes true. In this respect, they are similar to wait queues. But
contrary to wait queues, condition variables have different
semantics that allows events to be lost when there is no thread
waiting for them.
Condition variable:
Type: gl_cond_t
Declaration: gl_cond_define(extern, name)
Initializer: gl_cond_define_initialized(, name)
Initialization: gl_cond_init (name);
Waiting: gl_cond_wait (name, lock);
Timed wait: bool timedout = gl_cond_timedwait (name, lock, abstime);
where lock is a gl_lock_t variable (cf. <glthread/lock.h>)
Signaling: gl_cond_signal (name);
Broadcasting: gl_cond_broadcast (name);
De-initialization: gl_cond_destroy (name);
Equivalent functions with control of error handling:
Initialization: err = glthread_cond_init (&name);
Waiting: err = glthread_cond_wait (&name);
Timed wait: err = glthread_cond_timedwait (&name, &lock, abstime);
Signaling: err = glthread_cond_signal (&name);
Broadcasting: err = glthread_cond_broadcast (&name);
De-initialization: err = glthread_cond_destroy (&name);
*/
#ifndef _GLTHREAD_COND_H
#define _GLTHREAD_COND_H
/* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE, HAVE_THREADS_H. */
#if !_GL_CONFIG_H_INCLUDED
#error "Please include config.h first."
#endif
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include "glthread/lock.h"
#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_COND_INLINE
# define _GLTHREAD_COND_INLINE _GL_INLINE
#endif
/* ========================================================================= */
#if USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS
/* Use the ISO C threads library. */
# include <threads.h>
# ifdef __cplusplus
extern "C" {
# endif
/* -------------------------- gl_cond_t datatype -------------------------- */
typedef struct
{
int volatile init_needed;
once_flag init_once;
void (*init_func) (void);
cnd_t condition;
}
gl_cond_t;
# define gl_cond_define(STORAGECLASS, NAME) \
STORAGECLASS gl_cond_t NAME;
# define gl_cond_define_initialized(STORAGECLASS, NAME) \
static void _atomic_init_##NAME (void); \
STORAGECLASS gl_cond_t NAME = \
{ 1, ONCE_FLAG_INIT, _atomic_init_##NAME }; \
static void _atomic_init_##NAME (void) \
{ \
if (glthread_cond_init (&(NAME))) \
abort (); \
}
extern int glthread_cond_init (gl_cond_t *condition);
extern int glthread_cond_wait (gl_cond_t *condition, gl_lock_t *lock);
extern int glthread_cond_timedwait (gl_cond_t *condition, gl_lock_t *lock,
const struct timespec *abstime);
extern int glthread_cond_signal (gl_cond_t *condition);
extern int glthread_cond_broadcast (gl_cond_t *condition);
extern int glthread_cond_destroy (gl_cond_t *condition);
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if USE_POSIX_THREADS
/* Use the POSIX threads library. */
# include <pthread.h>
# ifdef __cplusplus
extern "C" {
# endif
# if PTHREAD_IN_USE_DETECTION_HARD
/* The pthread_in_use() detection needs to be done at runtime. */
# define pthread_in_use() \
glthread_in_use ()
extern int glthread_in_use (void);
# endif
# if USE_POSIX_THREADS_WEAK
/* Use weak references to the POSIX threads library. */
/* Weak references avoid dragging in external libraries if the other parts
of the program don't use them. Here we use them, because we don't want
every program that uses libintl to depend on libpthread. This assumes
that libpthread would not be loaded after libintl; i.e. if libintl is
loaded first, by an executable that does not depend on libpthread, and
then a module is dynamically loaded that depends on libpthread, libintl
will not be multithread-safe. */
/* The way to test at runtime whether libpthread is present is to test
whether a function pointer's value, such as &pthread_mutex_init, is
non-NULL. However, some versions of GCC have a bug through which, in
PIC mode, &foo != NULL always evaluates to true if there is a direct
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_cond_init
# pragma weak pthread_cond_wait
# pragma weak pthread_cond_timedwait
# pragma weak pthread_cond_signal
# pragma weak pthread_cond_broadcast
# pragma weak pthread_cond_destroy
# ifndef pthread_self
# pragma weak pthread_self
# endif
# if !PTHREAD_IN_USE_DETECTION_HARD
# pragma weak pthread_mutexattr_gettype
# define pthread_in_use() \
(pthread_mutexattr_gettype != NULL || c11_threads_in_use ())
# endif
# else
# if !PTHREAD_IN_USE_DETECTION_HARD
# define pthread_in_use() 1
# endif
# endif
/* -------------------------- gl_cond_t datatype -------------------------- */
typedef pthread_cond_t gl_cond_t;
# define gl_cond_define(STORAGECLASS, NAME) \
STORAGECLASS gl_cond_t NAME;
# define gl_cond_define_initialized(STORAGECLASS, NAME) \
STORAGECLASS gl_cond_t NAME = gl_cond_initializer;
# define gl_cond_initializer \
PTHREAD_COND_INITIALIZER
# define glthread_cond_init(COND) \
(pthread_in_use () ? pthread_cond_init (COND, NULL) : 0)
# define glthread_cond_wait(COND, LOCK) \
(pthread_in_use () ? pthread_cond_wait (COND, LOCK) : 0)
# define glthread_cond_timedwait(COND, LOCK, ABSTIME) \
(pthread_in_use () ? pthread_cond_timedwait (COND, LOCK, ABSTIME) : 0)
# define glthread_cond_signal(COND) \
(pthread_in_use () ? pthread_cond_signal (COND) : 0)
# define glthread_cond_broadcast(COND) \
(pthread_in_use () ? pthread_cond_broadcast (COND) : 0)
# define glthread_cond_destroy(COND) \
(pthread_in_use () ? pthread_cond_destroy (COND) : 0)
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if USE_WINDOWS_THREADS
# define WIN32_LEAN_AND_MEAN /* avoid including junk */
# include <windows.h>
# include "windows-cond.h"
# ifdef __cplusplus
extern "C" {
# endif
/* -------------------------- gl_cond_t datatype -------------------------- */
typedef glwthread_cond_t gl_cond_t;
# define gl_cond_define(STORAGECLASS, NAME) \
STORAGECLASS gl_cond_t NAME;
# define gl_cond_define_initialized(STORAGECLASS, NAME) \
STORAGECLASS gl_cond_t NAME = gl_cond_initializer;
# define gl_cond_initializer \
GLWTHREAD_COND_INIT
# define glthread_cond_init(COND) \
glwthread_cond_init (COND)
# define glthread_cond_wait(COND, LOCK) \
glwthread_cond_wait (COND, LOCK, \
(int (*) (void *)) glwthread_mutex_lock, \
(int (*) (void *)) glwthread_mutex_unlock)
# define glthread_cond_timedwait(COND, LOCK, ABSTIME) \
glwthread_cond_timedwait (COND, LOCK, \
(int (*) (void *)) glwthread_mutex_lock, \
(int (*) (void *)) glwthread_mutex_unlock, \
ABSTIME)
# define glthread_cond_signal(COND) \
glwthread_cond_signal (COND)
# define glthread_cond_broadcast(COND) \
glwthread_cond_broadcast (COND)
# define glthread_cond_destroy(COND) \
glwthread_cond_destroy (COND)
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if !(USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS)
/* Provide dummy implementation if threads are not supported. */
typedef int gl_cond_t;
# define gl_cond_define(STORAGECLASS, NAME)
# define gl_cond_define_initialized(STORAGECLASS, NAME)
# define glthread_cond_init(COND) 0
# define glthread_cond_wait(COND, LOCK) 0
# define glthread_cond_timedwait(COND, LOCK, ABSTIME) 0
# define glthread_cond_signal(COND) 0
# define glthread_cond_broadcast(COND) 0
# define glthread_cond_destroy(COND) 0
#endif
/* ========================================================================= */
/* Macros with built-in error handling. */
#ifdef __cplusplus
extern "C" {
#endif
#define gl_cond_init(COND) \
do \
{ \
if (glthread_cond_init (&COND)) \
abort (); \
} \
while (0)
#define gl_cond_wait(COND, LOCK) \
do \
{ \
if (glthread_cond_wait (&COND, &LOCK)) \
abort (); \
} \
while (0)
#define gl_cond_timedwait(COND, LOCK, ABSTIME) \
gl_cond_timedwait_func (&COND, &LOCK, ABSTIME)
_GLTHREAD_COND_INLINE bool
gl_cond_timedwait_func (gl_cond_t *cond, gl_lock_t *lock, struct timespec *abstime)
{
int err = glthread_cond_timedwait (cond, lock, abstime);
if (err == ETIMEDOUT)
return true;
if (err != 0)
abort ();
return false;
}
#define gl_cond_signal(COND) \
do \
{ \
if (glthread_cond_signal (&COND)) \
abort (); \
} \
while (0)
#define gl_cond_broadcast(COND) \
do \
{ \
if (glthread_cond_broadcast (&COND)) \
abort (); \
} \
while (0)
#define gl_cond_destroy(COND) \
do \
{ \
if (glthread_cond_destroy (&COND)) \
abort (); \
} \
while (0)
#ifdef __cplusplus
}
#endif
_GL_INLINE_HEADER_END
#endif /* _GLTHREAD_COND_H */

View File

@@ -1,656 +0,0 @@
/* Locking in multithreaded situations.
Copyright (C) 2005-2024 Free Software Foundation, Inc.
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 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 Lesser General Public License for more details.
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-win32.h. */
/* This file contains locking primitives for use with a given thread library.
It does not contain primitives for creating threads or for other
synchronization primitives.
Normal (non-recursive) locks:
Type: gl_lock_t
Declaration: gl_lock_define(extern, name)
Initializer: gl_lock_define_initialized(, name)
Initialization: gl_lock_init (name);
Taking the lock: gl_lock_lock (name);
Releasing the lock: gl_lock_unlock (name);
De-initialization: gl_lock_destroy (name);
Equivalent functions with control of error handling:
Initialization: err = glthread_lock_init (&name);
Taking the lock: err = glthread_lock_lock (&name);
Releasing the lock: err = glthread_lock_unlock (&name);
De-initialization: err = glthread_lock_destroy (&name);
Read-Write (non-recursive) locks:
Type: gl_rwlock_t
Declaration: gl_rwlock_define(extern, name)
Initializer: gl_rwlock_define_initialized(, name)
Initialization: gl_rwlock_init (name);
Taking the lock: gl_rwlock_rdlock (name);
gl_rwlock_wrlock (name);
Releasing the lock: gl_rwlock_unlock (name);
De-initialization: gl_rwlock_destroy (name);
Equivalent functions with control of error handling:
Initialization: err = glthread_rwlock_init (&name);
Taking the lock: err = glthread_rwlock_rdlock (&name);
err = glthread_rwlock_wrlock (&name);
Releasing the lock: err = glthread_rwlock_unlock (&name);
De-initialization: err = glthread_rwlock_destroy (&name);
Recursive locks:
Type: gl_recursive_lock_t
Declaration: gl_recursive_lock_define(extern, name)
Initializer: gl_recursive_lock_define_initialized(, name)
Initialization: gl_recursive_lock_init (name);
Taking the lock: gl_recursive_lock_lock (name);
Releasing the lock: gl_recursive_lock_unlock (name);
De-initialization: gl_recursive_lock_destroy (name);
Equivalent functions with control of error handling:
Initialization: err = glthread_recursive_lock_init (&name);
Taking the lock: err = glthread_recursive_lock_lock (&name);
Releasing the lock: err = glthread_recursive_lock_unlock (&name);
De-initialization: err = glthread_recursive_lock_destroy (&name);
*/
#ifndef _LOCK_H
#define _LOCK_H
/* This file uses HAVE_THREADS_H, HAVE_PTHREAD_RWLOCK,
HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER,
PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP,
HAVE_PTHREAD_MUTEX_RECURSIVE. */
#if !_GL_CONFIG_H_INCLUDED
#error "Please include config.h first."
#endif
#include <errno.h>
#include <stdlib.h>
#include "glthread/once.h"
/* c11_threads_in_use() is defined in glthread/once.h. */
/* ========================================================================= */
#if USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS
/* Use the ISO C threads library. */
# include <threads.h>
# ifdef __cplusplus
extern "C" {
# endif
/* -------------------------- gl_lock_t datatype -------------------------- */
typedef struct
{
int volatile init_needed;
once_flag init_once;
void (*init_func) (void);
mtx_t mutex;
}
gl_lock_t;
# define gl_lock_define(STORAGECLASS, NAME) \
STORAGECLASS gl_lock_t NAME;
# define gl_lock_define_initialized(STORAGECLASS, NAME) \
static void _atomic_init_##NAME (void); \
STORAGECLASS gl_lock_t NAME = \
{ 1, ONCE_FLAG_INIT, _atomic_init_##NAME }; \
static void _atomic_init_##NAME (void) \
{ \
if (glthread_lock_init (&(NAME))) \
abort (); \
}
extern int glthread_lock_init (gl_lock_t *lock);
extern int glthread_lock_lock (gl_lock_t *lock);
extern int glthread_lock_unlock (gl_lock_t *lock);
extern int glthread_lock_destroy (gl_lock_t *lock);
/* ------------------------- gl_rwlock_t datatype ------------------------- */
typedef struct
{
int volatile init_needed;
once_flag init_once;
void (*init_func) (void);
mtx_t lock; /* protects the remaining fields */
cnd_t waiting_readers; /* waiting readers */
cnd_t waiting_writers; /* waiting writers */
unsigned int waiting_writers_count; /* number of waiting writers */
int runcount; /* number of readers running, or -1 when a writer runs */
}
gl_rwlock_t;
# define gl_rwlock_define(STORAGECLASS, NAME) \
STORAGECLASS gl_rwlock_t NAME;
# define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
static void _atomic_init_##NAME (void); \
STORAGECLASS gl_rwlock_t NAME = \
{ 1, ONCE_FLAG_INIT, _atomic_init_##NAME }; \
static void _atomic_init_##NAME (void) \
{ \
if (glthread_rwlock_init (&(NAME))) \
abort (); \
}
extern int glthread_rwlock_init (gl_rwlock_t *lock);
extern int glthread_rwlock_rdlock (gl_rwlock_t *lock);
extern int glthread_rwlock_wrlock (gl_rwlock_t *lock);
extern int glthread_rwlock_unlock (gl_rwlock_t *lock);
extern int glthread_rwlock_destroy (gl_rwlock_t *lock);
/* --------------------- gl_recursive_lock_t datatype --------------------- */
typedef struct
{
int volatile init_needed;
once_flag init_once;
void (*init_func) (void);
mtx_t mutex;
}
gl_recursive_lock_t;
# define gl_recursive_lock_define(STORAGECLASS, NAME) \
STORAGECLASS gl_recursive_lock_t NAME;
# define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
static void _atomic_init_##NAME (void); \
STORAGECLASS gl_recursive_lock_t NAME = \
{ 1, ONCE_FLAG_INIT, _atomic_init_##NAME }; \
static void _atomic_init_##NAME (void) \
{ \
if (glthread_recursive_lock_init (&(NAME))) \
abort (); \
}
extern int glthread_recursive_lock_init (gl_recursive_lock_t *lock);
extern int glthread_recursive_lock_lock (gl_recursive_lock_t *lock);
extern int glthread_recursive_lock_unlock (gl_recursive_lock_t *lock);
extern int glthread_recursive_lock_destroy (gl_recursive_lock_t *lock);
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if USE_POSIX_THREADS
/* Use the POSIX threads library. */
# include <pthread.h>
# ifdef __cplusplus
extern "C" {
# endif
/* pthread_in_use() is defined in glthread/once.h. */
/* -------------------------- gl_lock_t datatype -------------------------- */
typedef pthread_mutex_t gl_lock_t;
# define gl_lock_define(STORAGECLASS, NAME) \
STORAGECLASS pthread_mutex_t NAME;
# define gl_lock_define_initialized(STORAGECLASS, NAME) \
STORAGECLASS pthread_mutex_t NAME = gl_lock_initializer;
# define gl_lock_initializer \
PTHREAD_MUTEX_INITIALIZER
# define glthread_lock_init(LOCK) \
(pthread_in_use () ? pthread_mutex_init (LOCK, NULL) : 0)
# define glthread_lock_lock(LOCK) \
(pthread_in_use () ? pthread_mutex_lock (LOCK) : 0)
# define glthread_lock_unlock(LOCK) \
(pthread_in_use () ? pthread_mutex_unlock (LOCK) : 0)
# define glthread_lock_destroy(LOCK) \
(pthread_in_use () ? pthread_mutex_destroy (LOCK) : 0)
/* ------------------------- gl_rwlock_t datatype ------------------------- */
# if HAVE_PTHREAD_RWLOCK && (HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER || (defined PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP && (__GNU_LIBRARY__ > 1)))
# if defined PTHREAD_RWLOCK_INITIALIZER || defined PTHREAD_RWLOCK_INITIALIZER_NP
typedef pthread_rwlock_t gl_rwlock_t;
# define gl_rwlock_define(STORAGECLASS, NAME) \
STORAGECLASS pthread_rwlock_t NAME;
# define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
STORAGECLASS pthread_rwlock_t NAME = gl_rwlock_initializer;
# if HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER
# if defined PTHREAD_RWLOCK_INITIALIZER
# define gl_rwlock_initializer \
PTHREAD_RWLOCK_INITIALIZER
# else
# define gl_rwlock_initializer \
PTHREAD_RWLOCK_INITIALIZER_NP
# endif
# define glthread_rwlock_init(LOCK) \
(pthread_in_use () ? pthread_rwlock_init (LOCK, NULL) : 0)
# else /* glibc with bug https://sourceware.org/bugzilla/show_bug.cgi?id=13701 */
# define gl_rwlock_initializer \
PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
# define glthread_rwlock_init(LOCK) \
(pthread_in_use () ? glthread_rwlock_init_for_glibc (LOCK) : 0)
extern int glthread_rwlock_init_for_glibc (pthread_rwlock_t *lock);
# endif
# define glthread_rwlock_rdlock(LOCK) \
(pthread_in_use () ? pthread_rwlock_rdlock (LOCK) : 0)
# define glthread_rwlock_wrlock(LOCK) \
(pthread_in_use () ? pthread_rwlock_wrlock (LOCK) : 0)
# define glthread_rwlock_unlock(LOCK) \
(pthread_in_use () ? pthread_rwlock_unlock (LOCK) : 0)
# define glthread_rwlock_destroy(LOCK) \
(pthread_in_use () ? pthread_rwlock_destroy (LOCK) : 0)
# else
typedef struct
{
int initialized;
pthread_mutex_t guard; /* protects the initialization */
pthread_rwlock_t rwlock; /* read-write lock */
}
gl_rwlock_t;
# define gl_rwlock_define(STORAGECLASS, NAME) \
STORAGECLASS gl_rwlock_t NAME;
# define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
# define gl_rwlock_initializer \
{ 0, PTHREAD_MUTEX_INITIALIZER }
# define glthread_rwlock_init(LOCK) \
(pthread_in_use () ? glthread_rwlock_init_multithreaded (LOCK) : 0)
# define glthread_rwlock_rdlock(LOCK) \
(pthread_in_use () ? glthread_rwlock_rdlock_multithreaded (LOCK) : 0)
# define glthread_rwlock_wrlock(LOCK) \
(pthread_in_use () ? glthread_rwlock_wrlock_multithreaded (LOCK) : 0)
# define glthread_rwlock_unlock(LOCK) \
(pthread_in_use () ? glthread_rwlock_unlock_multithreaded (LOCK) : 0)
# define glthread_rwlock_destroy(LOCK) \
(pthread_in_use () ? glthread_rwlock_destroy_multithreaded (LOCK) : 0)
extern int glthread_rwlock_init_multithreaded (gl_rwlock_t *lock);
extern int glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock);
extern int glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock);
extern int glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock);
extern int glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock);
# endif
# else
typedef struct
{
pthread_mutex_t lock; /* protects the remaining fields */
pthread_cond_t waiting_readers; /* waiting readers */
pthread_cond_t waiting_writers; /* waiting writers */
unsigned int waiting_writers_count; /* number of waiting writers */
int runcount; /* number of readers running, or -1 when a writer runs */
}
gl_rwlock_t;
# define gl_rwlock_define(STORAGECLASS, NAME) \
STORAGECLASS gl_rwlock_t NAME;
# define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
# define gl_rwlock_initializer \
{ PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0 }
# define glthread_rwlock_init(LOCK) \
(pthread_in_use () ? glthread_rwlock_init_multithreaded (LOCK) : 0)
# define glthread_rwlock_rdlock(LOCK) \
(pthread_in_use () ? glthread_rwlock_rdlock_multithreaded (LOCK) : 0)
# define glthread_rwlock_wrlock(LOCK) \
(pthread_in_use () ? glthread_rwlock_wrlock_multithreaded (LOCK) : 0)
# define glthread_rwlock_unlock(LOCK) \
(pthread_in_use () ? glthread_rwlock_unlock_multithreaded (LOCK) : 0)
# define glthread_rwlock_destroy(LOCK) \
(pthread_in_use () ? glthread_rwlock_destroy_multithreaded (LOCK) : 0)
extern int glthread_rwlock_init_multithreaded (gl_rwlock_t *lock);
extern int glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock);
extern int glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock);
extern int glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock);
extern int glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock);
# endif
/* --------------------- gl_recursive_lock_t datatype --------------------- */
# if HAVE_PTHREAD_MUTEX_RECURSIVE
# if defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER || defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
typedef pthread_mutex_t gl_recursive_lock_t;
# define gl_recursive_lock_define(STORAGECLASS, NAME) \
STORAGECLASS pthread_mutex_t NAME;
# define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
STORAGECLASS pthread_mutex_t NAME = gl_recursive_lock_initializer;
# ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER
# define gl_recursive_lock_initializer \
PTHREAD_RECURSIVE_MUTEX_INITIALIZER
# else
# define gl_recursive_lock_initializer \
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
# endif
# define glthread_recursive_lock_init(LOCK) \
(pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
# define glthread_recursive_lock_lock(LOCK) \
(pthread_in_use () ? pthread_mutex_lock (LOCK) : 0)
# define glthread_recursive_lock_unlock(LOCK) \
(pthread_in_use () ? pthread_mutex_unlock (LOCK) : 0)
# define glthread_recursive_lock_destroy(LOCK) \
(pthread_in_use () ? pthread_mutex_destroy (LOCK) : 0)
extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
# else
typedef struct
{
pthread_mutex_t recmutex; /* recursive mutex */
pthread_mutex_t guard; /* protects the initialization */
int initialized;
}
gl_recursive_lock_t;
# define gl_recursive_lock_define(STORAGECLASS, NAME) \
STORAGECLASS gl_recursive_lock_t NAME;
# define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
# define gl_recursive_lock_initializer \
{ PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, 0 }
# define glthread_recursive_lock_init(LOCK) \
(pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
# define glthread_recursive_lock_lock(LOCK) \
(pthread_in_use () ? glthread_recursive_lock_lock_multithreaded (LOCK) : 0)
# define glthread_recursive_lock_unlock(LOCK) \
(pthread_in_use () ? glthread_recursive_lock_unlock_multithreaded (LOCK) : 0)
# define glthread_recursive_lock_destroy(LOCK) \
(pthread_in_use () ? glthread_recursive_lock_destroy_multithreaded (LOCK) : 0)
extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
extern int glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock);
extern int glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock);
extern int glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock);
# endif
# else
/* Old versions of POSIX threads on Solaris did not have recursive locks.
We have to implement them ourselves. */
typedef struct
{
pthread_mutex_t mutex;
pthread_t owner;
unsigned long depth;
}
gl_recursive_lock_t;
# define gl_recursive_lock_define(STORAGECLASS, NAME) \
STORAGECLASS gl_recursive_lock_t NAME;
# define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
# define gl_recursive_lock_initializer \
{ PTHREAD_MUTEX_INITIALIZER, (pthread_t) 0, 0 }
# define glthread_recursive_lock_init(LOCK) \
(pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
# define glthread_recursive_lock_lock(LOCK) \
(pthread_in_use () ? glthread_recursive_lock_lock_multithreaded (LOCK) : 0)
# define glthread_recursive_lock_unlock(LOCK) \
(pthread_in_use () ? glthread_recursive_lock_unlock_multithreaded (LOCK) : 0)
# define glthread_recursive_lock_destroy(LOCK) \
(pthread_in_use () ? glthread_recursive_lock_destroy_multithreaded (LOCK) : 0)
extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
extern int glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock);
extern int glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock);
extern int glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock);
# endif
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if USE_WINDOWS_THREADS
# define WIN32_LEAN_AND_MEAN /* avoid including junk */
# include <windows.h>
# include "windows-mutex.h"
# include "windows-rwlock.h"
# include "windows-recmutex.h"
# ifdef __cplusplus
extern "C" {
# endif
/* We can use CRITICAL_SECTION directly, rather than the native Windows Event,
Mutex, Semaphore types, because
- we need only to synchronize inside a single process (address space),
not inter-process locking,
- we don't need to support trylock operations. (TryEnterCriticalSection
does not work on Windows 95/98/ME. Packages that need trylock usually
define their own mutex type.) */
/* There is no way to statically initialize a CRITICAL_SECTION. It needs
to be done lazily, once only. For this we need spinlocks. */
/* -------------------------- gl_lock_t datatype -------------------------- */
typedef glwthread_mutex_t gl_lock_t;
# define gl_lock_define(STORAGECLASS, NAME) \
STORAGECLASS gl_lock_t NAME;
# define gl_lock_define_initialized(STORAGECLASS, NAME) \
STORAGECLASS gl_lock_t NAME = gl_lock_initializer;
# define gl_lock_initializer \
GLWTHREAD_MUTEX_INIT
# define glthread_lock_init(LOCK) \
(glwthread_mutex_init (LOCK), 0)
# define glthread_lock_lock(LOCK) \
glwthread_mutex_lock (LOCK)
# define glthread_lock_unlock(LOCK) \
glwthread_mutex_unlock (LOCK)
# define glthread_lock_destroy(LOCK) \
glwthread_mutex_destroy (LOCK)
/* ------------------------- gl_rwlock_t datatype ------------------------- */
typedef glwthread_rwlock_t gl_rwlock_t;
# define gl_rwlock_define(STORAGECLASS, NAME) \
STORAGECLASS gl_rwlock_t NAME;
# define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
# define gl_rwlock_initializer \
GLWTHREAD_RWLOCK_INIT
# define glthread_rwlock_init(LOCK) \
(glwthread_rwlock_init (LOCK), 0)
# define glthread_rwlock_rdlock(LOCK) \
glwthread_rwlock_rdlock (LOCK)
# define glthread_rwlock_wrlock(LOCK) \
glwthread_rwlock_wrlock (LOCK)
# define glthread_rwlock_unlock(LOCK) \
glwthread_rwlock_unlock (LOCK)
# define glthread_rwlock_destroy(LOCK) \
glwthread_rwlock_destroy (LOCK)
/* --------------------- gl_recursive_lock_t datatype --------------------- */
typedef glwthread_recmutex_t gl_recursive_lock_t;
# define gl_recursive_lock_define(STORAGECLASS, NAME) \
STORAGECLASS gl_recursive_lock_t NAME;
# define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
# define gl_recursive_lock_initializer \
GLWTHREAD_RECMUTEX_INIT
# define glthread_recursive_lock_init(LOCK) \
(glwthread_recmutex_init (LOCK), 0)
# define glthread_recursive_lock_lock(LOCK) \
glwthread_recmutex_lock (LOCK)
# define glthread_recursive_lock_unlock(LOCK) \
glwthread_recmutex_unlock (LOCK)
# define glthread_recursive_lock_destroy(LOCK) \
glwthread_recmutex_destroy (LOCK)
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if !(USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS)
/* Provide dummy implementation if threads are not supported. */
/* -------------------------- gl_lock_t datatype -------------------------- */
typedef int gl_lock_t;
# define gl_lock_define(STORAGECLASS, NAME)
# define gl_lock_define_initialized(STORAGECLASS, NAME)
# define glthread_lock_init(NAME) 0
# define glthread_lock_lock(NAME) 0
# define glthread_lock_unlock(NAME) 0
# define glthread_lock_destroy(NAME) 0
/* ------------------------- gl_rwlock_t datatype ------------------------- */
typedef int gl_rwlock_t;
# define gl_rwlock_define(STORAGECLASS, NAME)
# define gl_rwlock_define_initialized(STORAGECLASS, NAME)
# define glthread_rwlock_init(NAME) 0
# define glthread_rwlock_rdlock(NAME) 0
# define glthread_rwlock_wrlock(NAME) 0
# define glthread_rwlock_unlock(NAME) 0
# define glthread_rwlock_destroy(NAME) 0
/* --------------------- gl_recursive_lock_t datatype --------------------- */
typedef int gl_recursive_lock_t;
# define gl_recursive_lock_define(STORAGECLASS, NAME)
# define gl_recursive_lock_define_initialized(STORAGECLASS, NAME)
# define glthread_recursive_lock_init(NAME) 0
# define glthread_recursive_lock_lock(NAME) 0
# define glthread_recursive_lock_unlock(NAME) 0
# define glthread_recursive_lock_destroy(NAME) 0
#endif
/* ========================================================================= */
/* Macros with built-in error handling. */
/* -------------------------- gl_lock_t datatype -------------------------- */
#define gl_lock_init(NAME) \
do \
{ \
if (glthread_lock_init (&NAME)) \
abort (); \
} \
while (0)
#define gl_lock_lock(NAME) \
do \
{ \
if (glthread_lock_lock (&NAME)) \
abort (); \
} \
while (0)
#define gl_lock_unlock(NAME) \
do \
{ \
if (glthread_lock_unlock (&NAME)) \
abort (); \
} \
while (0)
#define gl_lock_destroy(NAME) \
do \
{ \
if (glthread_lock_destroy (&NAME)) \
abort (); \
} \
while (0)
/* ------------------------- gl_rwlock_t datatype ------------------------- */
#define gl_rwlock_init(NAME) \
do \
{ \
if (glthread_rwlock_init (&NAME)) \
abort (); \
} \
while (0)
#define gl_rwlock_rdlock(NAME) \
do \
{ \
if (glthread_rwlock_rdlock (&NAME)) \
abort (); \
} \
while (0)
#define gl_rwlock_wrlock(NAME) \
do \
{ \
if (glthread_rwlock_wrlock (&NAME)) \
abort (); \
} \
while (0)
#define gl_rwlock_unlock(NAME) \
do \
{ \
if (glthread_rwlock_unlock (&NAME)) \
abort (); \
} \
while (0)
#define gl_rwlock_destroy(NAME) \
do \
{ \
if (glthread_rwlock_destroy (&NAME)) \
abort (); \
} \
while (0)
/* --------------------- gl_recursive_lock_t datatype --------------------- */
#define gl_recursive_lock_init(NAME) \
do \
{ \
if (glthread_recursive_lock_init (&NAME)) \
abort (); \
} \
while (0)
#define gl_recursive_lock_lock(NAME) \
do \
{ \
if (glthread_recursive_lock_lock (&NAME)) \
abort (); \
} \
while (0)
#define gl_recursive_lock_unlock(NAME) \
do \
{ \
if (glthread_recursive_lock_unlock (&NAME)) \
abort (); \
} \
while (0)
#define gl_recursive_lock_destroy(NAME) \
do \
{ \
if (glthread_recursive_lock_destroy (&NAME)) \
abort (); \
} \
while (0)
/* ========================================================================= */
#endif /* _LOCK_H */

View File

@@ -1,272 +0,0 @@
/* Once-only initialization in multithreaded situations.
Copyright (C) 2005-2024 Free Software Foundation, Inc.
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 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 Lesser General Public License for more details.
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-win32.h. */
/* This file contains once-only initialization primitives for use with a given
thread library.
It does not contain primitives for creating threads or for other
synchronization primitives.
Once-only execution:
Type: gl_once_t
Initializer: gl_once_define(extern, name)
Execution: gl_once (name, initfunction);
Equivalent functions with control of error handling:
Execution: err = glthread_once (&name, initfunction);
*/
#ifndef _ONCE_H
#define _ONCE_H
/* This file uses HAVE_THREADS_H. */
#if !_GL_CONFIG_H_INCLUDED
#error "Please include config.h first."
#endif
#include <errno.h>
#include <stdlib.h>
#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
/* ========================================================================= */
#if USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS
/* Use the ISO C threads library. */
# include <threads.h>
# ifdef __cplusplus
extern "C" {
# endif
/* -------------------------- gl_once_t datatype -------------------------- */
typedef once_flag gl_once_t;
# define gl_once_define(STORAGECLASS, NAME) \
STORAGECLASS once_flag NAME = ONCE_FLAG_INIT;
# define glthread_once(ONCE_CONTROL, INITFUNCTION) \
(call_once (ONCE_CONTROL, INITFUNCTION), 0)
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if USE_POSIX_THREADS
/* Use the POSIX threads library. */
# include <pthread.h>
# ifdef __cplusplus
extern "C" {
# endif
# if PTHREAD_IN_USE_DETECTION_HARD
/* The pthread_in_use() detection needs to be done at runtime. */
# define pthread_in_use() \
glthread_in_use ()
extern int glthread_in_use (void);
# endif
# if USE_POSIX_THREADS_WEAK
/* Use weak references to the POSIX threads library. */
/* Weak references avoid dragging in external libraries if the other parts
of the program don't use them. Here we use them, because we don't want
every program that uses libintl to depend on libpthread. This assumes
that libpthread would not be loaded after libintl; i.e. if libintl is
loaded first, by an executable that does not depend on libpthread, and
then a module is dynamically loaded that depends on libpthread, libintl
will not be multithread-safe. */
/* The way to test at runtime whether libpthread is present is to test
whether a function pointer's value, such as &pthread_mutex_init, is
non-NULL. However, some versions of GCC have a bug through which, in
PIC mode, &foo != NULL always evaluates to true if there is a direct
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_mutex_init
# pragma weak pthread_mutex_lock
# pragma weak pthread_mutex_unlock
# pragma weak pthread_mutex_destroy
/* Work around clang bug <https://github.com/llvm/llvm-project/issues/104670> */
# ifndef pthread_rwlock_init
# pragma weak pthread_rwlock_init
# endif
# pragma weak pthread_rwlock_rdlock
# pragma weak pthread_rwlock_wrlock
# pragma weak pthread_rwlock_unlock
# pragma weak pthread_rwlock_destroy
# pragma weak pthread_once
# pragma weak pthread_cond_init
# pragma weak pthread_cond_wait
# pragma weak pthread_cond_signal
# pragma weak pthread_cond_broadcast
# pragma weak pthread_cond_destroy
# pragma weak pthread_mutexattr_init
# pragma weak pthread_mutexattr_settype
# pragma weak pthread_mutexattr_destroy
/* Work around clang bug <https://github.com/llvm/llvm-project/issues/104670> */
# ifndef pthread_rwlockattr_init
# pragma weak pthread_rwlockattr_init
# endif
# if __GNU_LIBRARY__ > 1
# pragma weak pthread_rwlockattr_setkind_np
# endif
# pragma weak pthread_rwlockattr_destroy
# ifndef pthread_self
# pragma weak pthread_self
# endif
# if !PTHREAD_IN_USE_DETECTION_HARD
/* Considering all platforms with USE_POSIX_THREADS_WEAK, only few symbols
can be used to determine whether libpthread is in use. These are:
pthread_mutexattr_gettype
pthread_rwlockattr_destroy
pthread_rwlockattr_init
*/
# pragma weak pthread_mutexattr_gettype
# define pthread_in_use() \
(pthread_mutexattr_gettype != NULL || c11_threads_in_use ())
# endif
# else
# if !PTHREAD_IN_USE_DETECTION_HARD
# define pthread_in_use() 1
# endif
# endif
/* -------------------------- gl_once_t datatype -------------------------- */
typedef pthread_once_t gl_once_t;
# define gl_once_define(STORAGECLASS, NAME) \
STORAGECLASS pthread_once_t NAME = PTHREAD_ONCE_INIT;
# if PTHREAD_IN_USE_DETECTION_HARD || USE_POSIX_THREADS_WEAK
# define glthread_once(ONCE_CONTROL, INITFUNCTION) \
(pthread_in_use () \
? pthread_once (ONCE_CONTROL, INITFUNCTION) \
: (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0))
# else
# define glthread_once(ONCE_CONTROL, INITFUNCTION) \
(pthread_in_use () \
? glthread_once_multithreaded (ONCE_CONTROL, INITFUNCTION) \
: (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0))
extern int glthread_once_multithreaded (pthread_once_t *once_control,
void (*init_function) (void));
# endif
extern int glthread_once_singlethreaded (pthread_once_t *once_control);
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if USE_WINDOWS_THREADS
# define WIN32_LEAN_AND_MEAN /* avoid including junk */
# include <windows.h>
# include "windows-once.h"
# ifdef __cplusplus
extern "C" {
# endif
/* We can use CRITICAL_SECTION directly, rather than the native Windows Event,
Mutex, Semaphore types, because
- we need only to synchronize inside a single process (address space),
not inter-process locking,
- we don't need to support trylock operations. (TryEnterCriticalSection
does not work on Windows 95/98/ME. Packages that need trylock usually
define their own mutex type.) */
/* There is no way to statically initialize a CRITICAL_SECTION. It needs
to be done lazily, once only. For this we need spinlocks. */
/* -------------------------- gl_once_t datatype -------------------------- */
typedef glwthread_once_t gl_once_t;
# define gl_once_define(STORAGECLASS, NAME) \
STORAGECLASS gl_once_t NAME = GLWTHREAD_ONCE_INIT;
# define glthread_once(ONCE_CONTROL, INITFUNCTION) \
(glwthread_once (ONCE_CONTROL, INITFUNCTION), 0)
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if !(USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS)
/* Provide dummy implementation if threads are not supported. */
/* -------------------------- gl_once_t datatype -------------------------- */
typedef int gl_once_t;
# define gl_once_define(STORAGECLASS, NAME) \
STORAGECLASS gl_once_t NAME = 0;
# define glthread_once(ONCE_CONTROL, INITFUNCTION) \
(*(ONCE_CONTROL) == 0 ? (*(ONCE_CONTROL) = ~ 0, INITFUNCTION (), 0) : 0)
#endif
/* ========================================================================= */
/* Macros with built-in error handling. */
/* -------------------------- gl_once_t datatype -------------------------- */
#define gl_once(NAME, INITFUNCTION) \
do \
{ \
if (glthread_once (&NAME, INITFUNCTION)) \
abort (); \
} \
while (0)
/* ========================================================================= */
#endif /* _ONCE_H */

View File

@@ -1,346 +0,0 @@
/* Creating and controlling threads.
Copyright (C) 2005-2024 Free Software Foundation, Inc.
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 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 Lesser General Public License for more details.
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-win32.h. */
/* This file contains primitives for creating and controlling threads.
Thread data type: gl_thread_t.
Creating a thread:
thread = gl_thread_create (func, arg);
Or with control of error handling:
err = glthread_create (&thread, func, arg);
extern int glthread_create (gl_thread_t *result,
void *(*func) (void *), void *arg);
Querying and changing the signal mask of a thread (not supported on all
platforms):
gl_thread_sigmask (how, newmask, oldmask);
Or with control of error handling:
err = glthread_sigmask (how, newmask, oldmask);
extern int glthread_sigmask (int how, const sigset_t *newmask, sigset_t *oldmask);
Waiting for termination of another thread:
gl_thread_join (thread, &return_value);
Or with control of error handling:
err = glthread_join (thread, &return_value);
extern int glthread_join (gl_thread_t thread, void **return_value_ptr);
Getting a reference to the current thread:
current = gl_thread_self ();
extern gl_thread_t gl_thread_self (void);
Getting a reference to the current thread as a pointer, for debugging:
ptr = gl_thread_self_pointer ();
extern void * gl_thread_self_pointer (void);
Terminating the current thread:
gl_thread_exit (return_value);
extern _Noreturn void gl_thread_exit (void *return_value);
Requesting custom code to be executed at fork() time (not supported on all
platforms):
gl_thread_atfork (prepare_func, parent_func, child_func);
Or with control of error handling:
err = glthread_atfork (prepare_func, parent_func, child_func);
extern int glthread_atfork (void (*prepare_func) (void),
void (*parent_func) (void),
void (*child_func) (void));
Note that even on platforms where this is supported, use of fork() and
threads together is problematic, see
<https://lists.gnu.org/r/bug-gnulib/2008-08/msg00062.html>
*/
#ifndef _GLTHREAD_THREAD_H
#define _GLTHREAD_THREAD_H
/* This file uses _Noreturn, HAVE_THREADS_H, HAVE_PTHREAD_ATFORK. */
#if !_GL_CONFIG_H_INCLUDED
#error "Please include config.h first."
#endif
#include <errno.h>
#include <stdlib.h>
#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
/* ========================================================================= */
#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 || USE_ISOC_AND_POSIX_THREADS
/* Use the POSIX threads library. */
# include <pthread.h>
/* Get intptr_t. */
# include <stdint.h>
/* On IRIX, pthread_atfork is declared in <unistd.h>, not in <pthread.h>. */
# if defined __sgi
# include <unistd.h>
# endif
# if USE_POSIX_THREADS_WEAK
/* Compilers other than GCC need to see the declaration of pthread_sigmask
before the "#pragma weak pthread_sigmask" below. */
# include <signal.h>
# endif
# ifdef __cplusplus
extern "C" {
# endif
# if PTHREAD_IN_USE_DETECTION_HARD
/* The pthread_in_use() detection needs to be done at runtime. */
# define pthread_in_use() \
glthread_in_use ()
extern int glthread_in_use (void);
# endif
# if USE_POSIX_THREADS_WEAK
/* Use weak references to the POSIX threads library. */
/* Weak references avoid dragging in external libraries if the other parts
of the program don't use them. Here we use them, because we don't want
every program that uses libintl to depend on libpthread. This assumes
that libpthread would not be loaded after libintl; i.e. if libintl is
loaded first, by an executable that does not depend on libpthread, and
then a module is dynamically loaded that depends on libpthread, libintl
will not be multithread-safe. */
/* The way to test at runtime whether libpthread is present is to test
whether a function pointer's value, such as &pthread_mutex_init, is
non-NULL. However, some versions of GCC have a bug through which, in
PIC mode, &foo != NULL always evaluates to true if there is a direct
call to foo(...) in the same function. To avoid this, we test the
address of a function in libpthread that we don't use. */
# ifndef pthread_sigmask /* Do not declare rpl_pthread_sigmask weak. */
# pragma weak pthread_sigmask
# endif
# pragma weak pthread_join
# ifndef pthread_self
# pragma weak pthread_self
# endif
# pragma weak pthread_exit
# if HAVE_PTHREAD_ATFORK
# pragma weak pthread_atfork
# endif
# if !PTHREAD_IN_USE_DETECTION_HARD
# pragma weak pthread_mutexattr_gettype
# define pthread_in_use() \
(pthread_mutexattr_gettype != NULL || c11_threads_in_use ())
# endif
# else
# if !PTHREAD_IN_USE_DETECTION_HARD
# define pthread_in_use() 1
# endif
# endif
/* -------------------------- gl_thread_t datatype -------------------------- */
/* This choice of gl_thread_t assumes that
pthread_equal (a, b) is equivalent to ((a) == (b)).
This is the case on all platforms in use in 2008. */
typedef pthread_t gl_thread_t;
# define glthread_create(THREADP, FUNC, ARG) \
(pthread_in_use () ? pthread_create (THREADP, NULL, FUNC, ARG) : ENOSYS)
# define glthread_sigmask(HOW, SET, OSET) \
(pthread_in_use () ? pthread_sigmask (HOW, SET, OSET) : 0)
# define glthread_join(THREAD, RETVALP) \
(pthread_in_use () ? pthread_join (THREAD, RETVALP) : 0)
# ifdef PTW32_VERSION
/* In pthreads-win32, pthread_t is a struct with a pointer field 'p' and
other fields. */
# define gl_thread_self() \
(pthread_in_use () ? pthread_self () : gl_null_thread)
# define gl_thread_self_pointer() \
(pthread_in_use () ? pthread_self ().p : NULL)
extern const gl_thread_t gl_null_thread;
# elif defined __MVS__
/* On IBM z/OS, pthread_t is a struct with an 8-byte '__' field.
The first three bytes of this field appear to uniquely identify a
pthread_t, though not necessarily representing a pointer. */
# define gl_thread_self() \
(pthread_in_use () ? pthread_self () : gl_null_thread)
# define gl_thread_self_pointer() \
(pthread_in_use () ? *((void **) pthread_self ().__) : NULL)
extern const gl_thread_t gl_null_thread;
# else
# define gl_thread_self() \
(pthread_in_use () ? pthread_self () : (pthread_t) 0)
# define gl_thread_self_pointer() \
(pthread_in_use () ? (void *) (intptr_t) (pthread_t) pthread_self () : NULL)
# endif
# define gl_thread_exit(RETVAL) \
(void) (pthread_in_use () ? (pthread_exit (RETVAL), 0) : 0)
# if HAVE_PTHREAD_ATFORK
# define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) \
(pthread_in_use () ? pthread_atfork (PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) : 0)
# else
# define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
# endif
# 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 -------------------------- */
typedef glwthread_thread_t gl_thread_t;
# define glthread_create(THREADP, FUNC, ARG) \
glwthread_thread_create (THREADP, 0, FUNC, ARG)
# define glthread_sigmask(HOW, SET, OSET) \
/* unsupported */ 0
# define glthread_join(THREAD, RETVALP) \
glwthread_thread_join (THREAD, RETVALP)
# define gl_thread_self() \
glwthread_thread_self ()
# define gl_thread_self_pointer() \
gl_thread_self ()
# define gl_thread_exit(RETVAL) \
glwthread_thread_exit (RETVAL)
# define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if !(USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS)
/* Provide dummy implementation if threads are not supported. */
typedef int gl_thread_t;
# define glthread_create(THREADP, FUNC, ARG) ENOSYS
# define glthread_sigmask(HOW, SET, OSET) 0
# define glthread_join(THREAD, RETVALP) 0
# define gl_thread_self() 0
# define gl_thread_self_pointer() \
((void *) gl_thread_self ())
# define gl_thread_exit(RETVAL) (void)0
# define glthread_atfork(PREPARE_FUNC, PARENT_FUNC, CHILD_FUNC) 0
#endif
/* ========================================================================= */
/* Macros with built-in error handling. */
#ifdef __cplusplus
extern "C" {
#endif
extern gl_thread_t gl_thread_create (void *(*func) (void *arg), void *arg);
#define gl_thread_sigmask(HOW, SET, OSET) \
do \
{ \
if (glthread_sigmask (HOW, SET, OSET)) \
abort (); \
} \
while (0)
#define gl_thread_join(THREAD, RETVAL) \
do \
{ \
if (glthread_join (THREAD, RETVAL)) \
abort (); \
} \
while (0)
#define gl_thread_atfork(PREPARE, PARENT, CHILD) \
do \
{ \
if (glthread_atfork (PREPARE, PARENT, CHILD)) \
abort (); \
} \
while (0)
#ifdef __cplusplus
}
#endif
#endif /* _GLTHREAD_THREAD_H */

View File

@@ -1,267 +0,0 @@
/* Thread-local storage in multithreaded situations.
Copyright (C) 2005, 2007-2024 Free Software Foundation, Inc.
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 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 Lesser General Public License for more details.
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. */
/* This file contains thread-local storage primitives for use with a given
thread library. It does not contain primitives for creating threads or
for other multithreading primitives.
Type: gl_tls_key_t
Initialization: gl_tls_key_init (name, destructor);
Getting per-thread value: gl_tls_get (name)
Setting per-thread value: gl_tls_set (name, pointer);
De-initialization: gl_tls_key_destroy (name);
Equivalent functions with control of error handling:
Initialization: err = glthread_tls_key_init (&name, destructor);
Setting per-thread value: err = glthread_tls_set (&name, pointer);
De-initialization: err = glthread_tls_key_destroy (&name);
A per-thread value is of type 'void *'.
A destructor is a function pointer of type 'void (*) (void *)', called
when a thread exits, and taking the last per-thread value as argument. It
is unspecified whether the destructor function is called when the last
per-thread value is NULL. On some platforms, the destructor function is
not called at all.
*/
#ifndef _TLS_H
#define _TLS_H
/* This file uses HAVE_THREADS_H. */
#if !_GL_CONFIG_H_INCLUDED
#error "Please include config.h first."
#endif
#include <errno.h>
#include <stdlib.h>
#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
/* ========================================================================= */
#if USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS
/* Use the ISO C threads library. */
# include <threads.h>
# ifdef __cplusplus
extern "C" {
# endif
/* ------------------------- gl_tls_key_t datatype ------------------------- */
typedef tss_t gl_tls_key_t;
# define glthread_tls_key_init(KEY, DESTRUCTOR) \
(tss_create (KEY, DESTRUCTOR) != thrd_success ? EAGAIN : 0)
# define gl_tls_get(NAME) \
tss_get (NAME)
# define glthread_tls_set(KEY, POINTER) \
(tss_set (*(KEY), (POINTER)) != thrd_success ? ENOMEM : 0)
# define glthread_tls_key_destroy(KEY) \
(tss_delete (*(KEY)), 0)
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if USE_POSIX_THREADS
/* Use the POSIX threads library. */
# include <pthread.h>
# ifdef __cplusplus
extern "C" {
# endif
# if PTHREAD_IN_USE_DETECTION_HARD
/* The pthread_in_use() detection needs to be done at runtime. */
# define pthread_in_use() \
glthread_in_use ()
extern int glthread_in_use (void);
# endif
# if USE_POSIX_THREADS_WEAK
/* Use weak references to the POSIX threads library. */
# pragma weak pthread_key_create
# pragma weak pthread_getspecific
# pragma weak pthread_setspecific
# pragma weak pthread_key_delete
# ifndef pthread_self
# pragma weak pthread_self
# endif
# if !PTHREAD_IN_USE_DETECTION_HARD
# pragma weak pthread_mutexattr_gettype
# define pthread_in_use() \
(pthread_mutexattr_gettype != NULL || c11_threads_in_use ())
# endif
# else
# if !PTHREAD_IN_USE_DETECTION_HARD
# define pthread_in_use() 1
# endif
# endif
/* ------------------------- gl_tls_key_t datatype ------------------------- */
typedef union
{
void *singlethread_value;
pthread_key_t key;
}
gl_tls_key_t;
# define glthread_tls_key_init(KEY, DESTRUCTOR) \
(pthread_in_use () \
? pthread_key_create (&(KEY)->key, DESTRUCTOR) \
: ((KEY)->singlethread_value = NULL, 0))
# define gl_tls_get(NAME) \
(pthread_in_use () \
? pthread_getspecific ((NAME).key) \
: (NAME).singlethread_value)
# define glthread_tls_set(KEY, POINTER) \
(pthread_in_use () \
? pthread_setspecific ((KEY)->key, (POINTER)) \
: ((KEY)->singlethread_value = (POINTER), 0))
# define glthread_tls_key_destroy(KEY) \
(pthread_in_use () ? pthread_key_delete ((KEY)->key) : 0)
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if USE_WINDOWS_THREADS
# define WIN32_LEAN_AND_MEAN /* avoid including junk */
# include <windows.h>
# include "windows-tls.h"
# ifdef __cplusplus
extern "C" {
# endif
/* ------------------------- gl_tls_key_t datatype ------------------------- */
typedef glwthread_tls_key_t gl_tls_key_t;
# define glthread_tls_key_init(KEY, DESTRUCTOR) \
glwthread_tls_key_create (KEY, DESTRUCTOR)
# define gl_tls_get(NAME) \
TlsGetValue (NAME)
# define glthread_tls_set(KEY, POINTER) \
(!TlsSetValue (*(KEY), POINTER) ? EINVAL : 0)
# define glthread_tls_key_destroy(KEY) \
glwthread_tls_key_delete (*(KEY))
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if !(USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS)
/* Provide dummy implementation if threads are not supported. */
# ifdef __cplusplus
extern "C" {
# endif
/* ------------------------- gl_tls_key_t datatype ------------------------- */
typedef struct
{
void *singlethread_value;
}
gl_tls_key_t;
# define glthread_tls_key_init(KEY, DESTRUCTOR) \
((KEY)->singlethread_value = NULL, \
(void) (DESTRUCTOR), \
0)
# define gl_tls_get(NAME) \
(NAME).singlethread_value
# define glthread_tls_set(KEY, POINTER) \
((KEY)->singlethread_value = (POINTER), 0)
# define glthread_tls_key_destroy(KEY) \
0
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
/* Macros with built-in error handling. */
/* ------------------------- gl_tls_key_t datatype ------------------------- */
#define gl_tls_key_init(NAME, DESTRUCTOR) \
do \
{ \
if (glthread_tls_key_init (&NAME, DESTRUCTOR)) \
abort (); \
} \
while (0)
#define gl_tls_set(NAME, POINTER) \
do \
{ \
if (glthread_tls_set (&NAME, POINTER)) \
abort (); \
} \
while (0)
#define gl_tls_key_destroy(NAME) \
do \
{ \
if (glthread_tls_key_destroy (&NAME)) \
abort (); \
} \
while (0)
/* ========================================================================= */
#endif /* _TLS_H */

View File

@@ -1,100 +0,0 @@
/* Yielding the processor to other threads and processes.
Copyright (C) 2005-2024 Free Software Foundation, Inc.
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 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 Lesser General Public License for more details.
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/>. */
/* This file contains a primitive for yielding the processor to other threads.
extern void gl_thread_yield (void);
*/
#ifndef _GLTHREAD_YIELD_H
#define _GLTHREAD_YIELD_H
#include <errno.h>
/* ========================================================================= */
#if USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS
/* Use the ISO C threads library. */
# include <threads.h>
# ifdef __cplusplus
extern "C" {
# endif
# define gl_thread_yield() \
thrd_yield ()
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if USE_POSIX_THREADS
/* Use the POSIX threads library. */
# include <sched.h>
# ifdef __cplusplus
extern "C" {
# endif
# define gl_thread_yield() \
sched_yield ()
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if USE_WINDOWS_THREADS
# define WIN32_LEAN_AND_MEAN /* avoid including junk */
# include <windows.h>
# ifdef __cplusplus
extern "C" {
# endif
# define gl_thread_yield() \
Sleep (0)
# ifdef __cplusplus
}
# endif
#endif
/* ========================================================================= */
#if !(USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS)
/* Provide dummy implementation if threads are not supported. */
# define gl_thread_yield() 0
#endif
/* ========================================================================= */
#endif /* _GLTHREAD_YIELD_H */