-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ref.scala
407 lines (321 loc) · 12.8 KB
/
Ref.scala
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2016-2024 Daniel Urban and contributors listed in NOTICE.txt
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.tauri.choam
package refs
import scala.math.Ordering
import cats.kernel.{ Hash, Order }
import cats.effect.kernel.{ Ref => CatsRef }
import internal.mcas.{ MemoryLocation, RefIdGen }
import CompatPlatform.AtomicReferenceArray
/**
* A mutable memory location with a pure API and
* composable lock-free operations.
*
* `Ref` is similar to [[java.util.concurrent.atomic.AtomicReference]]
* or [[cats.effect.kernel.Ref]], but its operations are [[Rxn]]s.
* Thus, operations on a `Ref` are composable with other [[Rxn]]s.
*/
sealed trait Ref[A] extends RefLike[A] { this: MemoryLocation[A] =>
final override def get: Axn[A] =
Rxn.ref.get(this)
// TODO: needs better name (it's like `modify`)
final override def upd[B, C](f: (A, B) => (A, C)): Rxn[B, C] =
Rxn.ref.upd(this)(f)
// TODO: needs better name (it's like `modifyWith`)
final override def updWith[B, C](f: (A, B) => Axn[(A, C)]): Rxn[B, C] =
Rxn.ref.updWith(this)(f)
final def unsafeDirectRead: Axn[A] =
Rxn.unsafe.directRead(this)
final def unsafeTicketRead: Axn[Rxn.unsafe.Ticket[A]] =
Rxn.unsafe.ticketRead(this)
final def unsafeCas(ov: A, nv: A): Axn[Unit] =
Rxn.unsafe.cas(this, ov, nv)
final override def toCats[F[_]](implicit F: core.Reactive[F]): CatsRef[F, A] =
new Ref.CatsRefFromRef[F, A](this) {}
private[choam] final def loc: MemoryLocation[A] =
this
private[choam] def dummy(v: Byte): Long
}
private[refs] trait UnsealedRef[A] extends Ref[A] { this: MemoryLocation[A] =>
}
object Ref extends RefInstances0 {
sealed trait Array[A] {
def size: Int
def unsafeGet(idx: Int): Ref[A]
def apply(idx: Int): Option[Ref[A]]
final def length: Int =
this.size
}
final object Array {
final case class AllocationStrategy private (
sparse: Boolean,
flat: Boolean,
padded: Boolean,
) {
require(!(padded && flat), "padding is currently not supported for flat = true")
final def withSparse(sparse: Boolean): AllocationStrategy =
this.copy(sparse = sparse)
final def withFlat(flat: Boolean): AllocationStrategy =
this.copy(flat = flat)
final def withPadded(padded: Boolean): AllocationStrategy =
this.copy(padded = padded)
private[Ref] final def toInt: Int = {
var r = 0
if (this.sparse) {
r |= 4
}
if (this.flat) {
r |= 2
}
if (this.padded) {
r |= 1
}
r
}
}
final object AllocationStrategy {
final val Default: AllocationStrategy =
this.apply(sparse = false, flat = true, padded = false)
private[choam] val SparseFlat: AllocationStrategy =
this.apply(sparse = true, flat = true, padded = false)
private[Ref] final val DefaultInt: Int =
2
final def apply(sparse: Boolean, flat: Boolean, padded: Boolean): AllocationStrategy =
new AllocationStrategy(sparse = sparse, flat = flat, padded = padded)
}
}
private[refs] trait UnsealedArray[A] extends Array[A] { this: RefIdOnly =>
protected[refs] final override def refToString(): String = {
val idBase = this.id
s"Ref.Array[${size}]@${internal.mcas.refHashString(idBase)}"
}
protected final def checkIndex(idx: Int): Unit = {
if ((idx < 0) || (idx >= size)) {
throw new IndexOutOfBoundsException(s"Index ${idx} out of bounds for length ${size}")
}
}
}
private final class EmptyRefArray[A] extends Ref.Array[A] {
final override val size: Int =
0
final override def apply(idx: Int): Option[Ref[A]] =
None
final override def unsafeGet(idx: Int): Ref[A] =
throw new IndexOutOfBoundsException(s"Index ${idx} out of bounds for length 0")
final override def toString: String =
s"Ref.Array[0]@${java.lang.Long.toHexString(0L)}"
}
private[choam] final def apply[A](initial: A): Axn[Ref[A]] =
padded(initial)
// TODO: How to avoid allocating RefArrayRef objects?
// TODO: Create getAndUpdate(idx: Int, f: A => A) methods.
// TODO: But: what to do with out-of-bounds indices?
// TODO: (Refined? But can we avoid boxing?)
// TODO: Would implementing Traverse help? Probably not.
final def array[A](size: Int, initial: A): Axn[Ref.Array[A]] = {
safeArray(size = size, initial = initial, strategy = Ref.Array.AllocationStrategy.DefaultInt)
}
final def array[A](size: Int, initial: A, strategy: Ref.Array.AllocationStrategy): Axn[Ref.Array[A]] = {
safeArray(size = size, initial = initial, strategy = strategy.toInt)
}
private[choam] final def unsafeArray[A](size: Int, initial: A, strategy: Ref.Array.AllocationStrategy): Ref.Array[A] = {
unsafeArray(size, initial, strategy.toInt, RefIdGen.global)
}
// the duplicated logic with unsafeArray is to avoid
// having the `if` and `match` inside the `Axn`:
private[this] final def safeArray[A](size: Int, initial: A, strategy: Int): Axn[Ref.Array[A]] = {
if (size > 0) {
(strategy : @switch) match {
case 0 => Axn.unsafe.delayContext(ctx => new StrictArrayOfRefs(size, initial, padded = false, rig = ctx.refIdGen))
case 1 => Axn.unsafe.delayContext(ctx => new StrictArrayOfRefs(size, initial, padded = true, rig = ctx.refIdGen))
case 2 => Axn.unsafe.delayContext(ctx => unsafeStrictArray(size, initial, ctx.refIdGen))
case 3 => throw new IllegalArgumentException("flat && padded not implemented yet")
case 4 => Axn.unsafe.delay(new LazyArrayOfRefs(size, initial, padded = false))
case 5 => Axn.unsafe.delay(new LazyArrayOfRefs(size, initial, padded = true))
case 6 => Axn.unsafe.delayContext(ctx => unsafeLazyArray(size, initial, ctx.refIdGen))
case 7 => throw new IllegalArgumentException("flat && padded not implemented yet")
case _ => throw new IllegalArgumentException(s"invalid strategy: ${strategy}")
}
} else if (size == 0) {
Axn.unsafe.delay(new EmptyRefArray[A])
} else {
throw new IllegalArgumentException(s"size = ${size}")
}
}
private[this] final def unsafeArray[A](size: Int, initial: A, strategy: Int, rig: RefIdGen): Ref.Array[A] = {
if (size > 0) {
(strategy : @switch) match {
case 0 => new StrictArrayOfRefs(size, initial, padded = false, rig = rig)
case 1 => new StrictArrayOfRefs(size, initial, padded = true, rig = rig)
case 2 => unsafeStrictArray(size, initial, rig)
case 3 => throw new IllegalArgumentException("flat && padded not implemented yet")
case 4 => new LazyArrayOfRefs(size, initial, padded = false)
case 5 => new LazyArrayOfRefs(size, initial, padded = true)
case 6 => unsafeLazyArray(size, initial, rig)
case 7 => throw new IllegalArgumentException("flat && padded not implemented yet")
case _ => throw new IllegalArgumentException(s"invalid strategy: ${strategy}")
}
} else if (size == 0) {
new EmptyRefArray[A]
} else {
throw new IllegalArgumentException(s"size = ${size}")
}
}
private[refs] final class StrictArrayOfRefs[A](
final override val size: Int,
initial: A,
padded: Boolean,
rig: RefIdGen,
) extends Ref.Array[A] {
require(size > 0)
private[this] val arr: scala.Array[Ref[A]] = {
val a = new scala.Array[Ref[A]](size)
var idx = 0
while (idx < size) {
a(idx) = if (padded) {
Ref.unsafePadded(initial, rig)
} else {
Ref.unsafeUnpadded(initial, rig)
}
idx += 1
}
a
}
final override def unsafeGet(idx: Int): Ref[A] = {
CompatPlatform.checkArrayIndexIfScalaJs(idx, size) // TODO: check other places where we might need this
this.arr(idx)
}
final override def apply(idx: Int): Option[Ref[A]] = {
if ((idx >= 0) && (idx < size)) {
Some(this.unsafeGet(idx))
} else {
None
}
}
}
private[refs] final class LazyArrayOfRefs[A](
final override val size: Int,
initial: A,
padded: Boolean,
) extends Ref.Array[A] {
require(size > 0)
private[this] val arr: AtomicReferenceArray[Ref[A]] =
new AtomicReferenceArray[Ref[A]](size)
final override def unsafeGet(idx: Int): Ref[A] = {
val arr = this.arr
arr.getOpaque(idx) match { // FIXME: reading a `Ref` with a race!
case null =>
val nv = if (padded) {
Ref.unsafePadded(initial) // TODO: this uses global RIG
} else {
Ref.unsafeUnpadded(initial) // TODO: this uses global RIG
}
val wit = arr.compareAndExchange(idx, null, nv)
if (wit eq null) {
nv // we're the first
} else {
wit // found other
}
case ref =>
ref
}
}
final override def apply(idx: Int): Option[Ref[A]] = {
if ((idx >= 0) && (idx < size)) {
Some(this.unsafeGet(idx))
} else {
None
}
}
}
private[choam] final def catsRefFromRef[F[_] : Reactive, A](ref: Ref[A]): CatsRef[F, A] =
new CatsRefFromRef[F, A](ref) {}
private[refs] abstract class CatsRefFromRef[F[_], A](self: Ref[A])(implicit F: Reactive[F])
extends RefLike.CatsRefFromRefLike[F, A](self)(F) {
override def get: F[A] =
self.unsafeDirectRead.run[F]
}
private[this] final def unsafeStrictArray[A](size: Int, initial: A, rig: RefIdGen): Ref.Array[A] = {
require(size > 0)
unsafeNewStrictRefArray[A](size = size, initial = initial)(rig.nextArrayIdBase(size))
}
private[this] final def unsafeLazyArray[A](size: Int, initial: A, rig: RefIdGen): Ref.Array[A] = {
require(size > 0)
unsafeNewSparseRefArray[A](size = size, initial = initial)(rig.nextArrayIdBase(size))
}
final def padded[A](initial: A): Axn[Ref[A]] =
Axn.unsafe.delayContext[Ref[A]](ctx => Ref.unsafePadded(initial, ctx.refIdGen))
final def unpadded[A](initial: A): Axn[Ref[A]] =
Axn.unsafe.delayContext[Ref[A]](ctx => Ref.unsafeUnpadded(initial, ctx.refIdGen))
private[choam] final def unsafe[A](initial: A): Ref[A] = // TODO: don't use this (except in tests)
unsafePadded(initial)
private[choam] final def unsafePadded[A](initial: A): Ref[A] =
this.unsafePadded(initial, RefIdGen.global)
private[choam] final def unsafePadded[A](initial: A, rig: RefIdGen): Ref[A] = {
unsafeNewRefP1(initial)(rig.nextId())
}
private[choam] final def unsafeUnpadded[A](initial: A): Ref[A] =
this.unsafeUnpadded(initial, RefIdGen.global)
private[choam] final def unsafeUnpadded[A](initial: A, rig: RefIdGen): Ref[A] = {
unsafeNewRefU1(initial)(rig.nextId())
}
// Ref2:
def refP1P1[A, B](a: A, b: B): Axn[Ref2[A, B]] =
Ref2.p1p1(a, b)
def refP2[A, B](a: A, b: B): Axn[Ref2[A, B]] =
Ref2.p2(a, b)
// Utilities:
final def consistentRead[A, B](ra: Ref[A], rb: Ref[B]): Axn[(A, B)] = {
ra.get * rb.get
}
final def consistentReadMany[A](refs: List[Ref[A]]): Axn[List[A]] = {
refs.foldRight(Rxn.pure(List.empty[A])) { (ref, acc) =>
(ref.get * acc).map {
case (h, t) => h :: t
}
}
}
final def swap[A](r1: Ref[A], r2: Ref[A]): Axn[Unit] = {
r1.updateWith { o1 =>
r2.modify[A] { o2 =>
(o1, o2)
}
}
}
}
private[refs] sealed abstract class RefInstances0 extends RefInstances1 { this: Ref.type =>
private[this] val _orderingInstance: Ordering[Ref[Any]] = new Ordering[Ref[Any]] {
final override def compare(x: Ref[Any], y: Ref[Any]): Int =
MemoryLocation.globalCompare(x.loc, y.loc)
}
implicit final def orderingInstance[A]: Ordering[Ref[A]] =
_orderingInstance.asInstanceOf[Ordering[Ref[A]]]
}
private sealed abstract class RefInstances1 extends RefInstances2 { this: Ref.type =>
private[this] val _orderInstance: Order[Ref[Any]] = new Order[Ref[Any]] {
final override def compare(x: Ref[Any], y: Ref[Any]): Int =
MemoryLocation.globalCompare(x.loc, y.loc)
}
implicit final def orderInstance[A]: Order[Ref[A]] =
_orderInstance.asInstanceOf[Order[Ref[A]]]
}
private sealed abstract class RefInstances2 { this: Ref.type =>
private[this] val _hashInstance: Hash[Ref[Any]] =
Hash.fromUniversalHashCode[Ref[Any]]
implicit final def hashInstance[A]: Hash[Ref[A]] =
_hashInstance.asInstanceOf[Hash[Ref[A]]]
}