src/Pure/Concurrent/counter.ML
author wenzelm
Thu, 21 Apr 2022 11:28:50 +0200
changeset 75442 d5041b68a237
parent 68804 cbde6e3b132b
permissions -rw-r--r--
clarified signature;

(*  Title:      Pure/Concurrent/counter.ML
    Author:     Makarius

Synchronized counter for unique identifiers > 0.

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

signature COUNTER =
sig
  val make: unit -> unit -> int
end;

structure Counter: COUNTER =
struct

fun make () =
  let
    val counter = Synchronized.var "counter" 0;
    fun next () =
      Synchronized.change_result counter
        (fn i =>
          let
            val k = i + 1;
            val n = if Thread_Data.is_virtual then 2 * k + 1 else 2 * k;
          in (n, k) end);
  in next end;

end;