GraphChi 0.1

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros
atomic.hpp
1 #ifndef ATOMIC_HPP
2 #define ATOMIC_HPP
3
4 // Note, stolen from GraphLab.
5
6 namespace graphchi {
13 template<typename T>
14 class atomic{
15 public:
16 volatile T value;
17 atomic(const T& value = 0) : value(value) { }
18 T inc() { return __sync_add_and_fetch(&value, 1); }
19 T dec() { return __sync_sub_and_fetch(&value, 1); }
20 T inc(T val) { return __sync_add_and_fetch(&value, val); }
21 T dec(T val) { return __sync_sub_and_fetch(&value, val); }
22 };
23
24
34 template<typename T>
35 bool atomic_compare_and_swap(T& a, const T &oldval, const T &newval) {
36 return __sync_bool_compare_and_swap(&a, oldval, newval);
37 };
38
39
40 template <>
41 inline bool atomic_compare_and_swap(double& a, const double &oldval, const double &newval) {
42 return __sync_bool_compare_and_swap(reinterpret_cast<uint64_t*>(&a),
43 *reinterpret_cast<const uint64_t*>(&oldval),
44 *reinterpret_cast<const uint64_t*>(&newval));
45 };
46
47 template <>
48 inline bool atomic_compare_and_swap(float& a, const float &oldval, const float &newval) {
49 return __sync_bool_compare_and_swap(reinterpret_cast<uint32_t*>(&a),
50 *reinterpret_cast<const uint32_t*>(&oldval),
51 *reinterpret_cast<const uint32_t*>(&newval));
52 };
53
54 template<typename T>
55 void atomic_exchange(T& a, T& b) {
56 b =__sync_lock_test_and_set(&a, b);
57 };
58
59 }
60 #endif