diff --git a/core/jvm/src/main/scala/cats/effect/IOCompanionPlatform.scala b/core/jvm/src/main/scala/cats/effect/IOCompanionPlatform.scala index 02278b9874..9a91bd5f53 100644 --- a/core/jvm/src/main/scala/cats/effect/IOCompanionPlatform.scala +++ b/core/jvm/src/main/scala/cats/effect/IOCompanionPlatform.scala @@ -18,6 +18,9 @@ package cats.effect import cats.effect.std.Console import cats.effect.tracing.Tracing +import cats.effect.unsafe.WorkerThread + +import scala.reflect.ClassTag import java.time.Instant import java.util.concurrent.{CompletableFuture, CompletionStage} @@ -141,4 +144,15 @@ private[effect] abstract class IOCompanionPlatform { this: IO.type => */ def readLine: IO[String] = Console[IO].readLine + + def delayWithPollingSystem[S, A](f: S => A)(implicit ct: ClassTag[S]): IO[Option[A]] = delay { + Thread.currentThread() match { + case worker: WorkerThread => + val system = worker.pollingSystem + if (ct.runtimeClass.isInstance(system)) + Some(f(system.asInstanceOf[S])) + else None + case _ => None + } + } } diff --git a/core/jvm/src/main/scala/cats/effect/unsafe/WorkerThread.scala b/core/jvm/src/main/scala/cats/effect/unsafe/WorkerThread.scala index 75d527bc3e..83d136a0c4 100644 --- a/core/jvm/src/main/scala/cats/effect/unsafe/WorkerThread.scala +++ b/core/jvm/src/main/scala/cats/effect/unsafe/WorkerThread.scala @@ -39,7 +39,7 @@ import java.util.concurrent.locks.LockSupport * system when compared to a fixed size thread pool whose worker threads all draw tasks from a * single global work queue. */ -private final class WorkerThread( +private[effect] final class WorkerThread( idx: Int, // Local queue instance with exclusive write access. private[this] var queue: LocalQueue, @@ -98,6 +98,8 @@ private final class WorkerThread( val nameIndex: Int = pool.blockedWorkerThreadNamingIndex.incrementAndGet() + def pollingSystem: Any = ??? // stub + // Constructor code. { // Worker threads are daemon threads. diff --git a/kernel/jvm/src/main/scala/cats/effect/kernel/AsyncPlatform.scala b/kernel/jvm/src/main/scala/cats/effect/kernel/AsyncPlatform.scala index b5d0ee63d2..df31f7393a 100644 --- a/kernel/jvm/src/main/scala/cats/effect/kernel/AsyncPlatform.scala +++ b/kernel/jvm/src/main/scala/cats/effect/kernel/AsyncPlatform.scala @@ -16,6 +16,9 @@ package cats.effect.kernel +import scala.annotation.nowarn +import scala.reflect.ClassTag + import java.util.concurrent.{CompletableFuture, CompletionException, CompletionStage} private[kernel] trait AsyncPlatform[F[_]] extends Serializable { this: Async[F] => @@ -53,4 +56,14 @@ private[kernel] trait AsyncPlatform[F[_]] extends Serializable { this: Async[F] } } } + + /** + * Attempts to retrieve the runtime's polling system, if present and an instance of `S`. + * + * If successful, it invokes the side-effect `f` with it and returns the result. + * + * If no polling system of type `S` is available, then `f` is not run and `None` is returned. + */ + @nowarn + def delayWithPollingSystem[S: ClassTag, A](f: S => A): F[Option[A]] = pure(None) }