]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/AtomicWord.h
Removed CVS $ markers
[thirdparty/squid.git] / src / ipc / AtomicWord.h
1 /*
2 */
3
4 #ifndef SQUID_IPC_ATOMIC_WORD_H
5 #define SQUID_IPC_ATOMIC_WORD_H
6
7 namespace Ipc
8 {
9
10 namespace Atomic
11 {
12
13 /// Whether atomic operations support is available
14 bool Enabled();
15
16 #if HAVE_ATOMIC_OPS
17
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>
21 class WordT
22 {
23 public:
24 typedef ValueType Value;
25
26 WordT() {} // leave value unchanged
27 WordT(Value aValue): value(aValue) {} // XXX: unsafe
28
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); }
35
36 bool swap_if(const Value comparand, const Value replacement) { return __sync_bool_compare_and_swap(&value, comparand, replacement); }
37
38 /// v1 = value; value &= v2; return v1;
39 Value fetchAndAnd(const Value v2) { return __sync_fetch_and_and(&value, v2); }
40
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); }
43
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(); }
47
48 private:
49
50 Value value;
51 };
52
53 #else
54
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>
58 class WordT
59 {
60 public:
61 typedef ValueType Value;
62
63 WordT() {} // leave value unchanged
64 WordT(Value aValue): value(aValue) {} // XXX: unsafe
65
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--; }
71
72 bool swap_if(const Value comparand, const Value replacement)
73 { assert(Enabled()); return value == comparand ? value = replacement, true : false; }
74
75 /// v1 = value; value &= v2; return v1;
76 Value fetchAndAnd(const Value v2)
77 { assert(Enabled()); const Value v1 = value; value &= v2; return v1; }
78
79 // TODO: no need for __sync_bool_compare_and_swap here?
80 bool operator ==(const Value v2) { assert(Enabled()); return value == v2; }
81
82 // TODO: no need for __sync_fetch_and_add here?
83 Value get() const { assert(Enabled()); return value; }
84 operator Value () const { return get(); }
85
86 private:
87
88 Value value;
89 };
90
91 #endif /* HAVE_ATOMIC_OPS */
92
93 typedef WordT<int> Word;
94
95 } // namespace Atomic
96
97 } // namespace Ipc
98
99 #endif // SQUID_IPC_ATOMIC_WORD_H