Panda3D
atomicAdjustGccImpl.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file atomicAdjustGccImpl.I
10  * @author rdb
11  * @date 2014-07-04
12  */
13 
14 /**
15  * Atomically increments the indicated variable.
16  */
17 INLINE void AtomicAdjustGccImpl::
18 inc(TVOLATILE AtomicAdjustGccImpl::Integer &var) {
19  __atomic_fetch_add(&var, 1, __ATOMIC_SEQ_CST);
20 }
21 
22 /**
23  * Atomically decrements the indicated variable and returns true if the new
24  * value is nonzero, false if it is zero.
25  */
26 INLINE bool AtomicAdjustGccImpl::
27 dec(TVOLATILE AtomicAdjustGccImpl::Integer &var) {
28  return (__atomic_sub_fetch(&var, 1, __ATOMIC_SEQ_CST) != 0);
29 }
30 
31 /**
32  * Atomically computes var += delta. It is legal for delta to be negative.
33  * Returns the result of the addition.
34  */
35 INLINE AtomicAdjustGccImpl::Integer AtomicAdjustGccImpl::
36 add(TVOLATILE AtomicAdjustGccImpl::Integer &var,
37  AtomicAdjustGccImpl::Integer delta) {
38  return __atomic_add_fetch(&var, delta, __ATOMIC_SEQ_CST);
39 }
40 
41 /**
42  * Atomically changes the indicated variable and returns the original value.
43  */
44 INLINE AtomicAdjustGccImpl::Integer AtomicAdjustGccImpl::
45 set(TVOLATILE AtomicAdjustGccImpl::Integer &var,
46  AtomicAdjustGccImpl::Integer new_value) {
47 
48  return __atomic_exchange_n(&var, new_value, __ATOMIC_SEQ_CST);
49 }
50 
51 /**
52  * Atomically retrieves the snapshot value of the indicated variable. This is
53  * the only guaranteed safe way to retrieve the value that other threads might
54  * be asynchronously setting, incrementing, or decrementing (via other
55  * AtomicAjust methods).
56  */
57 INLINE AtomicAdjustGccImpl::Integer AtomicAdjustGccImpl::
58 get(const TVOLATILE AtomicAdjustGccImpl::Integer &var) {
59  return __atomic_load_n(&var, __ATOMIC_SEQ_CST);
60 }
61 
62 /**
63  * Atomically changes the indicated variable and returns the original value.
64  */
65 INLINE AtomicAdjustGccImpl::Pointer AtomicAdjustGccImpl::
66 set_ptr(TVOLATILE AtomicAdjustGccImpl::Pointer &var,
67  AtomicAdjustGccImpl::Pointer new_value) {
68 
69  return __atomic_exchange_n(&var, new_value, __ATOMIC_SEQ_CST);
70 }
71 
72 /**
73  * Atomically retrieves the snapshot value of the indicated variable. This is
74  * the only guaranteed safe way to retrieve the value that other threads might
75  * be asynchronously setting, incrementing, or decrementing (via other
76  * AtomicAjust methods).
77  */
78 INLINE AtomicAdjustGccImpl::Pointer AtomicAdjustGccImpl::
79 get_ptr(const TVOLATILE AtomicAdjustGccImpl::Pointer &var) {
80  return __atomic_load_n(&var, __ATOMIC_SEQ_CST);
81 }
82 
83 /**
84  * Atomic compare and exchange.
85  *
86  * If mem is equal to old_value, store new_value in mem. In either case,
87  * return the original value of mem. The caller can test for success by
88  * comparing return_value == old_value.
89  *
90  * The atomic function expressed in pseudo-code:
91  *
92  * orig_value = mem; if (mem == old_value) { mem = new_value; } return
93  * orig_value;
94  *
95  */
96 INLINE AtomicAdjustGccImpl::Integer AtomicAdjustGccImpl::
97 compare_and_exchange(TVOLATILE AtomicAdjustGccImpl::Integer &mem,
98  AtomicAdjustGccImpl::Integer old_value,
99  AtomicAdjustGccImpl::Integer new_value) {
100 
101  __atomic_compare_exchange_n(&mem, &old_value, new_value, false,
102  __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
103  return old_value;
104 }
105 
106 /**
107  * Atomic compare and exchange.
108  *
109  * As above, but works on pointers instead of integers.
110  */
111 INLINE AtomicAdjustGccImpl::Pointer AtomicAdjustGccImpl::
112 compare_and_exchange_ptr(TVOLATILE AtomicAdjustGccImpl::Pointer &mem,
113  AtomicAdjustGccImpl::Pointer old_value,
114  AtomicAdjustGccImpl::Pointer new_value) {
115 
116  __atomic_compare_exchange_n(&mem, &old_value, new_value, false,
117  __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
118  return old_value;
119 }