edit
[c11concurrency-benchmarks.git] / silo / masstree / circular_int.hh
1 /* Masstree
2  * Eddie Kohler, Yandong Mao, Robert Morris
3  * Copyright (c) 2012-2014 President and Fellows of Harvard College
4  * Copyright (c) 2012-2014 Massachusetts Institute of Technology
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, subject to the conditions
9  * listed in the Masstree LICENSE file. These conditions include: you must
10  * preserve this copyright notice, and you cannot mention the copyright
11  * holders in advertising related to the Software without their permission.
12  * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
13  * notice is a summary of the Masstree LICENSE file; the license in that file
14  * is legally binding.
15  */
16 #ifndef KVDB_CIRCULAR_INT_HH
17 #define KVDB_CIRCULAR_INT_HH 1
18 #include "compiler.hh"
19
20 template <typename T>
21 class circular_int {
22   public:
23     typedef typename mass::make_unsigned<T>::type value_type;
24     typedef typename mass::make_signed<T>::type difference_type;
25
26     circular_int()
27         : v_() {
28     }
29     circular_int(T x)
30         : v_(x) {
31     }
32
33     value_type value() const {
34         return v_;
35     }
36
37     circular_int<T> &operator++() {
38         ++v_;
39         return *this;
40     }
41     circular_int<T> operator++(int) {
42         ++v_;
43         return circular_int<T>(v_ - 1);
44     }
45     circular_int<T> &operator--() {
46         --v_;
47         return *this;
48     }
49     circular_int<T> operator--(int) {
50         --v_;
51         return circular_int<T>(v_ + 1);
52     }
53     circular_int<T> &operator+=(unsigned x) {
54         v_ += x;
55         return *this;
56     }
57     circular_int<T> &operator+=(int x) {
58         v_ += x;
59         return *this;
60     }
61     circular_int<T> &operator-=(unsigned x) {
62         v_ -= x;
63         return *this;
64     }
65     circular_int<T> &operator-=(int x) {
66         v_ -= x;
67         return *this;
68     }
69
70     circular_int<T> cmpxchg(circular_int<T> expected, circular_int<T> desired) {
71         return ::cmpxchg(&v_, expected.v_, desired.v_);
72     }
73     circular_int<T> cmpxchg(T expected, T desired) {
74         return ::cmpxchg(&v_, expected, desired);
75     }
76
77     typedef value_type (circular_int<T>::*unspecified_bool_type)() const;
78     operator unspecified_bool_type() const {
79         return v_ != 0 ? &circular_int<T>::value : 0;
80     }
81     bool operator!() const {
82         return v_ == 0;
83     }
84
85     circular_int<T> operator+(unsigned x) const {
86         return circular_int<T>(v_ + x);
87     }
88     circular_int<T> operator+(int x) const {
89         return circular_int<T>(v_ + x);
90     }
91     circular_int<T> next_nonzero() const {
92         value_type v = v_ + 1;
93         return circular_int<T>(v + !v);
94     }
95     static value_type next_nonzero(value_type x) {
96         ++x;
97         return x + !x;
98     }
99     circular_int<T> operator-(unsigned x) const {
100         return circular_int<T>(v_ - x);
101     }
102     circular_int<T> operator-(int x) const {
103         return circular_int<T>(v_ - x);
104     }
105     difference_type operator-(circular_int<T> x) const {
106         return v_ - x.v_;
107     }
108
109     bool operator==(circular_int<T> x) const {
110         return v_ == x.v_;
111     }
112     bool operator!=(circular_int<T> x) const {
113         return !(*this == x);
114     }
115     static bool less(value_type a, value_type b) {
116         return difference_type(a - b) < 0;
117     }
118     static bool less_equal(value_type a, value_type b) {
119         return difference_type(a - b) <= 0;
120     }
121     bool operator<(circular_int<T> x) const {
122         return less(v_, x.v_);
123     }
124     bool operator<=(circular_int<T> x) const {
125         return !less(x.v_, v_);
126     }
127     bool operator>=(circular_int<T> x) const {
128         return !less(v_, x.v_);
129     }
130     bool operator>(circular_int<T> x) const {
131         return less(x.v_, v_);
132     }
133
134   private:
135     value_type v_;
136 };
137
138 typedef circular_int<uint64_t> kvepoch_t;
139
140 template <typename T>
141 inline circular_int<T> cmpxchg(circular_int<T> *object, circular_int<T> expected,
142                                circular_int<T> desired) {
143     return object->cmpxchg(expected, desired);
144 }
145
146 #endif