TSemaphore

A convenience implementation of a semaphore in the Txn monad, built on top of TVar.

import cats.effect.IO
import cats.effect.unsafe.implicits.global

import io.github.timwspence.cats.stm.STM

val stm = STM.runtime[IO].unsafeRunSync()
// stm: STM[IO] = io.github.timwspence.cats.stm.STM$Make$$anon$1$$anon$2@4a2bca6d
import stm._

val txn: Txn[Long] = for {
  tsem  <- TSemaphore.make(1)
  _     <- tsem.acquire
  zero  <- tsem.available
  _     <- tsem.release
} yield zero
// txn: Txn[Long] = Bind(
//   txn = Bind(
//     txn = Alloc(
//       v = Delay(
//         thunk = cats.effect.IO$$$Lambda$10367/0x0000000802cffc40@6b4829ac,
//         event = cats.effect.tracing.TracingEvent$StackTrace
//       )
//     ),
//     f = scala.Function1$$Lambda$10436/0x0000000802da9840@3e66de6c
//   ),
//   f = <function1>
// )

val result = stm.commit(txn).unsafeRunSync()
// result: Long = 0L