diff options
Diffstat (limited to 'Build/source/libs/xpdf/xpdf-src/goo/GMutex.h')
-rw-r--r-- | Build/source/libs/xpdf/xpdf-src/goo/GMutex.h | 55 |
1 files changed, 50 insertions, 5 deletions
diff --git a/Build/source/libs/xpdf/xpdf-src/goo/GMutex.h b/Build/source/libs/xpdf/xpdf-src/goo/GMutex.h index 5d5621af2ae..972e4da7f97 100644 --- a/Build/source/libs/xpdf/xpdf-src/goo/GMutex.h +++ b/Build/source/libs/xpdf/xpdf-src/goo/GMutex.h @@ -3,14 +3,26 @@ // GMutex.h // // Portable mutex macros. +// Portable atomic increment/decrement. // -// Copyright 2002-2003 Glyph & Cog, LLC +// Copyright 2002-2014 Glyph & Cog, LLC // //======================================================================== #ifndef GMUTEX_H #define GMUTEX_H +#ifdef _WIN32 +# include <windows.h> +# include <intrin.h> +#else +# include <pthread.h> +#endif + +//------------------------------------------------------------------------ +// GMutex +//------------------------------------------------------------------------ + // Usage: // // GMutex m; @@ -24,8 +36,6 @@ #ifdef _WIN32 -#include <windows.h> - typedef CRITICAL_SECTION GMutex; #define gInitMutex(m) InitializeCriticalSection(m) @@ -35,8 +45,6 @@ typedef CRITICAL_SECTION GMutex; #else // assume pthreads -#include <pthread.h> - typedef pthread_mutex_t GMutex; #define gInitMutex(m) pthread_mutex_init(m, NULL) @@ -46,4 +54,41 @@ typedef pthread_mutex_t GMutex; #endif +//------------------------------------------------------------------------ +// atomic increment/decrement +//------------------------------------------------------------------------ + +// NB: this must be "long" to work on Windows +typedef long GAtomicCounter; + +// Increment *counter by one and return the final value (after the +// increment). +static inline GAtomicCounter gAtomicIncrement(GAtomicCounter *counter) { + GAtomicCounter newVal; + +#if defined(_WIN32) + newVal = _InterlockedIncrement(counter); +#elif defined(__GNUC__) // this also works for LLVM/clang + newVal = __sync_add_and_fetch(counter, 1); +#else +# error "gAtomicIncrement is not defined for this compiler/platform" +#endif + return newVal; +} + +// Decrement *counter by one and return the final value (after the +// decrement). +static inline GAtomicCounter gAtomicDecrement(GAtomicCounter *counter) { + GAtomicCounter newVal; + +#if defined(_WIN32) + newVal = _InterlockedDecrement(counter); +#elif defined(__GNUC__) // this also works for LLVM/clang + newVal = __sync_sub_and_fetch(counter, 1); +#else +# error "gAtomicDecrement is not defined for this compiler/platform" #endif + return newVal; +} + +#endif // GMUTEX_H |