-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcas.hpp
60 lines (43 loc) · 816 Bytes
/
cas.hpp
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
#ifndef CAS_HPP
#define CAS_HPP
#ifndef __GNUC__
#include <windows.h>
template <typename T>
T cas(T & destination, T new_value, T old_value)
{
return (T)InterlockedCompareExchangePointer((void **)&destination, (void *)new_value, (void *)old_value);
}
template <typename T>
T load_acquire(T const & v)
{
T res = v;
return res;
}
template <typename T>
void store_release(T & t, T v)
{
t = v;
}
#else
template <typename T>
T cas(T & destination, T new_value, T old_value)
{
return __sync_val_compare_and_swap(&destination, old_value, new_value);
}
template <typename T>
T load_acquire(T const & v)
{
__sync_synchronize();
T res = v;
__sync_synchronize();
return res;
}
template <typename T>
void store_release(T & t, T v)
{
__sync_synchronize();
t = v;
__sync_synchronize();
}
#endif
#endif