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

Add best-effort stealing API to PollingSystem #4113

Open
wants to merge 6 commits into
base: series/3.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ abstract class PollingSystem {
*/
def poll(poller: Poller, nanos: Long, reportFailure: Throwable => Unit): Boolean

/**
* Makes a best-effort to steal completed I/O events. Not all polling systems support this.
*
* This method is safe to call concurrently from threads that do not own the poller.
*
* @param poller
* the thread-local [[Poller]] used to poll events.
*
* @param reportFailure
* callback that handles any failures that occur during stealing.
*
* @return
* whether any events were stolen. e.g. if the method returned due to timeout, this should
* be `false`.
*/
def steal(poller: Poller, reportFailure: Throwable => Unit): Boolean
Copy link
Member

Choose a reason for hiding this comment

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

Something that's a bit unclear in this API is whether poller is what you're stealing from or to.

Copy link
Member Author

Choose a reason for hiding this comment

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

Noted, although you always need somewhere to steal from. Also similar to timers we only steal completed events, so they don't need a destination heap / poller to be stolen to, this would be pretty challenging to implement anyway.


/**
* @return
* whether poll should be called again (i.e., there are more events to be polled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ final class SelectorSystem private (provider: SelectorProvider) extends PollingS
} catch {
case ex if NonFatal(ex) =>
error = ex
readyOps = -1 // interest all waiters
readyOps = -1 // notify all waiters
}

val value = if (error ne null) Left(error) else Right(readyOps)
Expand Down Expand Up @@ -98,6 +98,8 @@ final class SelectorSystem private (provider: SelectorProvider) extends PollingS
} else false
}

def steal(poller: Poller, reportFailure: Throwable => Unit): Boolean = false

def needsPoll(poller: Poller): Boolean =
!poller.selector.keys().isEmpty()

Expand Down
2 changes: 2 additions & 0 deletions core/jvm/src/main/scala/cats/effect/unsafe/SleepSystem.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ object SleepSystem extends PollingSystem {
false
}

def steal(poller: Poller, reportFailure: Throwable => Unit): Boolean = false

def needsPoll(poller: Poller): Boolean = false

def interrupt(targetThread: Thread, targetPoller: Poller): Unit =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ object EpollSystem extends PollingSystem {
def poll(poller: Poller, nanos: Long, reportFailure: Throwable => Unit): Boolean =
poller.poll(nanos)

def steal(poller: Poller, reportFailure: Throwable => Unit): Boolean = false

def needsPoll(poller: Poller): Boolean = poller.needsPoll()

def interrupt(targetThread: Thread, targetPoller: Poller): Unit = ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ object KqueueSystem extends PollingSystem {
def poll(poller: Poller, nanos: Long, reportFailure: Throwable => Unit): Boolean =
poller.poll(nanos)

def steal(poller: Poller, reportFailure: Throwable => Unit): Boolean = false

def needsPoll(poller: Poller): Boolean =
poller.needsPoll()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ abstract class PollingExecutorScheduler(pollEvery: Int)
poller.poll(nanos.nanos)
true
}
def steal(poller: Poller, reportFailure: Throwable => Unit): Boolean = false
def needsPoll(poller: Poller) = needsPoll
def interrupt(targetThread: Thread, targetPoller: Poller): Unit = ()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ object SleepSystem extends PollingSystem {
false
}

def steal(poller: Poller, reportFailure: Throwable => Unit): Boolean = false

def needsPoll(poller: Poller): Boolean = false

def interrupt(targetThread: Thread, targetPoller: Poller): Unit = ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ trait IOPlatformSpecification extends DetectPlatform { self: BaseSpec with Scala
}
}

def steal(poller: Poller, reportFailure: Throwable => Unit) = false

def makeApi(access: (Poller => Unit) => Unit): DummySystem.Api =
new DummyPoller {
def poll = IO.async_[Unit] { cb =>
Expand Down
Loading