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: 2 additions & 2 deletions core/shared/src/main/scala/zio/FiberRef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,8 @@ object FiberRef {
private[zio] val forkScopeOverride: FiberRef[Option[FiberScope]] =
FiberRef.unsafe.make[Option[FiberScope]](None, _ => None, (parent, _) => parent)(Unsafe.unsafe)

private[zio] val overrideExecutor: FiberRef[Option[Executor]] =
FiberRef.unsafe.make[Option[Executor]](None)(Unsafe.unsafe)
private[zio] val overrideExecutor: FiberRef[Either[Executor, Executor]] =
FiberRef.unsafe.make[Either[Executor, Executor]](Left(Runtime.defaultExecutor))(Unsafe.unsafe)

private[zio] val currentEnvironment: FiberRef.WithPatch[ZEnvironment[Any], ZEnvironment.Patch[Any, Any]] =
FiberRef.unsafe.makeEnvironment(ZEnvironment.empty)(Unsafe.unsafe)
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/Runtime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ object Runtime extends RuntimePlatformSpecific {
ZLayer.scoped(ZIO.withConfigProviderScoped(configProvider))

def setExecutor(executor: Executor)(implicit trace: Trace): ZLayer[Any, Nothing, Unit] =
ZLayer.scoped(FiberRef.overrideExecutor.locallyScoped(Some(executor)))
ZLayer.scoped(FiberRef.overrideExecutor.locallyScoped(Right(executor)))

def setUnhandledErrorLogLevel(logLevel: LogLevel)(implicit trace: Trace): ZLayer[Any, Nothing, Unit] =
ZLayer.scoped(FiberRef.unhandledErrorLogLevel.locallyScoped(Some(logLevel)))
Expand Down
52 changes: 42 additions & 10 deletions core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3075,7 +3075,7 @@ object ZIO extends ZIOCompanionPlatformSpecific with ZIOCompanionVersionSpecific
status,
fiberState.getFiberRef(FiberRef.interruptedCause)(Unsafe.unsafe).interruptors,
fiberState.getCurrentExecutor()(Unsafe.unsafe),
fiberState.getFiberRef(FiberRef.overrideExecutor)(Unsafe.unsafe).isDefined
fiberState.getFiberRef(FiberRef.overrideExecutor)(Unsafe.unsafe).isRight
)

f(descriptor)
Expand Down Expand Up @@ -4174,12 +4174,17 @@ object ZIO extends ZIOCompanionPlatformSpecific with ZIOCompanionVersionSpecific
* provided executor, before potentially returning to the previous executor.
* See [[ZIO!.onExecutor]].
*/
def onExecutor[R, E, A](newExecutor: => Executor)(zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
def onExecutor[R, E, A](executor: => Executor)(zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
ZIO.descriptorWith { descriptor =>
val oldExecutor = descriptor.executor

if (descriptor.isLocked) ZIO.acquireReleaseWith(ZIO.shift(newExecutor))(_ => ZIO.shift(oldExecutor))(_ => zio)
else ZIO.acquireReleaseWith(ZIO.shift(newExecutor))(_ => ZIO.unshift)(_ => zio)
val newExecutor = executor

if (descriptor.isLocked && oldExecutor == newExecutor)
zio
else if (descriptor.isLocked)
ZIO.acquireReleaseWith(ZIO.shift(newExecutor))(_ => ZIO.shift(oldExecutor))(_ => zio)
else
ZIO.acquireReleaseWith(ZIO.shift(newExecutor))(_ => ZIO.unshift)(_ => zio)
}

/**
Expand All @@ -4188,8 +4193,15 @@ object ZIO extends ZIOCompanionPlatformSpecific with ZIOCompanionVersionSpecific
*/
def onExecutorScoped(executor: => Executor)(implicit trace: Trace): ZIO[Scope, Nothing, Unit] =
ZIO.descriptorWith { descriptor =>
if (descriptor.isLocked) ZIO.acquireRelease(ZIO.shift(executor))(_ => ZIO.shift(descriptor.executor))
else ZIO.acquireRelease(ZIO.shift(executor))(_ => ZIO.unshift)
val oldExecutor = descriptor.executor
val newExecutor = executor

if (descriptor.isLocked && oldExecutor == newExecutor)
ZIO.unit
else if (descriptor.isLocked)
ZIO.acquireRelease(ZIO.shift(executor))(_ => ZIO.shift(descriptor.executor))
else
ZIO.acquireRelease(ZIO.shift(executor))(_ => ZIO.unshift)
}

/**
Expand Down Expand Up @@ -4471,7 +4483,21 @@ object ZIO extends ZIOCompanionPlatformSpecific with ZIOCompanionVersionSpecific
* as [[ZIO!.onExecutor]] and [[ZIO!.onExecutionContext]].
*/
def shift(executor: => Executor)(implicit trace: Trace): UIO[Unit] =
(FiberRef.overrideExecutor.set(Some(executor)) *> ZIO.yieldNow)
ZIO.withFiberRuntime[Any, Nothing, Unit] { (fiberRuntime, _) =>
val newExecutor = executor
fiberRuntime.getFiberRef(FiberRef.overrideExecutor)(Unsafe.unsafe) match {
case Left(oldExecutor) =>
fiberRuntime.setFiberRef(FiberRef.overrideExecutor, Right(newExecutor))(Unsafe.unsafe)
if (oldExecutor == newExecutor) ZIO.unit
else ZIO.yieldNow
case Right(oldExecutor) =>
if (oldExecutor == newExecutor) ZIO.unit
else {
fiberRuntime.setFiberRef(FiberRef.overrideExecutor, Right(newExecutor))(Unsafe.unsafe)
ZIO.yieldNow
}
}
}

/**
* Returns an effect that suspends for the specified duration. This method is
Expand Down Expand Up @@ -4606,7 +4632,7 @@ object ZIO extends ZIOCompanionPlatformSpecific with ZIOCompanionVersionSpecific
* An effect that succeeds with a unit value.
*/
lazy val unit: UIO[Unit] =
succeedNow(())
succeed(())(Trace.empty)

/**
* Prefix form of `ZIO#uninterruptible`.
Expand Down Expand Up @@ -4658,7 +4684,13 @@ object ZIO extends ZIOCompanionPlatformSpecific with ZIOCompanionVersionSpecific
* after completing an effect on another executor.
*/
def unshift(implicit trace: Trace): UIO[Unit] =
FiberRef.overrideExecutor.set(None)
ZIO.withFiberRuntime[Any, Nothing, Unit] { (fiberRuntime, _) =>
fiberRuntime.getFiberRef(FiberRef.overrideExecutor)(Unsafe.unsafe) match {
case Left(executor) => ()
case Right(executor) => fiberRuntime.setFiberRef(FiberRef.overrideExecutor, Left(executor))(Unsafe.unsafe)
}
ZIO.unit
}

/**
* Updates the `FiberRef` values for the fiber running this effect using the
Expand Down
5 changes: 1 addition & 4 deletions core/shared/src/main/scala/zio/internal/FiberRuntime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,7 @@ final class FiberRuntime[E, A](fiberId: FiberId.Runtime, fiberRefs0: FiberRefs,
* log annotations and log level) may not be up-to-date.
*/
private[zio] def getCurrentExecutor()(implicit unsafe: Unsafe): Executor =
getFiberRef(FiberRef.overrideExecutor) match {
case None => Runtime.defaultExecutor
case Some(value) => value
}
getFiberRef(FiberRef.overrideExecutor).merge

/**
* Retrieves the state of the fiber ref, or else its initial value.
Expand Down