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
4 changes: 4 additions & 0 deletions core-tests/shared/src/test/scala/zio/ZIOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2268,6 +2268,10 @@ object ZIOSpec extends ZIOBaseSpec {
val io = IO.effectTotal(42).race(IO.never)
assertM(io)(equalTo(42))
},
testM("race in uninterruptible region") {
val effect = (ZIO.unit.race(ZIO.infinity)).uninterruptible
assertM(effect)(isUnit)
},
testM("firstSuccessOf of values") {
val io = IO.firstSuccessOf(IO.fail(0), List(IO.succeedNow(100))).either
assertM(io)(isRight(equalTo(100)))
Expand Down
41 changes: 26 additions & 15 deletions core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1150,22 +1150,23 @@ sealed trait ZIO[-R, +E, +A] extends Serializable with ZIOPlatformSpecific[R, E,
* in the background.
*/
final def race[R1 <: R, E1 >: E, A1 >: A](that: ZIO[R1, E1, A1]): ZIO[R1, E1, A1] =
ZIO.fiberId
.flatMap(parentFiberId =>
(self raceWith that)(
(exit, right) =>
exit.foldM[Any, E1, A1](
cause => right.join mapErrorCause (cause && _),
a => (right interruptAs parentFiberId) as a
),
(exit, left) =>
exit.foldM[Any, E1, A1](
cause => left.join mapErrorCause (_ && cause),
a => (left interruptAs parentFiberId) as a
)
)
ZIO.descriptorWith { descriptor =>
val parentFiberId = descriptor.id
def maybeDisconnect[R, E, A](zio: ZIO[R, E, A]): ZIO[R, E, A] =
ZIO.uninterruptibleMask(interruptible => interruptible.force(zio))
(maybeDisconnect(self) raceWith maybeDisconnect(that))(
(exit, right) =>
exit.foldM[Any, E1, A1](
cause => right.join mapErrorCause (cause && _),
a => (right interruptAs parentFiberId) as a
),
(exit, left) =>
exit.foldM[Any, E1, A1](
cause => left.join mapErrorCause (_ && cause),
a => (left interruptAs parentFiberId) as a
)
)
.refailWithTrace
}.refailWithTrace

/**
* Returns an effect that races this effect with all the specified effects,
Expand Down Expand Up @@ -3289,6 +3290,16 @@ object ZIO extends ZIOCompanionPlatformSpecific {
final class InterruptStatusRestore(private val flag: zio.InterruptStatus) extends AnyVal {
def apply[R, E, A](zio: ZIO[R, E, A]): ZIO[R, E, A] =
zio.interruptStatus(flag)

/**
* Returns a new effect that, if the parent region is uninterruptible, can
* be interrupted in the background instantaneously. If the parent region
* is interruptible, then the effect can be interrupted normally, in the
* foreground.
*/
def force[R, E, A](zio: ZIO[R, E, A]): ZIO[R, E, A] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def force[R, E, A](zio: ZIO[R, E, A]): ZIO[R, E, A] =
/**
* Returns a new effect that, if the parent region is uninterruptible, can be interrupted in the background
* instantaneously. If the parent region is interruptible, then the effect can be interrupted normally,
* in the foreground.
*/
def force[R, E, A](zio: ZIO[R, E, A]): ZIO[R, E, A] =

if (flag == InterruptStatus.Uninterruptible) zio.uninterruptible.disconnect.interruptible
else zio.interruptStatus(flag)
}

final class IfM[R, E](private val b: ZIO[R, E, Boolean]) extends AnyVal {
Expand Down