]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/AtomicWord.h
4 #ifndef SQUID_IPC_ATOMIC_WORD_H
5 #define SQUID_IPC_ATOMIC_WORD_H
13 /// Whether atomic operations support is available
18 /// Supplies atomic operations for an integral Value in memory shared by kids.
19 /// Used to implement non-blocking shared locks, queues, tables, and pools.
20 template <class ValueType
>
24 typedef ValueType Value
;
26 WordT() {} // leave value unchanged
27 WordT(Value aValue
): value(aValue
) {} // XXX: unsafe
29 Value
operator +=(int delta
) { return __sync_add_and_fetch(&value
, delta
); }
30 Value
operator -=(int delta
) { return __sync_sub_and_fetch(&value
, delta
); }
31 Value
operator ++() { return *this += 1; }
32 Value
operator --() { return *this -= 1; }
33 Value
operator ++(int) { return __sync_fetch_and_add(&value
, 1); }
34 Value
operator --(int) { return __sync_fetch_and_sub(&value
, 1); }
36 bool swap_if(const Value comparand
, const Value replacement
) { return __sync_bool_compare_and_swap(&value
, comparand
, replacement
); }
38 /// v1 = value; value &= v2; return v1;
39 Value
fetchAndAnd(const Value v2
) { return __sync_fetch_and_and(&value
, v2
); }
41 // TODO: no need for __sync_bool_compare_and_swap here?
42 bool operator ==(const Value v2
) { return __sync_bool_compare_and_swap(&value
, v2
, value
); }
44 // TODO: no need for __sync_fetch_and_add here?
45 Value
get() const { return __sync_fetch_and_add(const_cast<Value
*>(&value
), 0); }
46 operator Value () const { return get(); }
55 /// A wrapper to provide AtomicWordT API (and implementation asserting in SMP mode)
56 /// where we do not support atomic operations. This avoids ifdefs in core code.
57 template <class ValueType
>
61 typedef ValueType Value
;
63 WordT() {} // leave value unchanged
64 WordT(Value aValue
): value(aValue
) {} // XXX: unsafe
66 Value
operator +=(int delta
) { assert(Enabled()); return value
+= delta
; }
67 Value
operator ++() { return *this += 1; }
68 Value
operator --() { return *this += -1; }
69 Value
operator ++(int) { assert(Enabled()); return value
++; }
70 Value
operator --(int) { assert(Enabled()); return value
--; }
72 bool swap_if(const Value comparand
, const Value replacement
)
73 { assert(Enabled()); return value
== comparand
? value
= replacement
, true : false; }
75 /// v1 = value; value &= v2; return v1;
76 Value
fetchAndAnd(const Value v2
)
77 { assert(Enabled()); const Value v1
= value
; value
&= v2
; return v1
; }
79 // TODO: no need for __sync_bool_compare_and_swap here?
80 bool operator ==(const Value v2
) { assert(Enabled()); return value
== v2
; }
82 // TODO: no need for __sync_fetch_and_add here?
83 Value
get() const { assert(Enabled()); return value
; }
84 operator Value () const { return get(); }
91 #endif /* HAVE_ATOMIC_OPS */
93 typedef WordT
<int> Word
;
99 #endif // SQUID_IPC_ATOMIC_WORD_H