-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathatomics.h
86 lines (77 loc) · 1.94 KB
/
atomics.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef ATOMICS
#define ATOMICS
#if __x86_64__ || _WIN64
#define _64BIT
#define _ptr long long
#else
#define _32BIT
#define _ptr int
#endif
inline bool compare_and_set(const void* const pos, const void* const oldval, const void* const newval) __attribute__((always_inline));
#ifdef _64BIT
inline bool compare_and_set(const void* const pos, const void* const oldval, const void* const newval) {
return __sync_bool_compare_and_swap((long long*)pos,oldval,newval);
}
#else
inline bool compare_and_set(const void* pos, const void* const oldval, const void* const newval) {
return __sync_bool_compare_and_swap((int*)pos,oldval,newval);
}
#endif
class atomic_int{
public:
int a;
atomic_int():a(0){ }
atomic_int(const int param):a(param){}
operator int()const {return a;}
int& operator++(){
__sync_fetch_and_add(&a,1);
return a;
}
int& operator--(){
__sync_fetch_and_sub(&a,1);
return a;
}
int operator++(int){
return __sync_fetch_and_add(&a,1);
}
int operator--(int){
return __sync_fetch_and_sub(&a,1);
}
int& operator=(int integer){
a = integer;
return a;
}
};
template<typename T>
class markable_ptr{
public:
T* ptr;
markable_ptr():ptr(0){};
markable_ptr(const markable_ptr<T>& origin):ptr(origin.ptr){ };
markable_ptr(T* const _p):ptr(_p){ };
T& operator*() const {return *get_pointer();}
T* operator->() const {return get_pointer();}
bool operator==(const T* const rhs)const{
return ptr == rhs.ptr;
}
bool operator!=(const T* const rhs)const{
return ptr != rhs;
}
markable_ptr& operator=(T* const _p){
ptr = _p;
return *this;
}
bool attempt_mark(){return compare_and_set(&ptr,ptr,(T*)(((_ptr)ptr)|1));}
T* get_pointer() const {return (T*)((_ptr)ptr&(~1));}
T* get() const { return ptr;}
bool is_marked() const {return (_ptr)ptr&1;}
markable_ptr<T>& operator=(const markable_ptr<T>& origin){
ptr = origin.ptr;
return *this;
}
markable_ptr<T>& operator=(const T* origin){
ptr = origin;
return *this;
}
};
#endif