Is this naive implementation of semaphore logicall...
# coroutines
o
Is this naive implementation of semaphore logically correct?
Copy code
class Semaphore(permits: Int) {

  private val inner = java.util.concurrent.Semaphore(permits)

  suspend fun acquire() {
    return suspendCoroutineOrReturn { _ ->
      if (inner.tryAcquire()) {
        Unit
      } else {
        COROUTINE_SUSPENDED
      }
    }
  }

  fun release() = inner.release()

}

fun main(args: Array<String>) = runBlocking {
  val s = Semaphore(1)
  s.acquire()
  println("acquired")
  s.release()
}