forked from anqin/trident
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomic.h
More file actions
103 lines (93 loc) · 2.43 KB
/
atomic.h
File metadata and controls
103 lines (93 loc) · 2.43 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) 2014 The Trident Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
//
#ifndef _TRIDENT_ATOMIC_H_
#define _TRIDENT_ATOMIC_H_
#if !defined(__i386__) && !defined(__x86_64__)
#error "Arch not supprot!"
#endif
#include <stdint.h>
namespace trident {
template <typename T>
inline void atomic_inc(volatile T* n)
{
asm volatile ("lock; incl %0;":"+m"(*n)::"cc");
}
template <typename T>
inline void atomic_dec(volatile T* n)
{
asm volatile ("lock; decl %0;":"+m"(*n)::"cc");
}
template <typename T>
inline T atomic_add_ret_old(volatile T* n, T v)
{
asm volatile ("lock; xaddl %1, %0;":"+m"(*n),"+r"(v)::"cc");
return v;
}
template <typename T>
inline T atomic_inc_ret_old(volatile T* n)
{
T r = 1;
asm volatile ("lock; xaddl %1, %0;":"+m"(*n), "+r"(r)::"cc");
return r;
}
template <typename T>
inline T atomic_dec_ret_old(volatile T* n)
{
T r = (T)-1;
asm volatile ("lock; xaddl %1, %0;":"+m"(*n), "+r"(r)::"cc");
return r;
}
template <typename T>
inline T atomic_add_ret_old64(volatile T* n, T v)
{
asm volatile ("lock; xaddq %1, %0;":"+m"(*n),"+r"(v)::"cc");
return v;
}
template <typename T>
inline T atomic_inc_ret_old64(volatile T* n)
{
T r = 1;
asm volatile ("lock; xaddq %1, %0;":"+m"(*n), "+r"(r)::"cc");
return r;
}
template <typename T>
inline T atomic_dec_ret_old64(volatile T* n)
{
T r = (T)-1;
asm volatile ("lock; xaddq %1, %0;":"+m"(*n), "+r"(r)::"cc");
return r;
}
template <typename T>
inline void atomic_add(volatile T* n, T v)
{
asm volatile ("lock; addl %1, %0;":"+m"(*n):"r"(v):"cc");
}
template <typename T>
inline void atomic_sub(volatile T* n, T v)
{
asm volatile ("lock; subl %1, %0;":"+m"(*n):"r"(v):"cc");
}
template <typename T, typename C, typename D>
inline T atomic_cmpxchg(volatile T* n, C cmp, D dest)
{
asm volatile ("lock; cmpxchgl %1, %0":"+m"(*n), "+r"(dest), "+a"(cmp)::"cc");
return cmp;
}
// return old value
template <typename T>
inline T atomic_swap(volatile T* lockword, T value)
{
asm volatile ("lock; xchg %0, %1;" : "+r"(value), "+m"(*lockword));
return value;
}
template <typename T, typename E, typename C>
inline T atomic_comp_swap(volatile T* lockword, E exchange, C comperand)
{
return atomic_cmpxchg(lockword, comperand, exchange);
}
} // namespace trident
#endif // _TRIDENT_ATOMIC_H_
/* vim: set ts=4 sw=4 sts=4 tw=100 */