From 7c090d00e550d23862fbc967efb400a9bbba6597 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Wed, 20 Jul 2011 18:26:32 +0100 Subject: [PATCH] Implement _dbus_atomic_get directly, rather than via inc + dec The Windows implementation is untested, but does at least (cross-)compile, and matches what GLib does. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=38005 --- dbus/dbus-sysdeps-unix.c | 22 ++++++++++++++++++++++ dbus/dbus-sysdeps-win.c | 15 +++++++++++++++ dbus/dbus-sysdeps.c | 24 ------------------------ 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index e1e1728..f2235fe 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -2367,6 +2367,28 @@ _dbus_atomic_dec (DBusAtomic *atomic) #endif } +/** + * Atomically get the value of an integer. It may change at any time + * thereafter, so this is mostly only useful for assertions. + * + * @param atomic pointer to the integer to get + * @returns the value at this moment + */ +dbus_int32_t +_dbus_atomic_get (DBusAtomic *atomic) +{ +#if DBUS_USE_SYNC + return __sync_add_and_fetch(&atomic->value, 0); +#else + dbus_int32_t res; + + _DBUS_LOCK (atomic); + res = atomic->value; + _DBUS_UNLOCK (atomic); + return res; +#endif +} + #ifdef DBUS_BUILD_TESTS /** Gets our GID * @returns process GID diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index f9afada..a8c60bd 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -3082,6 +3082,21 @@ _dbus_atomic_dec (DBusAtomic *atomic) } /** + * Atomically get the value of an integer. It may change at any time + * thereafter, so this is mostly only useful for assertions. + * + * @param atomic pointer to the integer to get + * @returns the value at this moment + */ +dbus_int32_t +_dbus_atomic_get (DBusAtomic *atomic) +{ + /* this is what GLib does, hopefully it's right... */ + MemoryBarrier (); + return atomic->value; +} + +/** * Called when the bus daemon is signaled to reload its configuration; any * caches should be nuked. Of course any caches that need explicit reload * are probably broken, but c'est la vie. diff --git a/dbus/dbus-sysdeps.c b/dbus/dbus-sysdeps.c index 18f69dc..bab516d 100644 --- a/dbus/dbus-sysdeps.c +++ b/dbus/dbus-sysdeps.c @@ -1067,30 +1067,6 @@ _dbus_strerror_from_errno (void) return _dbus_strerror (errno); } -/** - * Atomically get the value of an integer. It may change at any time - * thereafter, so this is mostly only useful for assertions. - * - * This function temporarily increases the atomic integer, so only - * use it in contexts where that would be OK (such as refcounts). - * - * @param atomic pointer to the integer to increment - * @returns the value at this moment - */ -dbus_int32_t -_dbus_atomic_get (DBusAtomic *atomic) -{ - dbus_int32_t old_value; - - /* On Windows we use InterlockedIncrement and InterlockedDecrement, - * and there is no InterlockedGet, so we have to change the value. - * Increasing it is less likely to have bad side-effects (for instance, - * it's OK for refcounts). */ - old_value = _dbus_atomic_inc (atomic); - _dbus_atomic_dec (atomic); - return old_value; -} - /** @} end of sysdeps */ /* tests in dbus-sysdeps-util.c */ -- 1.7.5.4