Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class CountdownLatch private (_count: Ref[Int], _waiters: Promise[Nothing,
*/
val countDown: UIO[Unit] = _count.modify {
case 0 => ZIO.unit -> 0
case 1 => _waiters.succeed(()) -> 0
case 1 => _waiters.succeedUnit -> 0
case n => ZIO.unit -> (n - 1)
}.flatten.unit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class CyclicBarrier private (
_lock.get.flatMap(_.fail(()).unit)

private val succeed: UIO[Unit] =
_lock.get.flatMap(_.succeed(()).unit)
_lock.get.flatMap(_.succeedUnit.unit)

/** The number of parties required to trip this barrier. */
def parties: Int = _parties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ final class ReentrantLock private (fairness: Boolean, state: Ref[ReentrantLock.S
ZIO.unit -> State(epoch + 1, None, 0, Map.empty)
else {
val (fiberId, (_, promise)) = if (fairness) holders.minBy(_._2._1) else pickRandom(holders)
promise.succeed(()).unit -> State(epoch + 1, Some(fiberId), 1, holders - fiberId)
promise.succeedUnit.unit -> State(epoch + 1, Some(fiberId), 1, holders - fiberId)
}

private def pickRandom(
Expand Down
8 changes: 3 additions & 5 deletions core/shared/src/main/scala/zio/Hub.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,9 @@ object Hub {
ZIO.fiberIdWith { fiberId =>
shutdownFlag.set(true)
ZIO
.whenZIO(shutdownHook.succeed(())) {
.whenZIODiscard(shutdownHook.succeedUnit) {
scope.close(Exit.interrupt(fiberId)) *> strategy.shutdown
}
.unit
}.uninterruptible
def size(implicit trace: Trace): UIO[Int] =
ZIO.suspendSucceed {
Expand Down Expand Up @@ -229,15 +228,14 @@ object Hub {
ZIO.fiberIdWith { fiberId =>
shutdownFlag.set(true)
ZIO
.whenZIO(shutdownHook.succeed(())) {
ZIO.foreachPar(unsafePollAll(pollers))(_.interruptAs(fiberId)) *>
.whenZIODiscard(shutdownHook.succeedUnit) {
ZIO.foreachParDiscard(unsafePollAll(pollers))(_.interruptAs(fiberId)) *>
ZIO.succeed {
subscribers.remove(subscription -> pollers)
subscription.unsubscribe()
strategy.unsafeOnHubEmptySpace(hub, subscribers)
}
}
.unit
}.uninterruptible
def size(implicit trace: Trace): UIO[Int] =
ZIO.suspendSucceed {
Expand Down
13 changes: 13 additions & 0 deletions core/shared/src/main/scala/zio/Promise.scala
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ final class Promise[E, A] private (
def succeed(a: A)(implicit trace: Trace): UIO[Boolean] =
ZIO.succeed(unsafe.succeed(a)(trace, Unsafe.unsafe))

/**
* Internally, you can use this method instead of calling
* `myPromise.succeed(())`
*
* It avoids the `Exit` allocation
*/
private[zio] def succeedUnit(implicit ev0: A =:= Unit, trace: Trace): UIO[Boolean] =
ZIO.succeed(unsafe.succeedUnit(ev0, trace, Unsafe))

private def interruptJoiner(joiner: IO[E, A] => Any)(implicit trace: Trace): UIO[Any] = ZIO.succeed {
var retry = true

Expand Down Expand Up @@ -205,6 +214,7 @@ final class Promise[E, A] private (
def poll(implicit unsafe: Unsafe): Option[IO[E, A]]
def refailCause(e: Cause[E])(implicit trace: Trace, unsafe: Unsafe): Boolean
def succeed(a: A)(implicit trace: Trace, unsafe: Unsafe): Boolean
def succeedUnit(implicit ev0: A =:= Unit, trace: Trace, unsafe: Unsafe): Boolean
}

private[zio] val unsafe: UnsafeAPI =
Expand Down Expand Up @@ -280,6 +290,9 @@ final class Promise[E, A] private (

def succeed(a: A)(implicit trace: Trace, unsafe: Unsafe): Boolean =
completeWith(Exit.succeed(a))

override def succeedUnit(implicit ev0: A =:= Unit, trace: Trace, unsafe: Unsafe): Boolean =
completeWith(Exit.unit.asInstanceOf[IO[E, A]])
}

}
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/zio/Semaphore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ object Semaphore {
case None => acc -> Right(n)
case Some(((promise, permits), queue)) =>
if (n > permits)
loop(n - permits, Left(queue), acc *> promise.succeed(()))
loop(n - permits, Left(queue), acc *> promise.succeedUnit)
else if (n == permits)
(acc *> promise.succeed(())) -> Left(queue)
(acc *> promise.succeedUnit) -> Left(queue)
else
acc -> Left((promise -> (permits - n)) +: queue)
}
Expand Down
22 changes: 11 additions & 11 deletions streams/shared/src/main/scala/zio/stream/ZChannel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -689,11 +689,11 @@ sealed trait ZChannel[-Env, -InErr, -InElem, -InDone, +OutErr, +OutElem, +OutDon

permits
.withPermit(
latch.succeed(()) *>
latch.succeedUnit *>
f(outElem)
.catchAllCause(cause =>
failureRef.update(_ && cause).unless(cause.isInterruptedOnly) *>
errorSignal.succeed(()) *>
errorSignal.succeedUnit *>
ZChannel.failLeftUnit
)
)
Expand Down Expand Up @@ -770,11 +770,11 @@ sealed trait ZChannel[-Env, -InErr, -InElem, -InDone, +OutErr, +OutElem, +OutDon
for {
_ <- permits
.withPermit(
latch.succeed(()) *> f(outElem)
latch.succeedUnit *> f(outElem)
.foldCauseZIO(
cause =>
failure.update(_ && cause).unless(cause.isInterruptedOnly) *>
errorSignal.succeed(()) *>
errorSignal.succeedUnit *>
outgoing.offer(ZChannel.failLeftUnit),
elem => outgoing.offer(Exit.succeed(elem))
)
Expand Down Expand Up @@ -1248,8 +1248,8 @@ sealed trait ZChannel[-Env, -InErr, -InElem, -InDone, +OutErr, +OutElem, +OutDon
fiber <- restore(run(channelPromise, scopePromise, child)).forkDaemon
_ <- parent.addFinalizer {
channelPromise.isDone.flatMap { isDone =>
if (isDone) scopePromise.succeed(()) *> fiber.await *> fiber.inheritAll
else scopePromise.succeed(()) *> fiber.interrupt *> fiber.inheritAll
if (isDone) scopePromise.succeedUnit *> fiber.await *> fiber.inheritAll
else scopePromise.succeedUnit *> fiber.interrupt *> fiber.inheritAll
}
}
done <- restore(channelPromise.await)
Expand Down Expand Up @@ -1986,9 +1986,9 @@ object ZChannel {
}
}
case Left(l: Left[OutErr, OutDone]) =>
outgoing.offer(Result.Error(l.value)) *> errorSignal.succeed(()).unit
outgoing.offer(Result.Error(l.value)) *> errorSignal.succeedUnit.unit
case Right(cause) =>
outgoing.offer(Result.Fatal(cause)) *> errorSignal.succeed(()).unit
outgoing.offer(Result.Fatal(cause)) *> errorSignal.succeedUnit.unit
}
)

Expand All @@ -2005,7 +2005,7 @@ object ZChannel {
}

permits
.withPermit(latch.succeed(()) *> raceIOs)
.withPermit(latch.succeedUnit *> raceIOs)
.interruptible
.forkIn(childScope) *> latch.await
}
Expand All @@ -2017,7 +2017,7 @@ object ZChannel {

for {
size <- cancelers.size
_ <- ZIO.when(size >= n0)(cancelers.take.flatMap(_.succeed(())))
_ <- ZIO.when(size >= n0)(cancelers.take.flatMap(_.succeedUnit))
_ <- cancelers.offer(canceler)
raceIOs =
ZIO.scopedWith { scope =>
Expand All @@ -2026,7 +2026,7 @@ object ZChannel {
.flatMap(evaluatePull(_).race(canceler.await.interruptible))
}
_ <- permits
.withPermit(latch.succeed(()) *> raceIOs)
.withPermit(latch.succeedUnit *> raceIOs)
.interruptible
.forkIn(childScope)
_ <- latch.await
Expand Down
12 changes: 6 additions & 6 deletions streams/shared/src/main/scala/zio/stream/ZStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ final class ZStream[-R, +E, +A] private (val channel: ZChannel[R, Any, Any, Any,
): ZChannel[R1, Any, Any, Any, E1, Chunk[A1], Unit] = {
lazy val process: ZChannel[Any, Any, Any, Any, E1, Chunk[A1], Unit] =
ZChannel.fromZIO(queue.take).flatMap { case (take, promise) =>
ZChannel.fromZIO(promise.succeed(())) *>
ZChannel.fromZIO(promise.succeedUnit) *>
take.fold(
ZChannel.unit,
error => ZChannel.refailCause(error),
Expand All @@ -549,7 +549,7 @@ final class ZStream[-R, +E, +A] private (val channel: ZChannel[R, Any, Any, Any,
for {
queue <- scoped
start <- Promise.make[Nothing, Unit]
_ <- start.succeed(())
_ <- start.succeedUnit
ref <- Ref.make(start)
_ <- (channel >>> producer(queue, ref)).runScoped.forkScoped
} yield consumer(queue)
Expand Down Expand Up @@ -3320,7 +3320,7 @@ final class ZStream[-R, +E, +A] private (val channel: ZChannel[R, Any, Any, Any,
.pipeTo(loop)
.ensuring(queue.offer(Take.end).forkDaemon *> queue.awaitShutdown) *> ZChannel.unit
)
.merge(ZStream.execute((promise.succeed(()) *> right.run(sink)).ensuring(queue.shutdown)), HaltStrategy.Both)
.merge(ZStream.execute((promise.succeedUnit *> right.run(sink)).ensuring(queue.shutdown)), HaltStrategy.Both)
}

/**
Expand Down Expand Up @@ -5656,22 +5656,22 @@ object ZStream extends ZStreamPlatformSpecificConstructors {
Promise.make[Nothing, Unit].flatMap { p =>
ref.modify {
case s @ Handoff.State.Full(_, notifyProducer) => (notifyProducer.await *> offer(a), s)
case Handoff.State.Empty(notifyConsumer) => (notifyConsumer.succeed(()) *> p.await, Handoff.State.Full(a, p))
case Handoff.State.Empty(notifyConsumer) => (notifyConsumer.succeedUnit *> p.await, Handoff.State.Full(a, p))
}.flatten
}

def take(implicit trace: Trace): UIO[A] =
Promise.make[Nothing, Unit].flatMap { p =>
ref.modify {
case Handoff.State.Full(a, notifyProducer) => (notifyProducer.succeed(()).as(a), Handoff.State.Empty(p))
case Handoff.State.Full(a, notifyProducer) => (notifyProducer.succeedUnit.as(a), Handoff.State.Empty(p))
case s @ Handoff.State.Empty(notifyConsumer) => (notifyConsumer.await *> take, s)
}.flatten
}

def poll(implicit trace: Trace): UIO[Option[A]] =
Promise.make[Nothing, Unit].flatMap { p =>
ref.modify {
case Handoff.State.Full(a, notifyProducer) => (notifyProducer.succeed(()).as(Some(a)), Handoff.State.Empty(p))
case Handoff.State.Full(a, notifyProducer) => (notifyProducer.succeedUnit.as(Some(a)), Handoff.State.Empty(p))
case s @ Handoff.State.Empty(_) => (ZIO.succeed(None), s)
}.flatten
}
Expand Down
Loading