Scoop API

scoop:scoop.core:0.1.0

src/atomics.scoop

Selected production source · src/atomics.scoop

1/**
2 * The ordering of an atomic operation relative to other memory accesses. Allowed combinations
3 * depend on the operation.
4 */
5public enum MemoryOrder {
6 /**
7 * Provides atomicity without ordering unrelated accesses.
8 */
9 Relaxed,
10 /**
11 * Orders subsequent accesses after a matching release observed by this operation.
12 */
13 Acquire,
14 /**
15 * Publishes preceding accesses to an acquire that observes this operation.
16 */
17 Release,
18 /**
19 * Combines acquire and release for a read-modify-write operation.
20 */
21 AcqRel,
22 /**
23 * Participates in the sequentially consistent order in addition to the operation's
24 * acquire/release effects.
25 */
26 SeqCst }
28/**
29 * A shared atomic cell. Memory-order arguments must be compile-time enum variants. Loads permit
30 * Relaxed, Acquire, or SeqCst; stores permit Relaxed, Release, or SeqCst. Read-modify-write
31 * operations permit every order. Compare-exchange failure order cannot release or be stronger than
32 * success order.
33 */
34@Intrinsic("core_atomic_int")
35public class AtomicInt {
36 /**
37 * Atomically reads the current value with the selected load order.
38 */
39 @NoGC @Intrinsic("atomic_int_load")
40 public fun load(order: MemoryOrder = MemoryOrder.SeqCst): Int
42 /**
43 * Atomically replaces the value with the selected store order.
44 */
45 @NoGC @Intrinsic("atomic_int_store")
46 public fun store(value: Int, order: MemoryOrder = MemoryOrder.SeqCst): Unit
48 /**
49 * Atomically replaces the value and returns the value observed before replacement.
50 */
51 @NoGC @Intrinsic("atomic_int_exchange")
52 public fun exchange(value: Int, order: MemoryOrder = MemoryOrder.SeqCst): Int
54 /**
55 * Replaces the value only when it matches `expected`, returning whether replacement occurred.
56 */
57 @NoGC @Intrinsic("atomic_int_compare_and_set")
58 public fun compareAndSet(expected: Int, value: Int, successOrder: MemoryOrder = MemoryOrder.SeqCst, failureOrder: MemoryOrder = MemoryOrder.SeqCst): Boolean
60 /**
61 * Conditionally replaces the value and returns the value observed by the comparison.
62 */
63 @NoGC @Intrinsic("atomic_int_compare_and_exchange")
64 public fun compareAndExchange(expected: Int, value: Int, successOrder: MemoryOrder = MemoryOrder.SeqCst, failureOrder: MemoryOrder = MemoryOrder.SeqCst): Int
66 /**
67 * Atomically adds with integer wrapping and returns the previous value.
68 */
69 @NoGC @Intrinsic("atomic_int_fetch_add")
70 public fun fetchAdd(value: Int, order: MemoryOrder = MemoryOrder.SeqCst): Int
72 /**
73 * Atomically subtracts with integer wrapping and returns the previous value.
74 */
75 @NoGC @Intrinsic("atomic_int_fetch_sub")
76 public fun fetchSub(value: Int, order: MemoryOrder = MemoryOrder.SeqCst): Int
78 /**
79 * Atomically applies bitwise AND and returns the previous value.
80 */
81 @NoGC @Intrinsic("atomic_int_fetch_and")
82 public fun fetchAnd(value: Int, order: MemoryOrder = MemoryOrder.SeqCst): Int
84 /**
85 * Atomically applies bitwise OR and returns the previous value.
86 */
87 @NoGC @Intrinsic("atomic_int_fetch_or")
88 public fun fetchOr(value: Int, order: MemoryOrder = MemoryOrder.SeqCst): Int
90 /**
91 * Atomically applies bitwise XOR and returns the previous value.
92 */
93 @NoGC @Intrinsic("atomic_int_fetch_xor")
94 public fun fetchXor(value: Int, order: MemoryOrder = MemoryOrder.SeqCst): Int
96 /**
97 * Retries a compare-exchange loop and returns the old value. The callback may run more than
98 * once; callback side effects are not rolled back.
99 */
100 public fun getAndUpdate(update: (Int) -> Int): Int {
101 var current = load()
102 var next = update(current)
103 while (!compareAndSet(current, next)) {
104 current = load()
105 next = update(current)
106 }
107 return current
108 }
110 /**
111 * Retries a compare-exchange loop and returns the installed value. The callback may run more
112 * than once.
113 */
114 public fun updateAndGet(update: (Int) -> Int): Int {
115 var current = load()
116 var next = update(current)
117 while (!compareAndSet(current, next)) {
118 current = load()
119 next = update(current)
120 }
121 return next
122 }
125/**
126 * A shared atomic cell. Memory-order arguments must be compile-time enum variants. Loads permit
127 * Relaxed, Acquire, or SeqCst; stores permit Relaxed, Release, or SeqCst. Read-modify-write
128 * operations permit every order. Compare-exchange failure order cannot release or be stronger than
129 * success order.
130 */
131@Intrinsic("core_atomic_long")
132public class AtomicLong {
133 /**
134 * Atomically reads the current value with the selected load order.
135 */
136 @NoGC @Intrinsic("atomic_long_load")
137 public fun load(order: MemoryOrder = MemoryOrder.SeqCst): Long
139 /**
140 * Atomically replaces the value with the selected store order.
141 */
142 @NoGC @Intrinsic("atomic_long_store")
143 public fun store(value: Long, order: MemoryOrder = MemoryOrder.SeqCst): Unit
145 /**
146 * Atomically replaces the value and returns the value observed before replacement.
147 */
148 @NoGC @Intrinsic("atomic_long_exchange")
149 public fun exchange(value: Long, order: MemoryOrder = MemoryOrder.SeqCst): Long
151 /**
152 * Replaces the value only when it matches `expected`, returning whether replacement occurred.
153 */
154 @NoGC @Intrinsic("atomic_long_compare_and_set")
155 public fun compareAndSet(expected: Long, value: Long, successOrder: MemoryOrder = MemoryOrder.SeqCst, failureOrder: MemoryOrder = MemoryOrder.SeqCst): Boolean
157 /**
158 * Conditionally replaces the value and returns the value observed by the comparison.
159 */
160 @NoGC @Intrinsic("atomic_long_compare_and_exchange")
161 public fun compareAndExchange(expected: Long, value: Long, successOrder: MemoryOrder = MemoryOrder.SeqCst, failureOrder: MemoryOrder = MemoryOrder.SeqCst): Long
163 /**
164 * Atomically adds with integer wrapping and returns the previous value.
165 */
166 @NoGC @Intrinsic("atomic_long_fetch_add")
167 public fun fetchAdd(value: Long, order: MemoryOrder = MemoryOrder.SeqCst): Long
169 /**
170 * Atomically subtracts with integer wrapping and returns the previous value.
171 */
172 @NoGC @Intrinsic("atomic_long_fetch_sub")
173 public fun fetchSub(value: Long, order: MemoryOrder = MemoryOrder.SeqCst): Long
175 /**
176 * Atomically applies bitwise AND and returns the previous value.
177 */
178 @NoGC @Intrinsic("atomic_long_fetch_and")
179 public fun fetchAnd(value: Long, order: MemoryOrder = MemoryOrder.SeqCst): Long
181 /**
182 * Atomically applies bitwise OR and returns the previous value.
183 */
184 @NoGC @Intrinsic("atomic_long_fetch_or")
185 public fun fetchOr(value: Long, order: MemoryOrder = MemoryOrder.SeqCst): Long
187 /**
188 * Atomically applies bitwise XOR and returns the previous value.
189 */
190 @NoGC @Intrinsic("atomic_long_fetch_xor")
191 public fun fetchXor(value: Long, order: MemoryOrder = MemoryOrder.SeqCst): Long
193 /**
194 * Retries a compare-exchange loop and returns the old value. The callback may run more than
195 * once; callback side effects are not rolled back.
196 */
197 public fun getAndUpdate(update: (Long) -> Long): Long {
198 var current = load()
199 var next = update(current)
200 while (!compareAndSet(current, next)) {
201 current = load()
202 next = update(current)
203 }
204 return current
205 }
207 /**
208 * Retries a compare-exchange loop and returns the installed value. The callback may run more
209 * than once.
210 */
211 public fun updateAndGet(update: (Long) -> Long): Long {
212 var current = load()
213 var next = update(current)
214 while (!compareAndSet(current, next)) {
215 current = load()
216 next = update(current)
217 }
218 return next
219 }
222/**
223 * A shared atomic cell. Memory-order arguments must be compile-time enum variants. Loads permit
224 * Relaxed, Acquire, or SeqCst; stores permit Relaxed, Release, or SeqCst. Read-modify-write
225 * operations permit every order. Compare-exchange failure order cannot release or be stronger than
226 * success order.
227 */
228@Intrinsic("core_atomic_boolean")
229public class AtomicBoolean {
230 /**
231 * Atomically reads the current value with the selected load order.
232 */
233 @NoGC @Intrinsic("atomic_boolean_load")
234 public fun load(order: MemoryOrder = MemoryOrder.SeqCst): Boolean
236 /**
237 * Atomically replaces the value with the selected store order.
238 */
239 @NoGC @Intrinsic("atomic_boolean_store")
240 public fun store(value: Boolean, order: MemoryOrder = MemoryOrder.SeqCst): Unit
242 /**
243 * Atomically replaces the value and returns the value observed before replacement.
244 */
245 @NoGC @Intrinsic("atomic_boolean_exchange")
246 public fun exchange(value: Boolean, order: MemoryOrder = MemoryOrder.SeqCst): Boolean
248 /**
249 * Replaces the value only when it matches `expected`, returning whether replacement occurred.
250 */
251 @NoGC @Intrinsic("atomic_boolean_compare_and_set")
252 public fun compareAndSet(expected: Boolean, value: Boolean, successOrder: MemoryOrder = MemoryOrder.SeqCst, failureOrder: MemoryOrder = MemoryOrder.SeqCst): Boolean
254 /**
255 * Conditionally replaces the value and returns the value observed by the comparison.
256 */
257 @NoGC @Intrinsic("atomic_boolean_compare_and_exchange")
258 public fun compareAndExchange(expected: Boolean, value: Boolean, successOrder: MemoryOrder = MemoryOrder.SeqCst, failureOrder: MemoryOrder = MemoryOrder.SeqCst): Boolean
260 /**
261 * Atomically applies bitwise AND and returns the previous value.
262 */
263 @NoGC @Intrinsic("atomic_boolean_fetch_and")
264 public fun fetchAnd(value: Boolean, order: MemoryOrder = MemoryOrder.SeqCst): Boolean
266 /**
267 * Atomically applies bitwise OR and returns the previous value.
268 */
269 @NoGC @Intrinsic("atomic_boolean_fetch_or")
270 public fun fetchOr(value: Boolean, order: MemoryOrder = MemoryOrder.SeqCst): Boolean
272 /**
273 * Atomically applies bitwise XOR and returns the previous value.
274 */
275 @NoGC @Intrinsic("atomic_boolean_fetch_xor")
276 public fun fetchXor(value: Boolean, order: MemoryOrder = MemoryOrder.SeqCst): Boolean
278 /**
279 * Retries a compare-exchange loop and returns the old value. The callback may run more than
280 * once; callback side effects are not rolled back.
281 */
282 public fun getAndUpdate(update: (Boolean) -> Boolean): Boolean {
283 var current = load()
284 var next = update(current)
285 while (!compareAndSet(current, next)) {
286 current = load()
287 next = update(current)
288 }
289 return current
290 }
292 /**
293 * Retries a compare-exchange loop and returns the installed value. The callback may run more
294 * than once.
295 */
296 public fun updateAndGet(update: (Boolean) -> Boolean): Boolean {
297 var current = load()
298 var next = update(current)
299 while (!compareAndSet(current, next)) {
300 current = load()
301 next = update(current)
302 }
303 return next
304 }
307/**
308 * A shared atomic cell. Memory-order arguments must be compile-time enum variants. Loads permit
309 * Relaxed, Acquire, or SeqCst; stores permit Relaxed, Release, or SeqCst. Read-modify-write
310 * operations permit every order. Compare-exchange failure order cannot release or be stronger than
311 * success order. Reference comparison uses identity, not user-defined equality.
312 */
313@Intrinsic("core_atomic_ref")
314public class AtomicRef<T : ref> {
315 /**
316 * Atomically reads the current value with the selected load order.
317 */
318 @NoGC @Intrinsic("atomic_ref_load")
319 public fun load(order: MemoryOrder = MemoryOrder.SeqCst): T
321 /**
322 * Atomically replaces the value with the selected store order.
323 */
324 @NoGC @Intrinsic("atomic_ref_store")
325 public fun store(value: T, order: MemoryOrder = MemoryOrder.SeqCst): Unit
327 /**
328 * Atomically replaces the value and returns the value observed before replacement.
329 */
330 @NoGC @Intrinsic("atomic_ref_exchange")
331 public fun exchange(value: T, order: MemoryOrder = MemoryOrder.SeqCst): T
333 /**
334 * Replaces the value only when it matches `expected`, returning whether replacement occurred.
335 */
336 @NoGC @Intrinsic("atomic_ref_compare_and_set")
337 public fun compareAndSet(expected: T, value: T, successOrder: MemoryOrder = MemoryOrder.SeqCst, failureOrder: MemoryOrder = MemoryOrder.SeqCst): Boolean
339 /**
340 * Conditionally replaces the value and returns the value observed by the comparison.
341 */
342 @NoGC @Intrinsic("atomic_ref_compare_and_exchange")
343 public fun compareAndExchange(expected: T, value: T, successOrder: MemoryOrder = MemoryOrder.SeqCst, failureOrder: MemoryOrder = MemoryOrder.SeqCst): T
345 /**
346 * Retries a compare-exchange loop and returns the old value. The callback may run more than
347 * once; callback side effects are not rolled back.
348 */
349 public fun getAndUpdate(update: (T) -> T): T {
350 var current = load()
351 var next = update(current)
352 while (!compareAndSet(current, next)) {
353 current = load()
354 next = update(current)
355 }
356 return current
357 }
359 /**
360 * Retries a compare-exchange loop and returns the installed value. The callback may run more
361 * than once.
362 */
363 public fun updateAndGet(update: (T) -> T): T {
364 var current = load()
365 var next = update(current)
366 while (!compareAndSet(current, next)) {
367 current = load()
368 next = update(current)
369 }
370 return next
371 }