package gapi-ocaml

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file netsys_oothr.ml

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
(* $Id$ *)

class type mtprovider =
object
  method single_threaded : bool
  method create_thread : 's 't . ('s -> 't) -> 's -> thread
  method self : thread
  method yield : unit -> unit
  method create_mutex : unit -> mutex
  method create_condition : unit -> condition
end

and thread =
object
  method id : int
  method join : unit -> unit
  method repr : exn
end

and mutex =
object
  method lock : unit -> unit
  method unlock : unit -> unit
  method try_lock : unit -> bool
  method repr : exn
end

and condition =
object
  method wait : mutex -> unit
  method signal : unit -> unit
  method broadcast : unit -> unit
  method repr : exn
end

(* single-threaded dummy stuff: *)

exception Dummy

let stthread() : thread =
  ( object
      method id = 0
      method join() = 
	failwith "Netsys_oothr: join not possible in single-threaded program"
      method repr = Dummy
    end
  )

let stmutex() : mutex =
  ( object
      method lock() = ()
      method unlock() = ()
      method try_lock() = true
      method repr = Dummy
    end
  )

let stcondition() : condition =
  ( object
      method wait _ = ()
      method signal() = ()
      method broadcast() = ()
      method repr = Dummy
    end
  )

let stprovider : mtprovider =
  ( object
      method single_threaded = true
      method create_thread : 's 't . ('s -> 't) -> 's -> thread =
	fun _ _ -> failwith "Netsys_oothr: create_thread not possible in single-threaded program"
      method self = stthread()
      method yield() = ()
      method create_mutex() = stmutex()
      method create_condition() = stcondition()
    end
  )

let provider = ref stprovider
let single_threaded = ref false  (* whether we know this for sure *)
let st_init = ref false

let serialize  mutex f arg =
  if !single_threaded then (
    f arg
  )
  else (
    if not !st_init then (
      single_threaded := !provider # single_threaded;
      st_init := true
    );
    mutex # lock();
    let r = 
      try f arg
      with e -> mutex # unlock(); raise e in
    mutex # unlock();
    r
  )

let atomic_init var new_val =
  let new_val_opt = Some new_val in
  match !var with
    | None -> var := new_val_opt; new_val
    | Some x -> x

let compare_and_swap var old_value new_value =
  !var == old_value && (var := new_value; true)
OCaml

Innovation. Community. Security.