Panda3D
atomicAdjustGccImpl.h
1 // Filename: atomicAdjustGccImpl.h
2 // Created by: rdb (04Jul14)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef ATOMICADJUSTGCCIMPL_H
16 #define ATOMICADJUSTGCCIMPL_H
17 
18 #include "dtoolbase.h"
19 #include "selectThreadImpl.h"
20 
21 #if (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))) || (defined(__clang__) && (__clang_major__ >= 3))
22 
23 ////////////////////////////////////////////////////////////////////
24 // Class : AtomicAdjustGccImpl
25 // Description : Uses GCC built-ins to implement atomic adjustments.
26 ////////////////////////////////////////////////////////////////////
27 class EXPCL_DTOOL AtomicAdjustGccImpl {
28 public:
29 #if __GCC_ATOMIC_LONG_LOCK_FREE > __GCC_ATOMIC_INT_LOCK_FREE
30  // If the long can be more lock-free than int, use it instead.
31  typedef __attribute__ ((aligned (__SIZEOF_LONG__))) long Integer;
32 #else
33  typedef __attribute__ ((aligned (__SIZEOF_INT__))) int Integer;
34 #endif
35  typedef void *UnalignedPointer;
36  typedef __attribute__ ((aligned (__SIZEOF_POINTER__))) UnalignedPointer Pointer;
37 
38  INLINE static void inc(TVOLATILE Integer &var);
39  INLINE static bool dec(TVOLATILE Integer &var);
40  INLINE static void add(TVOLATILE Integer &var, Integer delta);
41  INLINE static Integer set(TVOLATILE Integer &var, Integer new_value);
42  INLINE static Integer get(const TVOLATILE Integer &var);
43 
44  INLINE static Pointer set_ptr(TVOLATILE Pointer &var, Pointer new_value);
45  INLINE static Pointer get_ptr(const TVOLATILE Pointer &var);
46 
47  INLINE static Integer compare_and_exchange(TVOLATILE Integer &mem,
48  Integer old_value,
49  Integer new_value);
50 
51  INLINE static Pointer compare_and_exchange_ptr(TVOLATILE Pointer &mem,
52  Pointer old_value,
53  Pointer new_value);
54 };
55 
56 #include "atomicAdjustGccImpl.I"
57 
58 #endif // HAVE_POSIX_THREADS
59 
60 #endif