forked from crystal-lang/crystal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatomic.cr
254 lines (231 loc) · 7.65 KB
/
atomic.cr
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# A value that may be updated atomically.
#
# Only primitive integer types, reference types or nilable reference types
# can be used with an Atomic type.
struct Atomic(T)
# Creates an Atomic with the given initial value.
def initialize(@value : T)
{% if !T.union? && (T == Char || T < Int::Primitive || T < Enum) %}
# Support integer types, enum types, or char (because it's represented as an integer)
{% elsif T < Reference || (T.union? && T.union_types.all? { |t| t == Nil || t < Reference }) %}
# Support reference types, or union types with only nil or reference types
{% else %}
{{ raise "Can only create Atomic with primitive integer types, reference types or nilable reference types, not #{T}" }}
{% end %}
end
# Compares this atomic's value with *cmp*:
#
# * if they are equal, sets the value to *new*, and returns `{old_value, true}`
# * if they are not equal the value remains the same, and returns `{old_value, false}`
#
# ```
# atomic = Atomic.new(1)
#
# atomic.compare_and_set(2, 3) # => {1, false}
# atomic.get # => 1
#
# atomic.compare_and_set(1, 3) # => {1, true}
# atomic.get # => 3
# ```
def compare_and_set(cmp : T, new : T) : {T, Bool}
# Check if it's a nilable reference type
{% if T.union? && T.union_types.all? { |t| t == Nil || t < Reference } %}
# If so, use addresses because LLVM < 3.9 doesn't support cmpxchg with pointers
address, success = Ops.cmpxchg(pointerof(@value).as(LibC::SizeT*), LibC::SizeT.new(cmp.as(T).object_id), LibC::SizeT.new(new.as(T).object_id), :sequentially_consistent, :sequentially_consistent)
{address == 0 ? nil : Pointer(T).new(address).as(T), success}
# Check if it's a reference type
{% elsif T < Reference %}
# Use addresses again (but this can't return nil)
address, success = Ops.cmpxchg(pointerof(@value).as(LibC::SizeT*), LibC::SizeT.new(cmp.as(T).object_id), LibC::SizeT.new(new.as(T).object_id), :sequentially_consistent, :sequentially_consistent)
{Pointer(T).new(address).as(T), success}
{% else %}
# Otherwise, this is an integer type
Ops.cmpxchg(pointerof(@value), cmp, new, :sequentially_consistent, :sequentially_consistent)
{% end %}
end
# Performs `atomic_value += value`. Returns the old value.
#
# ```
# atomic = Atomic.new(1)
# atomic.add(2) # => 1
# atomic.get # => 3
# ```
def add(value : T) : T
Ops.atomicrmw(:add, pointerof(@value), value, :sequentially_consistent, false)
end
# Performs `atomic_value -= value`. Returns the old value.
#
# ```
# atomic = Atomic.new(9)
# atomic.sub(2) # => 9
# atomic.get # => 7
# ```
def sub(value : T) : T
Ops.atomicrmw(:sub, pointerof(@value), value, :sequentially_consistent, false)
end
# Performs `atomic_value &= value`. Returns the old value.
#
# ```
# atomic = Atomic.new(5)
# atomic.and(3) # => 5
# atomic.get # => 1
# ```
def and(value : T) : T
Ops.atomicrmw(:and, pointerof(@value), value, :sequentially_consistent, false)
end
# Performs `atomic_value = ~(atomic_value & value)`. Returns the old value.
#
# ```
# atomic = Atomic.new(5)
# atomic.nand(3) # => 5
# atomic.get # => -2
# ```
def nand(value : T) : T
Ops.atomicrmw(:nand, pointerof(@value), value, :sequentially_consistent, false)
end
# Performs `atomic_value |= value`. Returns the old value.
#
# ```
# atomic = Atomic.new(5)
# atomic.or(2) # => 5
# atomic.get # => 7
# ```
def or(value : T) : T
Ops.atomicrmw(:or, pointerof(@value), value, :sequentially_consistent, false)
end
# Performs `atomic_value ^= value`. Returns the old value.
#
# ```
# atomic = Atomic.new(5)
# atomic.xor(3) # => 5
# atomic.get # => 6
# ```
def xor(value : T) : T
Ops.atomicrmw(:xor, pointerof(@value), value, :sequentially_consistent, false)
end
# Performs `atomic_value = max(atomic_value, value)`. Returns the old value.
#
# ```
# atomic = Atomic.new(5)
#
# atomic.max(3) # => 5
# atomic.get # => 5
#
# atomic.max(10) # => 5
# atomic.get # => 10
# ```
def max(value : T)
{% if T < Int::Signed %}
Ops.atomicrmw(:max, pointerof(@value), value, :sequentially_consistent, false)
{% else %}
Ops.atomicrmw(:umax, pointerof(@value), value, :sequentially_consistent, false)
{% end %}
end
# Performs `atomic_value = min(atomic_value, value)`. Returns the old value.
#
# ```
# atomic = Atomic.new(5)
#
# atomic.min(10) # => 5
# atomic.get # => 5
#
# atomic.min(3) # => 5
# atomic.get # => 3
# ```
def min(value : T)
{% if T < Int::Signed %}
Ops.atomicrmw(:min, pointerof(@value), value, :sequentially_consistent, false)
{% else %}
Ops.atomicrmw(:umin, pointerof(@value), value, :sequentially_consistent, false)
{% end %}
end
# Atomically sets this atomic's value to *value*. Returns the **old** value.
#
# ```
# atomic = Atomic.new(5)
# atomic.swap(10) # => 5
# atomic.get # => 10
# ```
def swap(value : T)
{% if T.union? && T.union_types.all? { |t| t == Nil || t < Reference } || T < Reference %}
address = Ops.atomicrmw(:xchg, pointerof(@value).as(LibC::SizeT*), LibC::SizeT.new(value.as(T).object_id), :sequentially_consistent, false)
Pointer(T).new(address).as(T)
{% else %}
Ops.atomicrmw(:xchg, pointerof(@value), value, :sequentially_consistent, false)
{% end %}
end
# Atomically sets this atomic's value to *value*. Returns the **new** value.
#
# ```
# atomic = Atomic.new(5)
# atomic.set(10) # => 10
# atomic.get # => 10
# ```
def set(value : T) : T
Ops.store(pointerof(@value), value.as(T), :sequentially_consistent, true)
value
end
# **Non-atomically** sets this atomic's value to *value*. Returns the **new** value.
#
# ```
# atomic = Atomic.new(5)
# atomic.lazy_set(10) # => 10
# atomic.get # => 10
# ```
def lazy_set(@value : T) : T
end
# Atomically returns this atomic's value.
def get : T
Ops.load(pointerof(@value), :sequentially_consistent, true)
end
# **Non-atomically** returns this atomic's value.
def lazy_get
@value
end
# :nodoc:
module Ops
# Defines methods that directly map to LLVM instructions related to atomic operations.
@[Primitive(:cmpxchg)]
def self.cmpxchg(ptr : T*, cmp : T, new : T, success_ordering : Symbol, failure_ordering : Symbol) : {T, Bool} forall T
end
@[Primitive(:atomicrmw)]
def self.atomicrmw(op : Symbol, ptr : T*, val : T, ordering : Symbol, singlethread : Bool) : T forall T
end
@[Primitive(:fence)]
def self.fence(ordering : Symbol, singlethread : Bool) : Nil
end
@[Primitive(:load_atomic)]
def self.load(ptr : T*, ordering : Symbol, volatile : Bool) : T forall T
end
@[Primitive(:store_atomic)]
def self.store(ptr : T*, value : T, ordering : Symbol, volatile : Bool) : Nil forall T
end
end
end
# An atomic flag, that can be set or not.
#
# Concurrency safe. If many fibers try to set the atomic in parallel, only one
# will succeed.
#
# Example:
# ```
# flag = Atomic::Flag.new
# flag.test_and_set # => true
# flag.test_and_set # => false
# flag.clear
# flag.test_and_set # => true
# ```
struct Atomic::Flag
def initialize
@value = 0_u8
end
# Atomically tries to set the flag. Only succeeds and returns `true` if the
# flag wasn't previously set; returns `false` otherwise.
def test_and_set : Bool
Atomic::Ops.atomicrmw(:xchg, pointerof(@value), 1_u8, :sequentially_consistent, false) == 0_u8
end
# Atomically clears the flag.
def clear : Nil
Atomic::Ops.store(pointerof(@value), 0_u8, :sequentially_consistent, true)
end
end