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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
/* $OpenBSD: atomic.h,v 1.15 2017/07/04 09:00:12 mpi Exp $ */
/*
* Copyright (c) 2007 Artur Grabowski <art@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _MACHINE_ATOMIC_H_
#define _MACHINE_ATOMIC_H_
static inline unsigned int
_atomic_cas_uint(volatile unsigned int *p, unsigned int e, unsigned int n)
{
__asm volatile("cas [%2], %3, %0"
: "+r" (n), "=m" (*p)
: "r" (p), "r" (e), "m" (*p));
return (n);
}
#define atomic_cas_uint(_p, _e, _n) _atomic_cas_uint((_p), (_e), (_n))
static inline unsigned long
_atomic_cas_ulong(volatile unsigned long *p, unsigned long e, unsigned long n)
{
__asm volatile("casx [%2], %3, %0"
: "+r" (n), "=m" (*p)
: "r" (p), "r" (e), "m" (*p));
return (n);
}
#define atomic_cas_ulong(_p, _e, _n) _atomic_cas_ulong((_p), (_e), (_n))
static inline void *
_atomic_cas_ptr(volatile void *p, void *e, void *n)
{
__asm volatile("casx [%2], %3, %0"
: "+r" (n), "=m" (*(volatile unsigned long *)p)
: "r" (p), "r" (e), "m" (*(volatile unsigned long *)p));
return (n);
}
#define atomic_cas_ptr(_p, _e, _n) _atomic_cas_ptr((_p), (_e), (_n))
#define _def_atomic_swap(_f, _t, _c) \
static inline _t \
_f(volatile _t *p, _t v) \
{ \
_t e; \
_t r; \
\
r = (_t)*p; \
do { \
e = r; \
r = _c(p, e, v); \
} while (r != e); \
\
return (r); \
}
_def_atomic_swap(_atomic_swap_uint, unsigned int, atomic_cas_uint)
_def_atomic_swap(_atomic_swap_ulong, unsigned long, atomic_cas_ulong)
#undef _def_atomic_swap
static inline void *
_atomic_swap_ptr(volatile void *p, void *v)
{
void *e, *r;
r = *(void **)p;
do {
e = r;
r = atomic_cas_ptr(p, e, v);
} while (r != e);
return (r);
}
#define atomic_swap_uint(_p, _v) _atomic_swap_uint(_p, _v)
#define atomic_swap_ulong(_p, _v) _atomic_swap_ulong(_p, _v)
#define atomic_swap_ptr(_p, _v) _atomic_swap_ptr(_p, _v)
#define _def_atomic_op_nv(_f, _t, _c, _op) \
static inline _t \
_f(volatile _t *p, _t v) \
{ \
_t e, r, f; \
\
r = *p; \
do { \
e = r; \
f = e _op v; \
r = _c(p, e, f); \
} while (r != e); \
\
return (f); \
}
_def_atomic_op_nv(_atomic_add_int_nv, unsigned int, atomic_cas_uint, +)
_def_atomic_op_nv(_atomic_add_long_nv, unsigned long, atomic_cas_ulong, +)
_def_atomic_op_nv(_atomic_sub_int_nv, unsigned int, atomic_cas_uint, -)
_def_atomic_op_nv(_atomic_sub_long_nv, unsigned long, atomic_cas_ulong, -)
#undef _def_atomic_op_nv
#define atomic_add_int_nv(_p, _v) _atomic_add_int_nv(_p, _v)
#define atomic_add_long_nv(_p, _v) _atomic_add_long_nv(_p, _v)
#define atomic_sub_int_nv(_p, _v) _atomic_sub_int_nv(_p, _v)
#define atomic_sub_long_nv(_p, _v) _atomic_sub_long_nv(_p, _v)
#define __membar(_m) __asm volatile("membar " _m ::: "memory")
#define membar_enter() __membar("#StoreLoad|#StoreStore")
#define membar_exit() __membar("#LoadStore|#StoreStore")
#define membar_producer() __membar("#StoreStore")
#define membar_consumer() __membar("#LoadLoad")
#define membar_sync() __membar("#Sync")
#if defined(_KERNEL)
static __inline void
atomic_setbits_int(volatile unsigned int *uip, unsigned int v)
{
unsigned int e, r;
r = *uip;
do {
e = r;
r = atomic_cas_uint(uip, e, e | v);
} while (r != e);
}
static __inline void
atomic_clearbits_int(volatile unsigned int *uip, unsigned int v)
{
unsigned int e, r;
r = *uip;
do {
e = r;
r = atomic_cas_uint(uip, e, e & ~v);
} while (r != e);
}
#endif /* defined(_KERNEL) */
#endif /* _MACHINE_ATOMIC_H_ */
|