-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mcas.scala
374 lines (325 loc) · 11.5 KB
/
Mcas.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
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2016-2023 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 internal
package mcas
import java.util.concurrent.ThreadLocalRandom
/** Common interface for MCAS (i.e., k-CAS) implementations */
sealed trait Mcas {
/** Returns the context associated with the current thread */
def currentContext(): Mcas.ThreadContext
/** True iff `this` can be used to perform ops on arbitrary refs */
private[choam] def isThreadSafe: Boolean
/** Only for testing/benchmarking */
private[choam] def getRetryStats(): Mcas.RetryStats = {
Mcas.RetryStats(0L, 0L, 0L)
}
/** Only for testing/benchmarking */
private[choam] def collectExchangerStats(): Map[Long, Map[AnyRef, AnyRef]] = {
Map.empty
}
/** Only for testing/benchmarking */
private[choam] def maxReusedWeakRefs(): Int = {
0
}
}
object Mcas extends McasCompanionPlatform { self =>
private[mcas] trait UnsealedMcas extends Mcas
private[mcas] trait UnsealedThreadContext extends ThreadContext
sealed trait ThreadContext {
// abstract:
/**
* Starts building a descriptor
* (its `.validTs` will be the
* current global sersion).
*/
def start(): Descriptor
private[mcas] def addVersionCas(desc: Descriptor): Descriptor
/**
* @return the current value of `ref`, as
* if read by `readIntoHwd(ref).nv`.
*/
def readDirect[A](ref: MemoryLocation[A]): A
/**
* @return the current value and version of `ref`
* in a word descriptor object. The
* value and version are guaranteed to be
* consistent with each other. The descriptor's
* `.ov` and `.nv` are guaranteed to be the same.
*/
def readIntoHwd[A](ref: MemoryLocation[A]): HalfWordDescriptor[A]
/**
* @return the current version of `ref`, as if
* read by `readIntoHwd(ref).version`.
*/
private[mcas] def readVersion[A](ref: MemoryLocation[A]): Long
def validateAndTryExtend(
desc: Descriptor,
hwd: HalfWordDescriptor[_], // may be null
): Descriptor
/**
* Directly tries to perform the k-CAS described by `desc`
*
* @return either `EmcasStatus.Successful` (if successful);
* `EmcasStatus.FailedVal` (if failed due to an
* expected value not matching); or the current
* global version (if failed due to the version
* being newer than `desc.validTs`).
*/
private[mcas] def tryPerformInternal(desc: Descriptor): Long
/** @return a `ThreadLocalRandom` valid for the current thread */
def random: ThreadLocalRandom
// concrete:
/** Utility to first try to read from the log, and only from the ref if not found */
final def readMaybeFromLog[A](ref: MemoryLocation[A], log: Descriptor): Option[(A, Descriptor)] = {
log.getOrElseNull(ref) match {
case null =>
// not in log
this.readIntoLog(ref, log) match {
case null =>
None // None means rollback needed
case newLog =>
val a: A = newLog.getOrElseNull(ref).nv
Some((a, newLog))
}
case hwd =>
// found in log
Some((hwd.cast[A].nv, log))
}
}
private[this] final def readIntoLog[A](ref: MemoryLocation[A], log: Descriptor): Descriptor = {
require(log.getOrElseNull(ref) eq null)
val hwd = this.readIntoHwd(ref)
val newLog = log.add(hwd)
if (!newLog.isValidHwd(hwd)) {
// this returns null if we need to roll back
// (and we pass on the null to our caller):
val res = this.validateAndTryExtend(newLog, hwd = null)
if (res ne null) {
assert(res.isValidHwd(hwd))
}
res
} else {
newLog
}
}
/**
* Tries to perform the ops in `desc`, with adding a
* version-CAS (if not read-only).
*
* @return either `EmcasStatus.Successful` (if successful);
* `EmcasStatus.FailedVal` (if failed due to an
* expected value not matching); or the current
* global version (if failed due to the version
* being newer than `desc.validTs`).
*/
final def tryPerform(desc: Descriptor): Long = {
if (desc.readOnly) {
// we've validated each read,
// so nothing to do here
McasStatus.Successful
} else {
// TODO: Could we ignore read-only HWDs
// TODO: in the log? They're validated,
// TODO: and the version CAS _should_
// TODO: catch any concurrent changes(?)
val finalDesc = this.addVersionCas(desc)
val res = this.tryPerformInternal(finalDesc)
assert((res == McasStatus.Successful) || (res == McasStatus.FailedVal) || Version.isValid(res))
res
}
}
/** Like `tryPerform`, but returns whether it was successful */
final def tryPerformOk(desc: Descriptor): Boolean = {
tryPerform(desc) == McasStatus.Successful
}
final def addCasFromInitial[A](desc: Descriptor, ref: MemoryLocation[A], ov: A, nv: A): Descriptor =
this.addCasWithVersion(desc, ref, ov = ov, nv = nv, version = Version.Start)
final def addCasWithVersion[A](
desc: Descriptor,
ref: MemoryLocation[A],
ov: A,
nv: A,
version: Long
): Descriptor = {
val wd = HalfWordDescriptor(ref, ov, nv, version)
desc.add(wd)
}
/**
* Tries to (re)validate `desc` based on the current
* versions of the refs it contains.
*
* @return true, iff `desc` is still valid.
*/
private[mcas] final def validate(desc: Descriptor): Boolean = {
@tailrec
def go(it: Iterator[HalfWordDescriptor[_]]): Boolean = {
if (it.hasNext) {
if (this.validateHwd(it.next())) go(it)
else false
} else {
true
}
}
go(desc.iterator())
}
private[mcas] final def validateHwd[A](hwd: HalfWordDescriptor[A]): Boolean = {
val currVer: Long = this.readVersion(hwd.address)
currVer == hwd.version
}
/**
* @return a snapshot of `desc`.
*/
final def snapshot(desc: Descriptor): Descriptor =
desc
/**
* Merges disjoint descriptors `to` and `from`.
*
* @return The merged descriptor, which contains
* all the ops either in `to` or `from`.
*/
private[choam] final def addAll(to: Descriptor, from: Descriptor): Descriptor = {
Descriptor.merge(to, from, this)
}
/**
* Tries to perform a "bare" 1-CAS, without
* changing the global version. This breaks
* opacity guarantees! It may change the value
* of a ref without changing its version!
*/
final def singleCasDirect[A](ref: MemoryLocation[A], ov: A, nv: A): Boolean = {
assert(!equ(ov, nv))
val hwd = this.readIntoHwd(ref)
val d0 = this.start() // do this after reading, so version is deemed valid
assert(d0.isValidHwd(hwd))
if (equ(hwd.ov, ov)) {
// create a (dangerous) descriptor, which will set
// the new version to `validTs` (which may equal the
// old version):
val d1 = d0.add(hwd.withNv(nv)).withNoNewVersion
assert(d1.newVersion == d1.validTs)
// we're intentionally NOT having a version-CAS:
this.tryPerformInternal(d1) == McasStatus.Successful
} else {
false
}
}
/**
* Tries to perform a 1-CAS, but
* also handles versions. Equivalent
* to creating a descriptor containing a
* single op, and calling `tryPerformOk`
* on it (but may be more efficient).
*/
final def tryPerformSingleCas[A](ref: MemoryLocation[A], ov: A, nv: A): Boolean = {
// TODO: this could be optimized (probably)
val d0 = this.start()
val d1 = this.readIntoLog(ref, d0)
assert(d1 ne null)
val hwd = d1.getOrElseNull(ref)
assert(hwd ne null)
if (equ(hwd.ov, ov)) {
val d2 = d1.overwrite(hwd.withNv(nv))
this.tryPerform(d2) == McasStatus.Successful
} else {
false
}
}
// statistics/testing/benchmarking:
/** Only for testing */
private[mcas] final def builder(): Builder = {
new Builder(this, this.start())
}
/** Only for testing/benchmarking */
private[choam] def recordCommit(@unused fullRetries: Int, @unused mcasRetries: Int): Unit = {
// we ignore stats by default; implementations
// can override if it matters
()
}
/** Only for testing/benchmarking */
private[choam] def supportsStatistics: Boolean = {
// we ignore stats by default; implementations
// can override if it matters
false
}
/** Only for testing/benchmarking */
private[choam] def getStatisticsPlain(): Map[AnyRef, AnyRef] = {
// we ignore stats by default; implementations
// can override if it matters
Map.empty
}
/** Only for testing/benchmarking */
private[choam] def getStatisticsOpaque(): Map[AnyRef, AnyRef] = {
// we ignore stats by default; implementations
// can override if it matters
Map.empty
}
/** Only for testing/benchmarking */
private[choam] def setStatisticsPlain(@unused stats: Map[AnyRef, AnyRef]): Unit = {
// we ignore stats by default; implementations
// can override if it matters
}
/** Only for testing/benchmarking */
private[choam] def maxReusedWeakRefs(): Int = {
0
}
}
private[choam] final case class RetryStats(
commits: Long,
fullRetries: Long,
mcasRetries: Long,
)
/** Only for testing */
private[mcas] final class Builder(
private[this] val ctx: ThreadContext,
private[this] val desc: Descriptor,
) {
final def updateRef[A](ref: MemoryLocation[A], f: A => A): Builder = {
this.ctx.readMaybeFromLog(ref, this.desc) match {
case Some((ov, newDesc)) =>
val nv = f(ov)
val newestDesc = newDesc.overwrite(
newDesc.getOrElseNull(ref).withNv(nv)
)
new Builder(this.ctx, newestDesc)
case None =>
throw new IllegalStateException("couldn't extend, rollback is necessary")
}
}
final def casRef[A](ref: MemoryLocation[A], from: A, to: A): Builder = {
this.tryCasRef(ref, from, to).getOrElse(
throw new IllegalStateException("couldn't extend, rollback is necessary")
)
}
final def tryCasRef[A](ref: MemoryLocation[A], from: A, to: A): Option[Builder] = {
this.ctx.readMaybeFromLog(ref, this.desc).map {
case (ov, newDesc) =>
val newestDesc = if (equ(ov, from)) {
newDesc.overwrite(newDesc.getOrElseNull(ref).withNv(to))
} else {
val oldHwd = newDesc.getOrElseNull(ref)
val newHwd = HalfWordDescriptor(ref, from, to, oldHwd.version)
newDesc.overwrite(newHwd)
}
new Builder(this.ctx, newestDesc)
}
}
final def tryPerformOk(): Boolean = {
this.ctx.tryPerformOk(this.desc)
}
}
}