-
Notifications
You must be signed in to change notification settings - Fork 1
/
GenWaitList.scala
230 lines (197 loc) · 6.46 KB
/
GenWaitList.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
/*
* 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 async
import cats.~>
import cats.effect.kernel.Async
trait GenWaitList[F[_], A] { self =>
def trySet: A =#> Boolean
def tryGet: Axn[Option[A]]
def asyncSet(a: A): F[Unit]
def asyncGet: F[A]
def mapK[G[_]](t: F ~> G)(implicit G: Reactive[G]): GenWaitList[G, A]
}
trait WaitList[F[_], A] extends GenWaitList[F, A] { self =>
def set: A =#> Unit
final override def trySet: A =#> Boolean =
this.set.as(true)
override def mapK[G[_]](t: F ~> G)(implicit G: Reactive[G]): WaitList[G, A]
}
object GenWaitList {
private type Callback[A] =
Either[Throwable, A] => Unit
private[this] final val RightUnit: Either[Nothing, Unit] =
Right(())
def genWaitListForAsync[F[_], A](
tryGet: Axn[Option[A]],
trySet: A =#> Boolean,
)(implicit F: Async[F], rF: AsyncReactive[F]): Axn[GenWaitList[F, A]] = {
data.Queue.unboundedWithRemove[Callback[A]].flatMapF { getters =>
data.Queue.unboundedWithRemove[(A, Callback[Unit])].map { setters =>
new AsyncGenWaitList[F, A](tryGet, trySet, getters, setters)
}
}
}
def waitListForAsync[F[_], A](
tryGet: Axn[Option[A]],
syncSet: A =#> Unit
)(implicit F: Async[F], rF: AsyncReactive[F]): Axn[WaitList[F, A]] = {
data.Queue.unboundedWithRemove[Callback[A]].map { waiters =>
new AsyncWaitList[F, A](tryGet, syncSet, waiters)
}
}
private abstract class GenWaitListCommon[F[_], A]
extends GenWaitList[F, A] { self =>
def mapK[G[_]](t: F ~> G)(implicit G: Reactive[G]): GenWaitList[G, A] = {
new GenWaitListCommon[G, A] {
override def trySet =
self.trySet
override def tryGet =
self.tryGet
override def asyncSet(a: A): G[Unit] =
t(self.asyncSet(a))
override def asyncGet: G[A] =
t(self.asyncGet)
}
}
protected[this] final def callCb[B](cb: Callback[B]): Rxn[B, Unit] = {
Rxn.identity[B].postCommit(Rxn.unsafe.delay { (b: B) =>
cb(Right(b))
}).void
}
}
private final class AsyncGenWaitList[F[_], A](
_tryGet: Axn[Option[A]],
_trySet: A =#> Boolean,
getters: data.Queue.WithRemove[Callback[A]],
setters: data.Queue.WithRemove[(A, Callback[Unit])],
)(implicit F: Async[F], rF: AsyncReactive[F]) extends GenWaitListCommon[F, A] {
private[this] val rightUnit: Either[Nothing, Unit] =
RightUnit
override def trySet: A =#> Boolean = {
getters.tryDeque.flatMap {
case Some(cb) =>
callCb(cb).as(true)
case None =>
_trySet
}
}
override def tryGet: Axn[Option[A]] = {
_tryGet.flatMapF {
case s @ Some(_) =>
// success, try to unblock a setter:
setters.tryDeque.flatMapF {
case Some((setterVal, setterCb)) =>
_trySet.provide(setterVal).flatMapF { ok =>
if (ok) callCbUnit(setterCb).as(s)
else throw new IllegalStateException("couldn't _trySet after successful _tryGet")
}
case None =>
// no setter to unblock:
Rxn.pure(s)
}
case None =>
// can't get, so it's "empty"; however,
// it can also be "full" (e.g., queue of
// size 0), so we need to check setters:
setters.tryDeque.flatMapF {
case Some((setterVal, setterCb)) =>
callCbUnit(setterCb).as(Some(setterVal))
case None =>
Rxn.pure(None)
}
}
}
override def asyncSet(a: A): F[Unit] = {
F.asyncCheckAttempt { cb =>
rF.apply(
getters.tryDeque.flatMap {
case Some(getterCb) =>
callCb(getterCb).as(rightUnit)
case None =>
_trySet.flatMapF { ok =>
if (ok) Rxn.pure(rightUnit)
else setters.enqueueWithRemover.provide((a, cb)).map { remover => Left(Some(rF.run(remover))) }
}
},
a
)
}
}
override def asyncGet: F[A] = {
F.asyncCheckAttempt { cb =>
rF.run(
this.tryGet.flatMapF {
case Some(a) =>
Rxn.pure(Right(a))
case None =>
getters.enqueueWithRemover.provide(cb).map { remover => Left(Some(rF.run(remover))) }
}
)
}
}
private[this] final def callCbUnit(cb: Callback[Unit]): Rxn[Any, Unit] = {
this.callCb(cb).provide(())
}
}
private abstract class WaitListCommon[F[_], A]
extends GenWaitListCommon[F, A]
with WaitList[F, A] { self =>
final override def mapK[G[_]](t: F ~> G)(implicit G: Reactive[G]): WaitList[G, A] = {
new WaitListCommon[G, A] with WaitList[G, A] {
override def set =
self.set
final override def asyncSet(a: A): G[Unit] =
G.apply(this.set, a)
override def asyncGet =
t(self.asyncGet)
override def tryGet =
self.tryGet
}
}
}
private final class AsyncWaitList[F[_], A](
val tryGet: Axn[Option[A]],
val syncSet: A =#> Unit,
waiters: data.Queue.WithRemove[Callback[A]],
)(implicit F: Async[F], arF: AsyncReactive[F]) extends WaitListCommon[F, A] { self =>
final def set: A =#> Unit = {
this.waiters.tryDeque.flatMap {
case None =>
this.syncSet
case Some(cb) =>
callCb(cb)
}
}
final def asyncSet(a: A): F[Unit] = {
arF.apply(this.set, a)
}
final def asyncGet: F[A] = {
F.asyncCheckAttempt { cb =>
val rxn: Axn[Either[Some[F[Unit]], A]] = this.tryGet.flatMapF {
case Some(a) =>
Rxn.pure(Right(a))
case None =>
this.waiters.enqueueWithRemover.provide(cb).map { remover =>
Left(Some(arF.run(remover)))
}
}
arF.run(rxn)
}
}
}
}