-
Notifications
You must be signed in to change notification settings - Fork 531
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
Native Polling System #3314
Closed
armanbilge
wants to merge
31
commits into
typelevel:series/3.x
from
armanbilge:feature/native-polling-system
Closed
Native Polling System #3314
Changes from 7 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
6d23310
Extract `PollingSystem` abstraction on Native
armanbilge 727bfe8
Add `FileDescriptorPoller` abstraction
armanbilge 9a8f7ed
Add `EpollSystem` and `KqueueSystem`
armanbilge 0889426
Add test for `Scheduler#sleep`
armanbilge 4250049
Add `FileDescriptorPollerSpec`
armanbilge 0b9ac02
Consistent error-handling in `KqueueSystem`
armanbilge 956734a
Make `pollingSystem` configurable in `IOApp`
armanbilge dda54b7
Nowarn unuseds
armanbilge 1206b27
Revise the fd poller spec
armanbilge e0c4ec3
Remove `maxEvents` config from `EpollSystem`
armanbilge eeeb3e6
Remove `maxEvents` config from `KqueueSystem`
armanbilge c4a0a16
Add test for many simultaneous events
armanbilge aae6e97
Remove redundant `final`
armanbilge 457f89c
Update comment
armanbilge 721c2fc
Add test for pre-existing readiness
armanbilge 4f9e57b
Add test for no readiness
armanbilge a520aee
Reimagine `FileDescriptorPoller`
armanbilge 5f8146b
Fix parameter names
armanbilge 3640605
Refactor/redesign `PollingSystem` ... again ... (:
armanbilge 42491da
Dump `EventLoop` abstraction
armanbilge 786127c
Update the `FileDescriptorPollerSpec`
armanbilge de3eea0
Rework `EpollSystem`
armanbilge eb8ba84
Set pipes to non-blocking mode
armanbilge 0124567
Add fcntl import
armanbilge 72b05a7
Fix bugs in spec
armanbilge d18fa76
Add some uncancelables
armanbilge 4d3a916
Revert "Add some uncancelables"
armanbilge 9ba870f
Rework `KqueueSystem`
armanbilge 9673883
Post-refactor typos
armanbilge 43b0b0a
Scope `.evalOn` even more tightly
armanbilge e5dd04f
Use `asyncCheckAttempt`
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
This file contains 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 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 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 |
---|---|---|
|
@@ -17,6 +17,9 @@ | |
package cats.effect | ||
|
||
import cats.effect.std.Console | ||
import cats.effect.unsafe.EventLoop | ||
|
||
import scala.reflect.ClassTag | ||
|
||
import java.time.Instant | ||
|
||
|
@@ -62,4 +65,11 @@ private[effect] abstract class IOCompanionPlatform { this: IO.type => | |
*/ | ||
def readLine: IO[String] = | ||
Console[IO].readLine | ||
|
||
def eventLoop[Poller](implicit ct: ClassTag[Poller]): IO[Option[EventLoop[Poller]]] = | ||
IO.executionContext.map { | ||
case loop: EventLoop[_] if ct.runtimeClass.isInstance(loop.poller()) => | ||
Some(loop.asInstanceOf[EventLoop[Poller]]) | ||
case _ => None | ||
} | ||
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. The |
||
} |
159 changes: 159 additions & 0 deletions
159
core/native/src/main/scala/cats/effect/unsafe/EpollSystem.scala
This file contains 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,159 @@ | ||
/* | ||
* Copyright 2020-2022 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 | ||
package unsafe | ||
|
||
import scala.scalanative.libc.errno._ | ||
import scala.scalanative.posix.string._ | ||
import scala.scalanative.posix.unistd | ||
import scala.scalanative.unsafe._ | ||
import scala.scalanative.unsigned._ | ||
import scala.util.control.NonFatal | ||
|
||
import java.io.IOException | ||
import java.util.{Collections, IdentityHashMap, Set} | ||
|
||
import EpollSystem.epoll._ | ||
import EpollSystem.epollImplicits._ | ||
|
||
final class EpollSystem private (maxEvents: Int) extends PollingSystem { | ||
|
||
def makePoller(): Poller = { | ||
val fd = epoll_create1(0) | ||
if (fd == -1) | ||
throw new IOException(fromCString(strerror(errno))) | ||
new Poller(fd, maxEvents) | ||
} | ||
|
||
def close(poller: Poller): Unit = poller.close() | ||
|
||
def poll(poller: Poller, nanos: Long, reportFailure: Throwable => Unit): Boolean = | ||
poller.poll(nanos, reportFailure) | ||
|
||
final class Poller private[EpollSystem] (private[EpollSystem] val epfd: Int, maxEvents: Int) | ||
extends FileDescriptorPoller { | ||
|
||
private[this] val callbacks: Set[FileDescriptorPoller.Callback] = | ||
Collections.newSetFromMap(new IdentityHashMap) | ||
|
||
private[EpollSystem] def close(): Unit = | ||
if (unistd.close(epfd) != 0) | ||
throw new IOException(fromCString(strerror(errno))) | ||
|
||
private[EpollSystem] def poll(timeout: Long, reportFailure: Throwable => Unit): Boolean = { | ||
val noCallbacks = callbacks.isEmpty() | ||
|
||
if (timeout <= 0 && noCallbacks) | ||
false // nothing to do here | ||
else { | ||
val timeoutMillis = if (timeout == -1) -1 else (timeout / 1000000).toInt | ||
|
||
val events = stackalloc[epoll_event](maxEvents.toUInt) | ||
|
||
val triggeredEvents = epoll_wait(epfd, events, maxEvents, timeoutMillis) | ||
|
||
if (triggeredEvents >= 0) { | ||
var i = 0 | ||
while (i < triggeredEvents) { | ||
val event = events + i.toLong | ||
val cb = FileDescriptorPoller.Callback.fromPtr(event.data) | ||
try { | ||
val e = event.events.toInt | ||
val readReady = (e & EPOLLIN) != 0 | ||
val writeReady = (e & EPOLLOUT) != 0 | ||
cb.notifyFileDescriptorEvents(readReady, writeReady) | ||
} catch { | ||
case ex if NonFatal(ex) => reportFailure(ex) | ||
} | ||
i += 1 | ||
} | ||
} else { | ||
throw new IOException(fromCString(strerror(errno))) | ||
} | ||
|
||
!callbacks.isEmpty() | ||
} | ||
} | ||
|
||
def registerFileDescriptor(fd: Int, reads: Boolean, writes: Boolean)( | ||
cb: FileDescriptorPoller.Callback): Runnable = { | ||
val event = stackalloc[epoll_event]() | ||
event.events = | ||
(EPOLLET | (if (reads) EPOLLIN else 0) | (if (writes) EPOLLOUT else 0)).toUInt | ||
event.data = FileDescriptorPoller.Callback.toPtr(cb) | ||
|
||
if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, event) != 0) | ||
throw new IOException(fromCString(strerror(errno))) | ||
callbacks.add(cb) | ||
|
||
() => { | ||
callbacks.remove(cb) | ||
if (epoll_ctl(epfd, EPOLL_CTL_DEL, fd, null) != 0) | ||
throw new IOException(fromCString(strerror(errno))) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
object EpollSystem { | ||
def apply(maxEvents: Int): EpollSystem = new EpollSystem(maxEvents) | ||
|
||
@extern | ||
private[unsafe] object epoll { | ||
|
||
final val EPOLL_CTL_ADD = 1 | ||
final val EPOLL_CTL_DEL = 2 | ||
final val EPOLL_CTL_MOD = 3 | ||
|
||
final val EPOLLIN = 0x001 | ||
final val EPOLLOUT = 0x004 | ||
final val EPOLLONESHOT = 1 << 30 | ||
final val EPOLLET = 1 << 31 | ||
|
||
type epoll_event | ||
type epoll_data_t = Ptr[Byte] | ||
|
||
def epoll_create1(flags: Int): Int = extern | ||
|
||
def epoll_ctl(epfd: Int, op: Int, fd: Int, event: Ptr[epoll_event]): Int = extern | ||
|
||
def epoll_wait(epfd: Int, events: Ptr[epoll_event], maxevents: Int, timeout: Int): Int = | ||
extern | ||
|
||
} | ||
|
||
private[unsafe] object epollImplicits { | ||
|
||
implicit final class epoll_eventOps(epoll_event: Ptr[epoll_event]) { | ||
def events: CUnsignedInt = !(epoll_event.asInstanceOf[Ptr[CUnsignedInt]]) | ||
def events_=(events: CUnsignedInt): Unit = | ||
!(epoll_event.asInstanceOf[Ptr[CUnsignedInt]]) = events | ||
|
||
def data: epoll_data_t = | ||
!((epoll_event.asInstanceOf[Ptr[Byte]] + sizeof[CUnsignedInt]) | ||
.asInstanceOf[Ptr[epoll_data_t]]) | ||
def data_=(data: epoll_data_t): Unit = | ||
!((epoll_event.asInstanceOf[Ptr[Byte]] + sizeof[CUnsignedInt]) | ||
.asInstanceOf[Ptr[epoll_data_t]]) = data | ||
} | ||
|
||
implicit val epoll_eventTag: Tag[epoll_event] = | ||
Tag.materializeCArrayTag[Byte, Nat.Digit2[Nat._1, Nat._2]].asInstanceOf[Tag[epoll_event]] | ||
|
||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
core/native/src/main/scala/cats/effect/unsafe/EventLoop.scala
This file contains 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,26 @@ | ||
/* | ||
* Copyright 2020-2022 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 | ||
package unsafe | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
trait EventLoop[Poller] extends ExecutionContext { | ||
|
||
def poller(): Poller | ||
|
||
} |
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.
We should… probably support Windows too someday :-(
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.
No.
More seriously, I think an fs2-uv with a libuv-based polling system would be the least masochistic way to deal with this.