summaryrefslogtreecommitdiff
path: root/lib/thread-optim.h
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff.email>2022-01-08 11:51:07 +0100
committerJörg Frings-Fürst <debian@jff.email>2022-01-08 11:51:07 +0100
commitbe8efac78d067c138ad8dda03df4336e73f94887 (patch)
tree5f5254a628ba0ef72065b93d949d1c985742ea8e /lib/thread-optim.h
parent7b65dbd4ebade81d504cfe5e681292a58ad1fdf0 (diff)
New upstream version 1.0upstream/1.0
Diffstat (limited to 'lib/thread-optim.h')
-rw-r--r--lib/thread-optim.h60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/thread-optim.h b/lib/thread-optim.h
new file mode 100644
index 0000000..daf887f
--- /dev/null
+++ b/lib/thread-optim.h
@@ -0,0 +1,60 @@
+/* Optimization of multithreaded code.
+
+ Copyright (C) 2020-2022 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-thread
+ 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 ();
+
+ The gl_multithreaded () invocation determines whether the program currently
+ is multithreaded.
+
+ if (mt) STATEMENT executes STATEMENT in the 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). */
+
+#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 */