File: lazy3.ml

package info (click to toggle)
js-of-ocaml 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 37,932 kB
  • sloc: ml: 135,957; javascript: 58,364; ansic: 437; makefile: 422; sh: 12; perl: 4
file content (35 lines) | stat: -rw-r--r-- 853 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(* TEST
 ocamlopt_flags += " -O3 ";
*)

(* In this test we force a lazy from two concurrent domains without
   synchronization. This leads to unspecified behavior but still
   should not crash. Currently, the implementation raises Undefined,
   and that's what we test here.
*)


let f count =
  let _n = (Domain.self ():> int) in
  let r = ref 0 in
  for i = 1 to count do
    incr r;
  done;
  !r

let main () =
  let l = lazy (f 1_000_000_000) in
  let d1 =
    Domain.spawn (fun () ->
        let _n = (Domain.self ():> int) in
        Lazy.force l)
  in
  let n2 = try Some (Lazy.force l) with Lazy.Undefined -> None in
  let n1 = Domain.join d1 in
  (n1, n2)

let _ =
  match main () with
  | (n1, Some n2) -> Printf.printf "n1=%d n2=%d\n" n1 n2
  | (_, None) -> print_endline "Undefined"
  | exception Lazy.Undefined -> print_endline "Undefined"