diff options
author | Simon McVittie <smcv@collabora.com> | 2023-08-15 13:34:10 +0000 |
---|---|---|
committer | Simon McVittie <smcv@collabora.com> | 2023-08-15 13:34:10 +0000 |
commit | 3365c95c202e3c3cfa78aa3abff34b21cdcc6e0f (patch) | |
tree | 3cebe13103c1899978c83ce3ae23287f33b8a05e | |
parent | dbe7de00ae823dc5dec3de9da0d46a599113f670 (diff) | |
parent | 88b57499846a1de4d97e8eff377132dfc1e0b7de (diff) |
Merge branch 'stdatomic' into 'master'
sysdeps: Use C11 stdatomic.h where possible
See merge request dbus/dbus!431
-rw-r--r-- | cmake/ConfigureChecks.cmake | 1 | ||||
-rw-r--r-- | cmake/config.h.cmake | 1 | ||||
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | dbus/CMakeLists.txt | 1 | ||||
-rw-r--r-- | dbus/Makefile.am | 1 | ||||
-rw-r--r-- | dbus/dbus-init-win.cpp | 2 | ||||
-rw-r--r-- | dbus/dbus-init-win.h | 17 | ||||
-rw-r--r-- | dbus/dbus-sysdeps-thread-win.c | 1 | ||||
-rw-r--r-- | dbus/dbus-sysdeps-unix.c | 31 | ||||
-rw-r--r-- | dbus/dbus-sysdeps-win.c | 31 | ||||
-rw-r--r-- | dbus/dbus-sysdeps-win.h | 3 | ||||
-rw-r--r-- | dbus/dbus-sysdeps.h | 8 | ||||
-rw-r--r-- | meson.build | 1 |
13 files changed, 83 insertions, 16 deletions
diff --git a/cmake/ConfigureChecks.cmake b/cmake/ConfigureChecks.cmake index b138b367..b7f37020 100644 --- a/cmake/ConfigureChecks.cmake +++ b/cmake/ConfigureChecks.cmake @@ -21,6 +21,7 @@ check_include_file(linux/close_range.h HAVE_LINUX_CLOSE_RANGE_H) check_include_file(linux/magic.h HAVE_LINUX_MAGIC_H) check_include_file(locale.h HAVE_LOCALE_H) check_include_file(signal.h HAVE_SIGNAL_H) +check_include_file(stdatomic.h HAVE_STDATOMIC_H) check_include_file(stdio.h HAVE_STDIO_H) # dbus-sysdeps.h check_include_files("stdint.h;sys/types.h;sys/event.h" HAVE_SYS_EVENT_H) check_include_file(sys/inotify.h HAVE_SYS_INOTIFY_H) diff --git a/cmake/config.h.cmake b/cmake/config.h.cmake index 16e737f3..77fc19c3 100644 --- a/cmake/config.h.cmake +++ b/cmake/config.h.cmake @@ -113,6 +113,7 @@ /* Define to 1 if you have stdio.h */ #cmakedefine HAVE_STDIO_H 1 +#cmakedefine HAVE_STDATOMIC_H 1 #cmakedefine HAVE_SYSLOG_H 1 #cmakedefine HAVE_SYS_EVENTS_H 1 #cmakedefine HAVE_SYS_INOTIFY_H 1 diff --git a/configure.ac b/configure.ac index 389b2796..6b717e75 100644 --- a/configure.ac +++ b/configure.ac @@ -419,6 +419,7 @@ errno.h linux/close_range.h locale.h signal.h +stdatomic.h sys/prctl.h sys/random.h sys/resource.h diff --git a/dbus/CMakeLists.txt b/dbus/CMakeLists.txt index cc192ef8..1f545ba6 100644 --- a/dbus/CMakeLists.txt +++ b/dbus/CMakeLists.txt @@ -185,6 +185,7 @@ if(WIN32) dbus-sysdeps-thread-win.c ) set(DBUS_SHARED_HEADERS ${DBUS_SHARED_HEADERS} + dbus-init-win.h dbus-sockets-win.h dbus-sysdeps-win.h ) diff --git a/dbus/Makefile.am b/dbus/Makefile.am index fa628ded..0d38aca2 100644 --- a/dbus/Makefile.am +++ b/dbus/Makefile.am @@ -77,6 +77,7 @@ DBUS_SHARED_arch_sources = \ $(wince_source) \ dbus-backtrace-win.c \ dbus-file-win.c \ + dbus-init-win.h \ dbus-pipe-win.c \ dbus-sockets-win.h \ dbus-sysdeps-win.c \ diff --git a/dbus/dbus-init-win.cpp b/dbus/dbus-init-win.cpp index 3464c88f..56311d8a 100644 --- a/dbus/dbus-init-win.cpp +++ b/dbus/dbus-init-win.cpp @@ -27,7 +27,7 @@ extern "C" { -#include "dbus-sysdeps-win.h" +#include "dbus-init-win.h" } class DBusInternalInit diff --git a/dbus/dbus-init-win.h b/dbus/dbus-init-win.h new file mode 100644 index 00000000..8a9de8f7 --- /dev/null +++ b/dbus/dbus-init-win.h @@ -0,0 +1,17 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* + * Copyright © 2013 Intel Corporation + * SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later + * + * Do not include other private headers in this one, particularly + * dbus-sysdeps.h: it gets included into C++ code which is not + * compatible with our use of <stdatomic.h>. + */ + +#ifndef DBUS_INIT_WIN_H +#define DBUS_INIT_WIN_H + +void _dbus_threads_windows_init_global (void); +void _dbus_threads_windows_ensure_ctor_linked (void); + +#endif diff --git a/dbus/dbus-sysdeps-thread-win.c b/dbus/dbus-sysdeps-thread-win.c index 48262306..9f1818f9 100644 --- a/dbus/dbus-sysdeps-thread-win.c +++ b/dbus/dbus-sysdeps-thread-win.c @@ -24,6 +24,7 @@ */ #include <config.h> +#include "dbus-init-win.h" #include "dbus-internals.h" #include "dbus-sysdeps.h" #include "dbus-sysdeps-win.h" diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index c66096e0..60bdc6f7 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -98,7 +98,7 @@ #include <systemd/sd-daemon.h> #endif -#if !DBUS_USE_SYNC +#if !defined(HAVE_STDATOMIC_H) && !DBUS_USE_SYNC #include <pthread.h> #endif @@ -3141,7 +3141,7 @@ _dbus_pid_for_log (void) return getpid (); } -#if !DBUS_USE_SYNC +#if !defined(HAVE_STDATOMIC_H) && !DBUS_USE_SYNC /* To be thread-safe by default on platforms that don't necessarily have * atomic operations (notably Debian armel, which is armv4t), we must * use a mutex that can be initialized statically, like this. @@ -3159,7 +3159,11 @@ static pthread_mutex_t atomic_mutex = PTHREAD_MUTEX_INITIALIZER; dbus_int32_t _dbus_atomic_inc (DBusAtomic *atomic) { -#if DBUS_USE_SYNC +#ifdef HAVE_STDATOMIC_H + /* Atomic version of "old = *atomic; *atomic += 1; return old" */ + return atomic_fetch_add (&atomic->value, 1); +#elif DBUS_USE_SYNC + /* Atomic version of "*atomic += 1; return *atomic - 1" */ return __sync_add_and_fetch(&atomic->value, 1)-1; #else dbus_int32_t res; @@ -3182,7 +3186,11 @@ _dbus_atomic_inc (DBusAtomic *atomic) dbus_int32_t _dbus_atomic_dec (DBusAtomic *atomic) { -#if DBUS_USE_SYNC +#ifdef HAVE_STDATOMIC_H + /* Atomic version of "old = *atomic; *atomic -= 1; return old" */ + return atomic_fetch_sub (&atomic->value, 1); +#elif DBUS_USE_SYNC + /* Atomic version of "*atomic -= 1; return *atomic + 1" */ return __sync_sub_and_fetch(&atomic->value, 1)+1; #else dbus_int32_t res; @@ -3206,7 +3214,10 @@ _dbus_atomic_dec (DBusAtomic *atomic) dbus_int32_t _dbus_atomic_get (DBusAtomic *atomic) { -#if DBUS_USE_SYNC +#ifdef HAVE_STDATOMIC_H + /* Atomic version of "return *atomic" */ + return atomic_load (&atomic->value); +#elif DBUS_USE_SYNC __sync_synchronize (); return atomic->value; #else @@ -3228,7 +3239,10 @@ _dbus_atomic_get (DBusAtomic *atomic) void _dbus_atomic_set_zero (DBusAtomic *atomic) { -#if DBUS_USE_SYNC +#ifdef HAVE_STDATOMIC_H + /* Atomic version of "*atomic = 0" */ + atomic_store (&atomic->value, 0); +#elif DBUS_USE_SYNC /* Atomic version of "*atomic &= 0; return *atomic" */ __sync_and_and_fetch (&atomic->value, 0); #else @@ -3246,7 +3260,10 @@ _dbus_atomic_set_zero (DBusAtomic *atomic) void _dbus_atomic_set_nonzero (DBusAtomic *atomic) { -#if DBUS_USE_SYNC +#ifdef HAVE_STDATOMIC_H + /* Atomic version of "*atomic = 1" */ + atomic_store (&atomic->value, 1); +#elif DBUS_USE_SYNC /* Atomic version of "*atomic |= 1; return *atomic" */ __sync_or_and_fetch (&atomic->value, 1); #else diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index fa408328..2f927317 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -3452,9 +3452,13 @@ _dbus_make_file_world_readable(const DBusString *filename, dbus_int32_t _dbus_atomic_inc (DBusAtomic *atomic) { - // +/- 1 is needed here! - // no volatile argument with mingw +#ifdef HAVE_STDATOMIC_H + /* Atomic version of "old = *atomic; *atomic += 1; return old" */ + return atomic_fetch_add (&atomic->value, 1); +#else + /* Atomic version of "*atomic += 1; return *atomic - 1" */ return InterlockedIncrement (&atomic->value) - 1; +#endif } /** @@ -3467,9 +3471,13 @@ _dbus_atomic_inc (DBusAtomic *atomic) dbus_int32_t _dbus_atomic_dec (DBusAtomic *atomic) { - // +/- 1 is needed here! - // no volatile argument with mingw +#ifdef HAVE_STDATOMIC_H + /* Atomic version of "old = *atomic; *atomic -= 1; return old" */ + return atomic_fetch_sub (&atomic->value, 1); +#else + /* Atomic version of "*atomic -= 1; return *atomic + 1" */ return InterlockedDecrement (&atomic->value) + 1; +#endif } /** @@ -3482,6 +3490,10 @@ _dbus_atomic_dec (DBusAtomic *atomic) dbus_int32_t _dbus_atomic_get (DBusAtomic *atomic) { +#ifdef HAVE_STDATOMIC_H + /* Atomic version of "return *atomic" */ + return atomic_load (&atomic->value); +#else /* In this situation, GLib issues a MemoryBarrier() and then returns * atomic->value. However, mingw from mingw.org (not to be confused with * mingw-w64 from mingw-w64.sf.net) does not have MemoryBarrier in its @@ -3495,6 +3507,7 @@ _dbus_atomic_get (DBusAtomic *atomic) InterlockedExchange (&dummy, 1); return atomic->value; +#endif } /** @@ -3505,7 +3518,12 @@ _dbus_atomic_get (DBusAtomic *atomic) void _dbus_atomic_set_zero (DBusAtomic *atomic) { +#ifdef HAVE_STDATOMIC_H + /* Atomic version of "*atomic = 0" */ + atomic_store (&atomic->value, 0); +#else InterlockedExchange (&atomic->value, 0); +#endif } /** @@ -3516,7 +3534,12 @@ _dbus_atomic_set_zero (DBusAtomic *atomic) void _dbus_atomic_set_nonzero (DBusAtomic *atomic) { +#ifdef HAVE_STDATOMIC_H + /* Atomic version of "*atomic = 1" */ + atomic_store (&atomic->value, 1); +#else InterlockedExchange (&atomic->value, 1); +#endif } /** diff --git a/dbus/dbus-sysdeps-win.h b/dbus/dbus-sysdeps-win.h index be9cbe15..f7be2101 100644 --- a/dbus/dbus-sysdeps-win.h +++ b/dbus/dbus-sysdeps-win.h @@ -87,9 +87,6 @@ _dbus_win_sid_to_name_and_domain (dbus_uid_t uid, DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_get_install_root (DBusString *str); -void _dbus_threads_windows_init_global (void); -void _dbus_threads_windows_ensure_ctor_linked (void); - DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_getsid(char **sid, dbus_pid_t process_id); diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index e9737179..91b6016f 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -38,6 +38,10 @@ #include <inttypes.h> #endif +#ifdef HAVE_STDATOMIC_H +#include <stdatomic.h> +#endif + #include <dbus/dbus-errors.h> #include <dbus/dbus-file.h> #include <dbus/dbus-string.h> @@ -333,7 +337,9 @@ typedef struct DBusAtomic DBusAtomic; */ struct DBusAtomic { -#ifdef DBUS_WIN +#ifdef HAVE_STDATOMIC_H + atomic_int value; /**< Value of the atomic integer. */ +#elif defined(DBUS_WIN) volatile long value; /**< Value of the atomic integer. */ #else volatile dbus_int32_t value; /**< Value of the atomic integer. */ diff --git a/meson.build b/meson.build index 4b2d9b90..da3c0d20 100644 --- a/meson.build +++ b/meson.build @@ -691,6 +691,7 @@ check_headers = [ 'linux/magic.h', 'locale.h', 'signal.h', + 'stdatomic.h', 'syslog.h', 'sys/prctl.h', 'sys/random.h', |