Ruby 2.0.0p353(2013-11-22revision43784)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

ruby_atomic.h
Go to the documentation of this file.
1 #ifndef RUBY_ATOMIC_H
2 #define RUBY_ATOMIC_H
3
4 #if 0
5 #elif defined HAVE_GCC_ATOMIC_BUILTINS
6 /* @shyouhei hack to support atomic operations in case of gcc. Gcc
7 * has its own pseudo-insns to support them. See info, or
8 * http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html */
9
10 typedef unsigned int rb_atomic_t; /* Anything OK */
11 # define ATOMIC_SET(var, val) (void)__sync_lock_test_and_set(&(var), (val))
12 # define ATOMIC_INC(var) __sync_fetch_and_add(&(var), 1)
13 # define ATOMIC_DEC(var) __sync_fetch_and_sub(&(var), 1)
14 # define ATOMIC_OR(var, val) __sync_or_and_fetch(&(var), (val))
15 # define ATOMIC_EXCHANGE(var, val) __sync_lock_test_and_set(&(var), (val))
16 # define ATOMIC_CAS(var, oldval, newval) __sync_val_compare_and_swap(&(var), (oldval), (newval))
17
18 # define ATOMIC_SIZE_ADD(var, val) __sync_fetch_and_add(&(var), (val))
19 # define ATOMIC_SIZE_SUB(var, val) __sync_fetch_and_sub(&(var), (val))
20 # define ATOMIC_SIZE_INC(var) __sync_fetch_and_add(&(var), 1)
21 # define ATOMIC_SIZE_DEC(var) __sync_fetch_and_sub(&(var), 1)
22 # define ATOMIC_SIZE_EXCHANGE(var, val) __sync_lock_test_and_set(&(var), (val))
23
24 #elif defined _WIN32
25 #if defined _MSC_VER && _MSC_VER > 1200
26 #pragma intrinsic(_InterlockedOr)
27 #endif
28 typedef LONG rb_atomic_t;
29
30 # define ATOMIC_SET(var, val) InterlockedExchange(&(var), (val))
31 # define ATOMIC_INC(var) InterlockedIncrement(&(var))
32 # define ATOMIC_DEC(var) InterlockedDecrement(&(var))
33 #if defined __GNUC__
34 # define ATOMIC_OR(var, val) __asm__("lock\n\t" "orl\t%1, %0" : "=m"(var) : "Ir"(val))
35 #elif defined _MSC_VER && _MSC_VER <= 1200
36 # define ATOMIC_OR(var, val) rb_w32_atomic_or(&(var), (val))
37 static inline void
38 rb_w32_atomic_or(volatile rb_atomic_t *var, rb_atomic_t val)
39 {
40 #ifdef _M_IX86
41 __asm mov eax, var;
42 __asm mov ecx, val;
43 __asm lock or [eax], ecx;
44 #else
45 #error unsupported architecture
46 #endif
47 }
48 #else
49 # define ATOMIC_OR(var, val) _InterlockedOr(&(var), (val))
50 #endif
51 # define ATOMIC_EXCHANGE(var, val) InterlockedExchange(&(var), (val))
52 # define ATOMIC_CAS(var, oldval, newval) InterlockedCompareExchange(&(var), (newval), (oldval))
53 # ifdef _M_AMD64
54 # define ATOMIC_SIZE_ADD(var, val) InterlockedExchangeAdd64(&(var), (val))
55 # define ATOMIC_SIZE_SUB(var, val) InterlockedExchangeAdd64(&(var), -(val))
56 # define ATOMIC_SIZE_INC(var) InterlockedIncrement64(&(var))
57 # define ATOMIC_SIZE_DEC(var) InterlockedDecrement64(&(var))
58 # define ATOMIC_SIZE_EXCHANGE(var, val) InterlockedExchange64(&(var), (val))
59 # else
60 # define ATOMIC_SIZE_ADD(var, val) InterlockedExchangeAdd((LONG *)&(var), (val))
61 # define ATOMIC_SIZE_SUB(var, val) InterlockedExchangeAdd((LONG *)&(var), -(val))
62 # define ATOMIC_SIZE_INC(var) InterlockedIncrement((LONG *)&(var))
63 # define ATOMIC_SIZE_DEC(var) InterlockedDecrement((LONG *)&(var))
64 # define ATOMIC_SIZE_EXCHANGE(var, val) InterlockedExchange((LONG *)&(var), (val))
65 # endif
66
67 #elif defined(__sun) && defined(HAVE_ATOMIC_H)
68 #include <atomic.h>
69 typedef unsigned int rb_atomic_t;
70
71 # define ATOMIC_SET(var, val) (void)atomic_swap_uint(&(var), (val))
72 # define ATOMIC_INC(var) atomic_inc_uint(&(var))
73 # define ATOMIC_DEC(var) atomic_dec_uint(&(var))
74 # define ATOMIC_OR(var, val) atomic_or_uint(&(var), (val))
75 # define ATOMIC_EXCHANGE(var, val) atomic_swap_uint(&(var), (val))
76 # define ATOMIC_CAS(var, oldval, newval) atomic_cas_uint(&(var), (oldval), (newval))
77
78 # if SIZEOF_SIZE_T == SIZEOF_LONG
79 # define ATOMIC_SIZE_ADD(var, val) atomic_add_long(&(var), (val))
80 # define ATOMIC_SIZE_SUB(var, val) atomic_add_long(&(var), -(val))
81 # define ATOMIC_SIZE_INC(var) atomic_inc_ulong(&(var))
82 # define ATOMIC_SIZE_DEC(var) atomic_dec_ulong(&(var))
83 # define ATOMIC_SIZE_EXCHANGE(var, val) atomic_swap_ulong(&(var), (val))
84 # else
85 # define ATOMIC_SIZE_ADD(var, val) atomic_add_int(&(var), (val))
86 # define ATOMIC_SIZE_SUB(var, val) atomic_add_int(&(var), -(val))
87 # define ATOMIC_SIZE_INC(var) atomic_inc_uint(&(var))
88 # define ATOMIC_SIZE_DEC(var) atomic_dec_uint(&(var))
89 # define ATOMIC_SIZE_EXCHANGE(var, val) atomic_swap_uint(&(var), (val))
90 # endif
91
92 #else
93 typedef int rb_atomic_t;
94 #define NEED_RUBY_ATOMIC_OPS
95 extern rb_atomic_t ruby_atomic_exchange(rb_atomic_t *ptr, rb_atomic_t val);
96 extern rb_atomic_t ruby_atomic_compare_and_swap(rb_atomic_t *ptr,
97 rb_atomic_t cmp,
98 rb_atomic_t newval);
99
100 # define ATOMIC_SET(var, val) (void)((var) = (val))
101 # define ATOMIC_INC(var) ((var)++)
102 # define ATOMIC_DEC(var) ((var)--)
103 # define ATOMIC_OR(var, val) ((var) |= (val))
104 # define ATOMIC_EXCHANGE(var, val) ruby_atomic_exchange(&(var), (val))
105 # define ATOMIC_CAS(var, oldval, newval) ruby_atomic_compare_and_swap(&(var), (oldval), (newval))
106
107 # define ATOMIC_SIZE_ADD(var, val) (void)((var) += (val))
108 # define ATOMIC_SIZE_SUB(var, val) (void)((var) -= (val))
109 # define ATOMIC_SIZE_INC(var) ((var)++)
110 # define ATOMIC_SIZE_DEC(var) ((var)--)
111 # define ATOMIC_SIZE_EXCHANGE(var, val) atomic_size_exchange(&(var), (val))
112 static inline size_t
113 atomic_size_exchange(size_t *ptr, size_t val)
114 {
115 size_t old = *ptr;
116 *ptr = val;
117 return old;
118 }
119 #endif
120
121 #endif /* RUBY_ATOMIC_H */
atomic_size_exchange
static size_t atomic_size_exchange(size_t *ptr, size_t val)
Definition: ruby_atomic.h:113
var
VALUE var
Definition: tcltklib.c:5517
ruby_atomic_compare_and_swap
rb_atomic_t ruby_atomic_compare_and_swap(rb_atomic_t *ptr, rb_atomic_t cmp, rb_atomic_t newval)
Definition: signal.c:36
val
#define val
Definition: tcltklib.c:1949
ptr
return ptr
Definition: tcltklib.c:784
rb_atomic_t
int rb_atomic_t
Definition: ruby_atomic.h:93
cmp
static ID cmp
Definition: compar.c:16
ruby_atomic_exchange
rb_atomic_t ruby_atomic_exchange(rb_atomic_t *ptr, rb_atomic_t val)
Definition: signal.c:28