1
0
mirror of https://https.git.savannah.gnu.org/git/gnulib.git synced 2026-04-28 06:33:36 +00:00
Files
gnulib/lib/thread-optim.h
Paul Eggert 87fb310b69 doc: be more like POSIX in threading terms
In documentation and comments, be more like POSIX in terminology
involving multithreading.  Explain the distinction between
multithreaded process vs multithreaded program.  Change “program”
to “process” when the latter wording is more accurate or informative.
Simplify the wording for the constraints on processes that use
unlocked I/O.  Change “multithread-safe” to “thread-safe”.
Change “thread-safety” to “thread safety”.
However, do not change “multithreaded” to “multi-threaded” even
though there are some uses of both spellinga, as there are a whole
bunch of uses of “multithreaded”, also in identifier names;
perhaps Gnulib should even standardize on “multithreaded”
(not “multi-threaded”), contra POSIX.
2026-04-11 13:21:19 -07:00

66 lines
2.3 KiB
C

/* Optimization of multithreaded code.
Copyright (C) 2020-2026 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>, 2020. */
#ifndef _THREAD_OPTIM_H
#define _THREAD_OPTIM_H
/* This file defines a way to optimize multithreaded code for the
single-threaded case, based on the variable '__libc_single_threaded',
defined in glibc >= 2.32. */
/* Typical use: In a block or function, use
bool mt = gl_multithreaded ();
...
if (mt)
if (pthread_mutex_lock (&lock)) abort ();
...
if (mt)
if (pthread_mutex_unlock (&lock)) abort ();
gl_multithreaded () returns true if the process might be multithreaded,
and false if the process is definitely single-threaded.
if (mt) STATEMENT executes STATEMENT in the possibly-multithreaded case,
and skips it in the single-threaded case.
The code between the gl_multithreaded () invocation and any use of the
variable 'mt' must not create threads or invoke functions that may
indirectly create threads (e.g. 'dlopen' may, indirectly through C++
initializers of global variables in the shared library being opened,
create threads).
The lock here is meant to synchronize threads in the same process. The
same optimization cannot be applied to locks that synchronize different
processes (e.g. through shared memory mappings). */
/* This file uses HAVE_SYS_SINGLE_THREADED_H. */
#if !_GL_CONFIG_H_INCLUDED
#error "Please include config.h first."
#endif
#if HAVE_SYS_SINGLE_THREADED_H /* glibc >= 2.32 */
# include <sys/single_threaded.h>
# define gl_multithreaded() (!__libc_single_threaded)
#else
# define gl_multithreaded() 1
#endif
#endif /* _THREAD_OPTIM_H */