-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exchanger.scala
158 lines (132 loc) · 4.09 KB
/
Exchanger.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
/*
* 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 core
import java.util.concurrent.atomic.LongAdder
sealed trait Exchanger[A, B] {
def exchange: Rxn[A, B]
def dual: Exchanger[B, A]
private[core] def key: Exchanger.Key
}
private[choam] object Exchanger extends ExchangerCompanionPlatform { // TODO: should be private[core]
private[core] def apply[A, B]: Axn[Exchanger[A, B]] =
Rxn.unsafe.delay { _ => this.unsafe[A, B] }
private[choam] def profiled[A, B](counter: LongAdder): Axn[Exchanger[A, B]] = { // TODO: should be private[core]
this.apply[A, B].flatMapF { underlying =>
Rxn.unsafe.delay { _ =>
new ProfiledExchanger[A, B](
d = null,
underlying = underlying,
counter = counter,
)
}
}
}
private[core] final class Key
extends Serializable {
final override def toString: String =
s"Key@${this.hashCode.toHexString}"
}
private[core] trait UnsealedExchanger[A, B]
extends Exchanger[A, B]
private[this] final class ProfiledExchanger[A, B](
d: ProfiledExchanger[B, A],
underlying: Exchanger[A, B],
counter: LongAdder,
) extends UnsealedExchanger[A, B] {
private[this] val isPrimary: Boolean =
d eq null
final override def exchange: Rxn[A, B] = {
// Every exchange has 2 sides, so only
// the primary side increments the counter:
if (this.isPrimary) {
underlying.exchange.postCommit(Axn.unsafe.delay {
counter.increment()
})
} else {
underlying.exchange
}
}
final override val dual: Exchanger[B, A] = {
if (d ne null) d
else new ProfiledExchanger[B, A](this, underlying.dual, counter)
}
private[core] final override val key =
underlying.key
}
// TODO: these are JVM-only:
import internal.mcas.{ Mcas, Descriptor }
private[choam] val paramsKey = // TODO: should be private[core]
new Exchanger.Key
// TODO: these are temporarily mutable for benchmarking
@volatile
private[core] var params: Params =
Params()
private[core] final case class Params(
final val maxMisses: Byte =
64,
final val minMisses: Byte =
-64,
final val maxExchanges: Byte =
4,
final val minExchanges: Byte =
-4,
final val maxSizeShift: Byte =
8,
final val maxSpin: Int =
1024,
// these two are interdependent:
final val defaultSpin: Int =
128,
final val maxSpinShift: Byte =
16,
)
private[core] final case class Msg(
value: Any,
contK: ObjStack.Lst[Any],
contT: Array[Byte],
desc: Descriptor,
postCommit: ObjStack.Lst[Axn[Unit]],
exchangerData: Rxn.ExStatMap,
)
private[core] object Msg {
def fromFinishedEx(fx: FinishedEx[_], newStats: Rxn.ExStatMap, ctx: Mcas.ThreadContext): Msg = {
Msg(
value = fx.result,
contK = fx.contK,
contT = fx.contT,
desc = ctx.start(),
postCommit = ObjStack.Lst.empty[Axn[Unit]],
exchangerData = newStats,
)
}
}
private[core] sealed abstract class NodeResult[C]
private[core] final class FinishedEx[C](
val result: C,
val contK: ObjStack.Lst[Any],
val contT: Array[Byte],
) extends NodeResult[C]
private[core] final class Rescinded[C]
extends NodeResult[C]
private[core] final object Rescinded {
def apply[C]: Rescinded[C] =
_singleton.asInstanceOf[Rescinded[C]]
private[this] val _singleton =
new Rescinded[Any]
}
}