-
Notifications
You must be signed in to change notification settings - Fork 509
/
GenConcurrent.scala
343 lines (285 loc) · 12.2 KB
/
GenConcurrent.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
/*
* Copyright 2020-2022 Typelevel
*
* 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 cats.effect.kernel
import cats.{Monoid, Semigroup, Traverse}
import cats.data.{EitherT, IorT, Kleisli, OptionT, WriterT}
import cats.effect.kernel.instances.spawn._
import cats.effect.kernel.syntax.all._
import cats.syntax.all._
trait GenConcurrent[F[_], E] extends GenSpawn[F, E] {
import GenConcurrent._
def ref[A](a: A): F[Ref[F, A]]
def deferred[A]: F[Deferred[F, A]]
/**
* Caches the result of `fa`.
*
* The returned inner effect, hence referred to as `get`, when sequenced, will evaluate `fa`
* and cache the result. If `get` is sequenced multiple times `fa` will only be evaluated
* once.
*
* If all `get`s are canceled prior to `fa` completing, it will be canceled and evaluated
* again the next time `get` is sequenced.
*/
def memoize[A](fa: F[A]): F[F[A]] = {
import Memoize._
implicit val F: GenConcurrent[F, E] = this
ref[Memoize[F, E, A]](Unevaluated()) map { state =>
def eval: F[A] =
deferred[Unit] flatMap { latch =>
uncancelable { poll =>
state.modify {
case Unevaluated() =>
val go =
poll(fa).guaranteeCase { outcome =>
val stateAction = outcome match {
case Outcome.Canceled() =>
state.set(Unevaluated())
case Outcome.Errored(err) =>
state.set(Finished(Left(err)))
case Outcome.Succeeded(fa) =>
state.set(Finished(Right(fa)))
}
stateAction <* latch.complete(())
}
Evaluating(latch.get) -> go
case other =>
other -> poll(get)
}.flatten
}
}
def get: F[A] =
state.get flatMap {
case Unevaluated() => eval
case Evaluating(await) => await *> get
case Finished(efa) => fromEither(efa).flatten
}
get
}
}
/**
* Like `Parallel.parReplicateA`, but limits the degree of parallelism.
*/
def parReplicateAN[A](n: Int)(replicas: Int, ma: F[A]): F[List[A]] =
parSequenceN(n)(List.fill(replicas)(ma))
/**
* Like `Parallel.parSequence`, but limits the degree of parallelism.
*/
def parSequenceN[T[_]: Traverse, A](n: Int)(tma: T[F[A]]): F[T[A]] =
parTraverseN(n)(tma)(identity)
/**
* Like `Parallel.parTraverse`, but limits the degree of parallelism. Note that the semantics
* of this operation aim to maximise fairness: when a spot to execute becomes available, every
* task has a chance to claim it, and not only the next `n` tasks in `ta`
*/
def parTraverseN[T[_]: Traverse, A, B](n: Int)(ta: T[A])(f: A => F[B]): F[T[B]] = {
require(n >= 1, s"Concurrency limit should be at least 1, was: $n")
implicit val F: GenConcurrent[F, E] = this
MiniSemaphore[F](n).flatMap { sem => ta.parTraverse { a => sem.withPermit(f(a)) } }
}
override def racePair[A, B](fa: F[A], fb: F[B])
: F[Either[(Outcome[F, E, A], Fiber[F, E, B]), (Fiber[F, E, A], Outcome[F, E, B])]] = {
implicit val F: GenConcurrent[F, E] = this
uncancelable { poll =>
for {
result <-
deferred[Either[Outcome[F, E, A], Outcome[F, E, B]]]
fibA <- start(guaranteeCase(fa)(oc => result.complete(Left(oc)).void))
fibB <- start(guaranteeCase(fb)(oc => result.complete(Right(oc)).void))
back <- onCancel(
poll(result.get),
for {
canA <- start(fibA.cancel)
canB <- start(fibB.cancel)
_ <- canA.join
_ <- canB.join
} yield ())
} yield back match {
case Left(oc) => Left((oc, fibB))
case Right(oc) => Right((fibA, oc))
}
}
}
}
object GenConcurrent {
def apply[F[_], E](implicit F: GenConcurrent[F, E]): F.type = F
def apply[F[_]](implicit F: GenConcurrent[F, _], d: DummyImplicit): F.type = F
private sealed abstract class Memoize[F[_], E, A]
private object Memoize {
final case class Unevaluated[F[_], E, A]() extends Memoize[F, E, A]
final case class Evaluating[F[_], E, A](await: F[Unit]) extends Memoize[F, E, A]
final case class Finished[F[_], E, A](result: Either[E, F[A]]) extends Memoize[F, E, A]
}
implicit def genConcurrentForOptionT[F[_], E](
implicit F0: GenConcurrent[F, E]): GenConcurrent[OptionT[F, *], E] =
F0 match {
case async: Async[F @unchecked] =>
Async.asyncForOptionT[F](async)
case temporal: GenTemporal[F @unchecked, E @unchecked] =>
GenTemporal.instantiateGenTemporalForOptionT[F, E](temporal)
case concurrent =>
instantiateGenConcurrentForOptionT(concurrent)
}
private[kernel] def instantiateGenConcurrentForOptionT[F[_], E](
F0: GenConcurrent[F, E]): OptionTGenConcurrent[F, E] =
new OptionTGenConcurrent[F, E] {
override implicit protected def F: GenConcurrent[F, E] = F0
}
implicit def genConcurrentForEitherT[F[_], E0, E](
implicit F0: GenConcurrent[F, E]): GenConcurrent[EitherT[F, E0, *], E] =
F0 match {
case async: Async[F @unchecked] =>
Async.asyncForEitherT[F, E0](async)
case temporal: GenTemporal[F @unchecked, E @unchecked] =>
GenTemporal.instantiateGenTemporalForEitherT[F, E0, E](temporal)
case concurrent =>
instantiateGenConcurrentForEitherT(concurrent)
}
private[kernel] def instantiateGenConcurrentForEitherT[F[_], E0, E](
F0: GenConcurrent[F, E]): EitherTGenConcurrent[F, E0, E] =
new EitherTGenConcurrent[F, E0, E] {
override implicit protected def F: GenConcurrent[F, E] = F0
}
implicit def genConcurrentForKleisli[F[_], R, E](
implicit F0: GenConcurrent[F, E]): GenConcurrent[Kleisli[F, R, *], E] =
F0 match {
case async: Async[F @unchecked] =>
Async.asyncForKleisli[F, R](async)
case temporal: GenTemporal[F @unchecked, E @unchecked] =>
GenTemporal.instantiateGenTemporalForKleisli[F, R, E](temporal)
case concurrent =>
instantiateGenConcurrentForKleisli(concurrent)
}
private[kernel] def instantiateGenConcurrentForKleisli[F[_], R, E](
F0: GenConcurrent[F, E]): KleisliGenConcurrent[F, R, E] =
new KleisliGenConcurrent[F, R, E] {
override implicit protected def F: GenConcurrent[F, E] = F0
}
implicit def genConcurrentForIorT[F[_], L, E](
implicit F0: GenConcurrent[F, E],
L0: Semigroup[L]): GenConcurrent[IorT[F, L, *], E] =
F0 match {
case async: Async[F @unchecked] =>
Async.asyncForIorT[F, L](async, L0)
case temporal: GenTemporal[F @unchecked, E @unchecked] =>
GenTemporal.instantiateGenTemporalForIorT[F, L, E](temporal)
case concurrent =>
instantiateGenConcurrentForIorT(concurrent)
}
private[kernel] def instantiateGenConcurrentForIorT[F[_], L, E](F0: GenConcurrent[F, E])(
implicit L0: Semigroup[L]): IorTGenConcurrent[F, L, E] =
new IorTGenConcurrent[F, L, E] {
override implicit protected def F: GenConcurrent[F, E] = F0
override implicit protected def L: Semigroup[L] = L0
}
implicit def genConcurrentForWriterT[F[_], L, E](
implicit F0: GenConcurrent[F, E],
L0: Monoid[L]): GenConcurrent[WriterT[F, L, *], E] =
F0 match {
case async: Async[F @unchecked] =>
Async.asyncForWriterT[F, L](async, L0)
case temporal: GenTemporal[F @unchecked, E @unchecked] =>
GenTemporal.instantiateGenTemporalForWriterT[F, L, E](temporal)
case concurrent =>
instantiateGenConcurrentForWriterT(concurrent)
}
private[kernel] def instantiateGenConcurrentForWriterT[F[_], L, E](F0: GenConcurrent[F, E])(
implicit L0: Monoid[L]): WriterTGenConcurrent[F, L, E] =
new WriterTGenConcurrent[F, L, E] {
override implicit protected def F: GenConcurrent[F, E] = F0
override implicit protected def L: Monoid[L] = L0
}
private[kernel] trait OptionTGenConcurrent[F[_], E]
extends GenConcurrent[OptionT[F, *], E]
with GenSpawn.OptionTGenSpawn[F, E] {
implicit protected def F: GenConcurrent[F, E]
override def ref[A](a: A): OptionT[F, Ref[OptionT[F, *], A]] =
OptionT.liftF(F.map(F.ref(a))(_.mapK(OptionT.liftK)))
override def deferred[A]: OptionT[F, Deferred[OptionT[F, *], A]] =
OptionT.liftF(F.map(F.deferred[A])(_.mapK(OptionT.liftK)))
override def racePair[A, B](fa: OptionT[F, A], fb: OptionT[F, B]): OptionT[
F,
Either[
(Outcome[OptionT[F, *], E, A], Fiber[OptionT[F, *], E, B]),
(Fiber[OptionT[F, *], E, A], Outcome[OptionT[F, *], E, B])]] =
super.racePair(fa, fb)
}
private[kernel] trait EitherTGenConcurrent[F[_], E0, E]
extends GenConcurrent[EitherT[F, E0, *], E]
with GenSpawn.EitherTGenSpawn[F, E0, E] {
implicit protected def F: GenConcurrent[F, E]
override def ref[A](a: A): EitherT[F, E0, Ref[EitherT[F, E0, *], A]] =
EitherT.liftF(F.map(F.ref(a))(_.mapK(EitherT.liftK)))
override def deferred[A]: EitherT[F, E0, Deferred[EitherT[F, E0, *], A]] =
EitherT.liftF(F.map(F.deferred[A])(_.mapK(EitherT.liftK)))
override def racePair[A, B](fa: EitherT[F, E0, A], fb: EitherT[F, E0, B]): EitherT[
F,
E0,
Either[
(Outcome[EitherT[F, E0, *], E, A], Fiber[EitherT[F, E0, *], E, B]),
(Fiber[EitherT[F, E0, *], E, A], Outcome[EitherT[F, E0, *], E, B])]] =
super.racePair(fa, fb)
}
private[kernel] trait KleisliGenConcurrent[F[_], R, E]
extends GenConcurrent[Kleisli[F, R, *], E]
with GenSpawn.KleisliGenSpawn[F, R, E] {
implicit protected def F: GenConcurrent[F, E]
override def ref[A](a: A): Kleisli[F, R, Ref[Kleisli[F, R, *], A]] =
Kleisli.liftF(F.map(F.ref(a))(_.mapK(Kleisli.liftK)))
override def deferred[A]: Kleisli[F, R, Deferred[Kleisli[F, R, *], A]] =
Kleisli.liftF(F.map(F.deferred[A])(_.mapK(Kleisli.liftK)))
override def racePair[A, B](fa: Kleisli[F, R, A], fb: Kleisli[F, R, B]): Kleisli[
F,
R,
Either[
(Outcome[Kleisli[F, R, *], E, A], Fiber[Kleisli[F, R, *], E, B]),
(Fiber[Kleisli[F, R, *], E, A], Outcome[Kleisli[F, R, *], E, B])]] =
super.racePair(fa, fb)
}
private[kernel] trait IorTGenConcurrent[F[_], L, E]
extends GenConcurrent[IorT[F, L, *], E]
with GenSpawn.IorTGenSpawn[F, L, E] {
implicit protected def F: GenConcurrent[F, E]
implicit protected def L: Semigroup[L]
override def ref[A](a: A): IorT[F, L, Ref[IorT[F, L, *], A]] =
IorT.liftF(F.map(F.ref(a))(_.mapK(IorT.liftK)))
override def deferred[A]: IorT[F, L, Deferred[IorT[F, L, *], A]] =
IorT.liftF(F.map(F.deferred[A])(_.mapK(IorT.liftK)))
override def racePair[A, B](fa: IorT[F, L, A], fb: IorT[F, L, B]): IorT[
F,
L,
Either[
(Outcome[IorT[F, L, *], E, A], Fiber[IorT[F, L, *], E, B]),
(Fiber[IorT[F, L, *], E, A], Outcome[IorT[F, L, *], E, B])]] =
super.racePair(fa, fb)
}
private[kernel] trait WriterTGenConcurrent[F[_], L, E]
extends GenConcurrent[WriterT[F, L, *], E]
with GenSpawn.WriterTGenSpawn[F, L, E] {
implicit protected def F: GenConcurrent[F, E]
implicit protected def L: Monoid[L]
override def ref[A](a: A): WriterT[F, L, Ref[WriterT[F, L, *], A]] =
WriterT.liftF(F.map(F.ref(a))(_.mapK(WriterT.liftK)))
override def deferred[A]: WriterT[F, L, Deferred[WriterT[F, L, *], A]] =
WriterT.liftF(F.map(F.deferred[A])(_.mapK(WriterT.liftK)))
override def racePair[A, B](fa: WriterT[F, L, A], fb: WriterT[F, L, B]): WriterT[
F,
L,
Either[
(Outcome[WriterT[F, L, *], E, A], Fiber[WriterT[F, L, *], E, B]),
(Fiber[WriterT[F, L, *], E, A], Outcome[WriterT[F, L, *], E, B])]] =
super.racePair(fa, fb)
}
}