From 26f35893aedd9c3354ca3e35b47a518e8c5ee027 Mon Sep 17 00:00:00 2001 From: Twan van Laarhoven Date: Thu, 7 May 2020 02:17:14 +0200 Subject: [PATCH] Remove our own atomic type in favor of std::atomic --- src/util/age.cpp | 4 +- src/util/age.hpp | 19 ++++---- src/util/atomic.hpp | 114 -------------------------------------------- 3 files changed, 13 insertions(+), 124 deletions(-) delete mode 100644 src/util/atomic.hpp diff --git a/src/util/age.cpp b/src/util/age.cpp index 256ca309..b720be6d 100644 --- a/src/util/age.cpp +++ b/src/util/age.cpp @@ -13,6 +13,6 @@ // what a waste of a source file... -AtomicInt Age::new_age(2); +atomic Age::new_age(2); -IMPLEMENT_DYNAMIC_ARG(AtomicIntEquiv, last_update_age, 0); +IMPLEMENT_DYNAMIC_ARG(Age::age_t, last_update_age, 0); diff --git a/src/util/age.hpp b/src/util/age.hpp index 092666d2..b3a6c24b 100644 --- a/src/util/age.hpp +++ b/src/util/age.hpp @@ -10,14 +10,17 @@ #include #include -#include +#include +#include // ----------------------------------------------------------------------------- : Age /// Represents the age of a value, higher values are newer /** Age is counted using a global variable */ class Age { - public: +public: + typedef uint_fast64_t age_t; + /// Construct a new age value Age() { update(); @@ -27,7 +30,7 @@ class Age { * 1: before 'beginning of time', the age conceptually just before program start * 2..: normal ages */ - Age(AtomicIntEquiv age) : age(age) {} + Age(age_t age) : age(age) {} /// Update the age to become the newest one inline void update() { @@ -40,13 +43,13 @@ class Age { inline bool operator == (Age a) const { return age == a.age; } /// A number corresponding to the age - inline AtomicIntEquiv get() const { return age; } + inline age_t get() const { return age; } - private: +private: /// This age - AtomicIntEquiv age; + age_t age; /// Global age counter, value of the last age created - static AtomicInt new_age; + static atomic new_age; }; @@ -56,5 +59,5 @@ class Age { * if last_update_age > 0 they return whether the image is still up to date * if last_update_age == 0 they generate the image */ -DECLARE_DYNAMIC_ARG (AtomicIntEquiv, last_update_age); +DECLARE_DYNAMIC_ARG(Age::age_t, last_update_age); diff --git a/src/util/atomic.hpp b/src/util/atomic.hpp deleted file mode 100644 index 241c2376..00000000 --- a/src/util/atomic.hpp +++ /dev/null @@ -1,114 +0,0 @@ -//+----------------------------------------------------------------------------+ -//| Description: Magic Set Editor - Program to make Magic (tm) cards | -//| Copyright: (C) Twan van Laarhoven and the other MSE developers | -//| License: GNU General Public License 2 or later (see file COPYING) | -//+----------------------------------------------------------------------------+ - -#pragma once - -/** @file util/atomic.hpp - * - * @brief Provides the type AtomicInt, which is an integer that can be incremented and decremented atomicly - */ - -// ----------------------------------------------------------------------------- : Includes - -// ----------------------------------------------------------------------------- : AtomicInt : windows - -#if defined(__WXMSW__) - #ifdef _MSC_VER - extern "C" { - LONG __cdecl _InterlockedIncrement(LONG volatile *Addend); - LONG __cdecl _InterlockedDecrement(LONG volatile *Addend); - } - #pragma intrinsic (_InterlockedIncrement) - #define InterlockedIncrement _InterlockedIncrement - #pragma intrinsic (_InterlockedDecrement) - #define InterlockedDecrement _InterlockedDecrement - #endif - - /// An integer which is equivalent to an AtomicInt, but which doesn't support attomic operations - typedef LONG AtomicIntEquiv; - - /// An integer that can be incremented and decremented atomicly - class AtomicInt { - public: - AtomicInt(AtomicIntEquiv v) : v(v) {} - inline operator AtomicIntEquiv() const { - return v; - } - /// Attomicly increments this AtomicInt, returns the new value - inline AtomicIntEquiv operator ++ () { - return InterlockedIncrement(&v); - } - /// Attomicly decrements this AtomicInt, returns the new value - inline AtomicIntEquiv operator -- () { - return InterlockedDecrement(&v); - } - private: - AtomicIntEquiv v; ///< The value - }; - - /// We have a fast AtomicInt - #define HAVE_FAST_ATOMIC - -// ----------------------------------------------------------------------------- : AtomicInt : GCC -#elif defined(__GNUC__) - - /// An integer which is equivalent to an AtomicInt, but which doesn't support attomic operations - typedef unsigned int AtomicIntEquiv; - - /// An integer that can be incremented and decremented atomicly - class AtomicInt { - public: - AtomicInt(AtomicIntEquiv v) : v(v) {} - inline operator AtomicIntEquiv() const { - return v; - } - inline AtomicInt operator ++ () { - return __sync_add_and_fetch(&v,1); - } - inline AtomicInt operator -- () { - return __sync_sub_and_fetch(&v,1); - } - private: - AtomicIntEquiv v; - }; - - /// We have a fast AtomicInt - #define HAVE_FAST_ATOMIC - -// ----------------------------------------------------------------------------- : AtomicInt : portable -#else - - /// An integer which is equivalent to an AtomicInt, but which doesn't support attomic operations - typedef long AtomicIntEquiv; - - /// An integer that can be incremented and decremented atomicly - class AtomicInt { - public: - AtomicInt(AtomicIntEquiv v) : v(v) {} - AtomicInt(const AtomicInt& i) { - wxCriticalSectionLocker lock(i.cs); - v = i.v; - } - inline operator AtomicIntEquiv() const { - return v; - } - /// Attomicly increments this AtomicInt, returns the new value - inline AtomicIntEquiv operator ++ () { - wxCriticalSectionLocker lock(cs); - return ++v; - } - /// Attomicly decrements this AtomicInt, returns the new value - inline AtomicIntEquiv operator -- () { - wxCriticalSectionLocker lock(cs); - return --v; - } - private: - AtomicIntEquiv v; ///< The value - mutable wxCriticalSection cs; ///< Critical section protecting v - }; - -#endif -