summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-threads/mutex_errors.ml
blob: 25d3330955dcbf903192f9a17961db4ea0613558 (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
(* TEST

* hassysthreads
include systhreads
** bytecode
** native

*)

let log s =
  Printf.printf "%s\n%!" s

let mutex_lock_must_fail m =
  try
    Mutex.lock m; log "Should have failed!"
  with Sys_error _ ->
    log "Error reported"

let mutex_unlock_must_fail m =
  try
    Mutex.unlock m; log "Should have failed!"
  with Sys_error _ ->
    log "Error reported"

let mutex_deadlock () =
  let m = Mutex.create() in
  log "Acquiring mutex";
  Mutex.lock m;
  log "Acquiring mutex again";
  mutex_lock_must_fail m;
  log "Releasing mutex";
  Mutex.unlock m;
  let f () =
    log "Acquiring mutex from another thread";
    Mutex.lock m;
    log "Success";
    Mutex.unlock m in
  Thread.join (Thread.create f ())

let mutex_unlock_twice () =
  let m = Mutex.create() in
  log "Acquiring mutex";
  Mutex.lock m;
  log "Releasing mutex";
  Mutex.unlock m;
  log "Releasing mutex again";
  mutex_unlock_must_fail m;
  log "Releasing mutex one more time";
  mutex_unlock_must_fail m

let mutex_unlock_other_thread () =
  let m = Mutex.create() in
  log "Acquiring mutex";
  Mutex.lock m;
  let f () =
    log "Releasing mutex from another thread";
    mutex_unlock_must_fail m;
    log "Releasing mutex from another thread (again)";
    mutex_unlock_must_fail m in
  Thread.join (Thread.create f ())

let _ =
  log "---- Self deadlock";
  mutex_deadlock();
  log "---- Unlock twice";
  mutex_unlock_twice();
  log "---- Unlock in other thread";
  mutex_unlock_other_thread()