Skip to content
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

Refactor scala.scalanative.runtime.ExecutionContext #3144

Conversation

WojciechMazur
Copy link
Contributor

  • Refactor scala.scalanative.runtime.ExecutionContext
    • Rename to NativeExecutionContext to match Scala.js naming conventions (JsExecutionContext)
    • Deprecated global accessor, use queue instead
    • Add capability of running single task from QueueExecutionContext based on @lolgab work in Add loopRunOnce to runtime and ExecutionContext #1852
    • Add accessor for hasNext, sheduled, runNext
  • Add tests for new interface of NativeExecutionContext
  • Adapt junit-async and test-interface modules to use new interface
  • Depreacate package method scala.scalanative.runtime.loop

Based on that change we would provide the scala.concurrent.ExecutionContext changes allowing to use real concurrent execution context in multithreading mode

@WojciechMazur WojciechMazur changed the title Refactor scala.concurrent.ExecutionContext Refactor scala.scalanative.runtime.ExecutionContext Feb 6, 2023
Comment on lines 45 to 59
// The proxy methods here are defined to allow integrating projects
// to interact with our private execution context without leaking the abstraction

/** Checks if there is any scheduled runnable which can be executed */
private[runtime] def hasNext: Boolean = QueueExecutionContext.hasNext

/** Exuecute next available runnable in the queue. No-op if the queue is empty
*/
private[runtime] def runNext(): Unit = QueueExecutionContext.runNext()

/** Returns the number of currently scheduled tasks */
private[runtime] def scheduled: Int = QueueExecutionContext.scheduled

/** Execute all the tasks in the queue until there is none left */
private[runtime] def loop(): Unit = QueueExecutionContext.loop()
Copy link
Member

@armanbilge armanbilge Feb 6, 2023

Choose a reason for hiding this comment

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

I'm glad we are considering these APIs, but maybe can this happen in a separate PR? I think this deserves a discussion. Cats Effect might also benefit from this, but we have also been successful without it—it is not a blocker.

In Scala Contributors1 we discussed adding some new APIs to Scala standard library that will better support event loops. For example a Scheduler API. This appears to have support from Scala Center, and Cats Effect team is also motivated for this change.

If those changes land in Scala 2.13 standard library, then it means single-thread Scala Native will be able to support tasks and timers out-of-the-box. So we should be careful to design this API so that we can make those changes without breaking backwards-compatibility.

Footnotes

  1. https://contributors.scala-lang.org/t/return-of-the-scala-actor/6041/8

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, that's a good point. In such a case, I'll be putting this on hold, and I would use the old API instead in further integration of multithreading.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've reverted all the changes that expose new methods, I'll create a seperate PR with API extension

@WojciechMazur WojciechMazur added this to the 0.5.0 milestone Feb 6, 2023
@WojciechMazur WojciechMazur marked this pull request as draft February 6, 2023 18:27
@WojciechMazur WojciechMazur force-pushed the refactor/multithread-native-execution-context branch 2 times, most recently from 85f8302 to 32896c0 Compare February 6, 2023 20:48
@WojciechMazur WojciechMazur force-pushed the refactor/multithread-native-execution-context branch from 32896c0 to 7cac7ec Compare March 27, 2024 20:36
@WojciechMazur WojciechMazur marked this pull request as ready for review March 27, 2024 21:21
@WojciechMazur WojciechMazur merged commit 6123483 into scala-native:main Mar 28, 2024
61 checks passed
@WojciechMazur WojciechMazur deleted the refactor/multithread-native-execution-context branch March 28, 2024 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants