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
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
16 #ifndef KVDB_CIRCULAR_INT_HH
17 #define KVDB_CIRCULAR_INT_HH 1
18 #include "compiler.hh"
23 typedef typename mass::make_unsigned<T>::type value_type;
24 typedef typename mass::make_signed<T>::type difference_type;
33 value_type value() const {
37 circular_int<T> &operator++() {
41 circular_int<T> operator++(int) {
43 return circular_int<T>(v_ - 1);
45 circular_int<T> &operator--() {
49 circular_int<T> operator--(int) {
51 return circular_int<T>(v_ + 1);
53 circular_int<T> &operator+=(unsigned x) {
57 circular_int<T> &operator+=(int x) {
61 circular_int<T> &operator-=(unsigned x) {
65 circular_int<T> &operator-=(int x) {
70 circular_int<T> cmpxchg(circular_int<T> expected, circular_int<T> desired) {
71 return ::cmpxchg(&v_, expected.v_, desired.v_);
73 circular_int<T> cmpxchg(T expected, T desired) {
74 return ::cmpxchg(&v_, expected, desired);
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;
81 bool operator!() const {
85 circular_int<T> operator+(unsigned x) const {
86 return circular_int<T>(v_ + x);
88 circular_int<T> operator+(int x) const {
89 return circular_int<T>(v_ + x);
91 circular_int<T> next_nonzero() const {
92 value_type v = v_ + 1;
93 return circular_int<T>(v + !v);
95 static value_type next_nonzero(value_type x) {
99 circular_int<T> operator-(unsigned x) const {
100 return circular_int<T>(v_ - x);
102 circular_int<T> operator-(int x) const {
103 return circular_int<T>(v_ - x);
105 difference_type operator-(circular_int<T> x) const {
109 bool operator==(circular_int<T> x) const {
112 bool operator!=(circular_int<T> x) const {
113 return !(*this == x);
115 static bool less(value_type a, value_type b) {
116 return difference_type(a - b) < 0;
118 static bool less_equal(value_type a, value_type b) {
119 return difference_type(a - b) <= 0;
121 bool operator<(circular_int<T> x) const {
122 return less(v_, x.v_);
124 bool operator<=(circular_int<T> x) const {
125 return !less(x.v_, v_);
127 bool operator>=(circular_int<T> x) const {
128 return !less(v_, x.v_);
130 bool operator>(circular_int<T> x) const {
131 return less(x.v_, v_);
138 typedef circular_int<uint64_t> kvepoch_t;
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);