summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-threads/signal.ml
blob: bfc1ea8c301e0271b32d65dc9bdad29530e9c946 (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(* TEST

* hassysthreads
include systhreads

readonly_files = "sigint.c"

** libunix (* excludes mingw32/64 and msvc32/64 *)

*** setup-ocamlc.byte-build-env

program = "${test_build_directory}/signal.byte"

**** ocamlc.byte

program = "sigint"
all_modules = "sigint.c"

***** ocamlc.byte

program = "${test_build_directory}/signal.byte"
all_modules = "signal.ml"

****** check-ocamlc.byte-output
******* run
******** check-program-output

*** setup-ocamlopt.byte-build-env

program = "${test_build_directory}/signal.opt"

**** ocamlopt.byte

program = "sigint"
all_modules = "sigint.c"

***** ocamlopt.byte

program = "${test_build_directory}/signal.opt"
all_modules = "signal.ml"

****** check-ocamlopt.byte-output
******* run
******** check-program-output

*)

let signaled = ref false

let counter = ref 0

let sighandler _ =
  signaled := true

let print_message delay c =
  while (not !signaled) && (!counter <= 20) do
    incr counter;
    print_char c; flush stdout; Thread.delay delay
  done

let _ =
  ignore (Sys.signal Sys.sigint (Sys.Signal_handle sighandler));
  let th1 = Thread.create (print_message 0.6666666666) 'a' in
  print_message 1.0 'b';
  Thread.join th1;
  if !signaled then begin
    print_string "Got ctrl-C, exiting"; print_newline();
    exit 0
  end else begin
    print_string "not signaled???"; print_newline();
    exit 2
  end