Skip to content

Instantly share code, notes, and snippets.

@Octachron
Created July 20, 2022 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Octachron/163c1401b8e7ceb28e8b7a2e457b26d7 to your computer and use it in GitHub Desktop.
Save Octachron/163c1401b8e7ceb28e8b7a2e457b26d7 to your computer and use it in GitHub Desktop.
type t = < count: int >
let leak: t option Atomic.t = Atomic.make None
let stop = Atomic.make false
class virtual counter = object(self)
val mutable counter = -1
method count = counter
initializer
Atomic.set leak (Some((self:>t)))
end
let rec spy () = match Atomic.get stop, Atomic.get leak with
| true, _ -> ()
| false, Some c when c#count = -1 ->
Format.printf "uninitialized counter seen@.";
Atomic.set stop true
| _ -> spy ()
let slow_random_int () =
let a = Array.init 1024 (fun i -> i+1) in
a.(Random.int 1024)
class concrete = object
inherit counter
initializer
counter <- slow_random_int ()
end
let () =
let n_domains = ref 1 in
let n_tries = ref 1 in
let args = [
"-domains", Arg.Int ((:=) n_domains), "number of parallel spies";
"-tries", Arg.Int ((:=) n_tries), "number of generated candidates";
]
in
let () = Arg.parse args ignore "try to spy on an unitialized object" in
let n_domains, n_tries = !n_domains, !n_tries in
let spies = Array.init n_domains (fun _ -> Domain.spawn spy) in
let candidates = Array.init n_tries (fun _ -> new concrete) in
let () = Atomic.set stop true in
let () = Array.iter Domain.join spies in
let selection = Random.int n_tries in
Format.printf "counter of candidate %d: %d@." selection (candidates.(selection)#count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment