-
Notifications
You must be signed in to change notification settings - Fork 551
blocking
and interruptible
on Scala Native
#4378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
djspiewak
wants to merge
4
commits into
typelevel:series/3.x
Choose a base branch
from
djspiewak:feature/sn-blocking
base: series/3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3b838ec
Shifted several `IO` companion functions to share with Native
djspiewak deb3eed
First cut at porting `interruptible` to SN
djspiewak 8523d8a
Fixed up `interruptible` on JVM/SN for special NIO exception
djspiewak 4b5e595
Remove duplicated test
armanbilge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
138 changes: 138 additions & 0 deletions
138
core/jvm-native/src/main/scala/cats/effect/IOCompanionMultithreadedPlatform.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright 2020-2025 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 | ||
|
||
import cats.effect.std.Console | ||
import cats.effect.tracing.Tracing | ||
|
||
import java.time.{Instant, ZonedDateTime} | ||
|
||
private[effect] abstract class IOCompanionMultithreadedPlatform { this: IO.type => | ||
private[this] val TypeDelay = Sync.Type.Delay | ||
private[this] val TypeBlocking = Sync.Type.Blocking | ||
private[this] val TypeInterruptibleOnce = Sync.Type.InterruptibleOnce | ||
private[this] val TypeInterruptibleMany = Sync.Type.InterruptibleMany | ||
|
||
/** | ||
* Intended for thread blocking operations. `blocking` will shift the execution of the | ||
* blocking operation to a separate threadpool to avoid blocking on the main execution | ||
* context. See the thread-model documentation for more information on why this is necessary. | ||
* Note that the created effect will be uncancelable; if you need cancelation then you should | ||
* use [[interruptible[A](thunk:=>A):*]] or [[interruptibleMany]]. | ||
* | ||
* {{{ | ||
* IO.blocking(scala.io.Source.fromFile("path").mkString) | ||
* }}} | ||
* | ||
* @param thunk | ||
* The side effect which is to be suspended in `IO` and evaluated on a blocking execution | ||
* context | ||
* | ||
* Implements [[cats.effect.kernel.Sync.blocking]]. | ||
*/ | ||
def blocking[A](thunk: => A): IO[A] = { | ||
val fn = () => thunk | ||
Blocking(TypeBlocking, fn, Tracing.calculateTracingEvent(fn.getClass)) | ||
} | ||
|
||
// this cannot be marked private[effect] because of static forwarders in Java | ||
@deprecated("use interruptible / interruptibleMany instead", "3.3.0") | ||
def interruptible[A](many: Boolean, thunk: => A): IO[A] = { | ||
val fn = () => thunk | ||
Blocking( | ||
if (many) TypeInterruptibleMany else TypeInterruptibleOnce, | ||
fn, | ||
Tracing.calculateTracingEvent(fn.getClass)) | ||
} | ||
|
||
/** | ||
* Like [[blocking]] but will attempt to abort the blocking operation using thread interrupts | ||
* in the event of cancelation. The interrupt will be attempted only once. | ||
* | ||
* Note the following tradeoffs: | ||
* - this has slightly more overhead than [[blocking]] due to the machinery necessary for | ||
* the interrupt coordination, | ||
* - thread interrupts are very often poorly considered by Java (and Scala!) library | ||
* authors, and it is possible for interrupts to result in resource leaks or invalid | ||
* states. It is important to be certain that this will not be the case before using this | ||
* mechanism. | ||
* | ||
* @param thunk | ||
* The side effect which is to be suspended in `IO` and evaluated on a blocking execution | ||
* context | ||
* | ||
* Implements [[cats.effect.kernel.Sync.interruptible[A](thunk:=>A):*]] | ||
*/ | ||
def interruptible[A](thunk: => A): IO[A] = { | ||
val fn = () => thunk | ||
Blocking(TypeInterruptibleOnce, fn, Tracing.calculateTracingEvent(fn.getClass)) | ||
} | ||
|
||
/** | ||
* Like [[blocking]] but will attempt to abort the blocking operation using thread interrupts | ||
* in the event of cancelation. The interrupt will be attempted repeatedly until the blocking | ||
* operation completes or exits. | ||
* | ||
* Note the following tradeoffs: | ||
* - this has slightly more overhead than [[blocking]] due to the machinery necessary for | ||
* the interrupt coordination, | ||
* - thread interrupts are very often poorly considered by Java (and Scala!) library | ||
* authors, and it is possible for interrupts to result in resource leaks or invalid | ||
* states. It is important to be certain that this will not be the case before using this | ||
* mechanism. | ||
* | ||
* @param thunk | ||
* The side effect which is to be suspended in `IO` and evaluated on a blocking execution | ||
* context | ||
* | ||
* Implements [[cats.effect.kernel.Sync!.interruptibleMany]] | ||
*/ | ||
def interruptibleMany[A](thunk: => A): IO[A] = { | ||
val fn = () => thunk | ||
Blocking(TypeInterruptibleMany, fn, Tracing.calculateTracingEvent(fn.getClass)) | ||
} | ||
|
||
def suspend[A](hint: Sync.Type)(thunk: => A): IO[A] = | ||
if (hint eq TypeDelay) | ||
apply(thunk) | ||
else { | ||
val fn = () => thunk | ||
Blocking(hint, fn, Tracing.calculateTracingEvent(fn.getClass)) | ||
} | ||
|
||
def realTimeInstant: IO[Instant] = asyncForIO.realTimeInstant | ||
|
||
def realTimeZonedDateTime: IO[ZonedDateTime] = asyncForIO.realTimeZonedDateTime | ||
|
||
/** | ||
* Reads a line as a string from the standard input using the platform's default charset, as | ||
* per `java.nio.charset.Charset.defaultCharset()`. | ||
* | ||
* The effect can raise a `java.io.EOFException` if no input has been consumed before the EOF | ||
* is observed. This should never happen with the standard input, unless it has been replaced | ||
* with a finite `java.io.InputStream` through `java.lang.System#setIn` or similar. | ||
* | ||
* @see | ||
* `cats.effect.std.Console#readLineWithCharset` for reading using a custom | ||
* `java.nio.charset.Charset` | ||
* | ||
* @return | ||
* an IO effect that describes reading the user's input from the standard input as a string | ||
*/ | ||
def readLine: IO[String] = | ||
Console[IO].readLine | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
core/jvm/src/main/scala/cats/effect/InterruptThrowable.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2020-2025 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 | ||
|
||
import java.nio.channels.ClosedByInterruptException | ||
|
||
private[effect] object InterruptThrowable { | ||
def apply(t: Throwable): Boolean = t match { | ||
case _: InterruptedException => true | ||
case _: ClosedByInterruptException => true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Le sigh… Well, we'll remove my shims when we do. |
||
case _ => false | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was the lack of
InterruptedException
here an oversight? Otherwise it seems like this is changing the semantic 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's incorporated in the matcher
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I think that was unclear: the previous matcher checked only for
ClosedByInterruptException
, but the new matcher now checks for bothClosedByInterruptException
andInterruptedException
. That's the change in semantic I'm referring to.