src/Pure/Concurrent/counter.scala
author wenzelm
Sat, 17 Feb 2024 21:26:00 +0100
changeset 79656 10e560f2f580
parent 78243 0e221a8128e4
permissions -rw-r--r--
more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;

/*  Title:      Pure/Concurrent/counter.scala
    Author:     Makarius

Synchronized counter for unique identifiers < 0.

NB: ML ticks forwards, JVM ticks backwards.
*/

package isabelle


object Counter {
  type ID = Long
  def make(): Counter = new Counter
}

final class Counter private {
  private var count: Counter.ID = 0

  def apply(): Counter.ID = synchronized {
    require(count > Long.MinValue, "counter overflow")
    count -= 1
    count
  }

  override def toString: String = count.toString
}