Skip to content

Instantly share code, notes, and snippets.

@kubukoz
Forked from amitayh/CatsCountDownLatch.scala
Created February 17, 2020 17:20
Show Gist options
  • Save kubukoz/a3339f0e219c230c33ef6375eb2b6d10 to your computer and use it in GitHub Desktop.
Save kubukoz/a3339f0e219c230c33ef6375eb2b6d10 to your computer and use it in GitHub Desktop.
import cats.effect.Concurrent
import cats.syntax.flatMap._
import cats.syntax.functor._
import cats.effect.concurrent.Semaphore
trait CountDownLatch[F[_]] {
def countDown: F[Unit]
def await: F[Unit]
}
object CountDownLatch {
def apply[F[_]: Concurrent](count: Int): F[CountDownLatch[F]] =
for {
sem <- Semaphore[F](0)
get <- Concurrent.memoize(sem.acquireN(count.toLong))
} yield new CountDownLatch[F] {
override val countDown: F[Unit] = sem.release
override val await: F[Unit] = get
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment