Skip to content

Commit

Permalink
Fallback IOLocal#unsafeThreadLocal by a true ThreadLocal
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge committed Dec 13, 2024
1 parent 9ce05f2 commit a01fba0
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions core/jvm/src/main/scala/cats/effect/IOLocalPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,41 @@ import IOFiberConstants.ioLocalPropagation
private[effect] trait IOLocalPlatform[A] { self: IOLocal[A] =>

/**
* Returns a [[java.lang.ThreadLocal]] view of this [[IOLocal]] that allows to unsafely get,
* set, and remove (aka reset) the value in the currently running fiber. The system property
* `cats.effect.ioLocalPropagation` must be `true`, otherwise throws an
* Creates a [[java.lang.ThreadLocal]] based on this [[IOLocal]]. This allows to unsafely get,
* set, and remove (aka reset) the [[IOLocal]]'s value in the currently running fiber. When
* used outside of a fiber, behaves like an ordinary `ThreadLocal`.
*
* The system property `cats.effect.ioLocalPropagation` must be `true`, otherwise throws an
* [[java.lang.UnsupportedOperationException]].
*/
def unsafeThreadLocal(): ThreadLocal[A] = if (ioLocalPropagation)
new ThreadLocal[A] {
override def initialValue(): A = self.getOrDefault(IOLocalState.empty)

override def get(): A = {
val fiber = IOFiber.currentIOFiber()
val state = if (fiber ne null) fiber.getLocalState() else IOLocalState.empty
self.getOrDefault(state)
if (fiber ne null) {
self.getOrDefault(fiber.getLocalState())
} else {
super.get()
}
}

override def set(value: A): Unit = {
val fiber = IOFiber.currentIOFiber()
if (fiber ne null) {
fiber.setLocalState(self.set(fiber.getLocalState(), value))
} else {
super.set(value)
}
}

override def remove(): Unit = {
val fiber = IOFiber.currentIOFiber()
if (fiber ne null) {
fiber.setLocalState(self.reset(fiber.getLocalState()))
} else {
super.remove()
}
}
}
Expand Down

0 comments on commit a01fba0

Please sign in to comment.