Skip to content

Commit

Permalink
Doodle polling system high-level API
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge committed Sep 29, 2022
1 parent 18fb15d commit 3c27ad9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
14 changes: 14 additions & 0 deletions core/jvm/src/main/scala/cats/effect/IOCompanionPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions kernel/jvm/src/main/scala/cats/effect/kernel/AsyncPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] =>
Expand Down Expand Up @@ -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)
}

0 comments on commit 3c27ad9

Please sign in to comment.