Source file poll.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
module Event = Event
module Backend = Backend
module Timeout = Timeout
module Poll_intf = Poll_intf

let backends =
  [ (module Kqueue_poll : Poll_intf.S), Kqueue.available
  ; (module Epoll_poll : Poll_intf.S), Epoll_poll.available
  ; (module Wepoll_poll : Poll_intf.S), Wepoll_poll.available
  ; (module Empty_poll : Poll_intf.S), true
  ]
;;

module Poll = struct
  module type S = sig
    include Poll_intf.S

    val poll : t
  end

  type t = (module S)
end

type t = Poll.t

let create' ?num_events backend =
  let module P = struct
    include (val backend : Poll_intf.S)

    let poll = create ?num_events ()
  end
  in
  (module P : Poll.S)
;;

let create ?num_events () =
  let backend =
    let rec aux = function
      | [] -> failwith "No poll backend found"
      | (poll, available) :: xs ->
        let module P = (val poll : Poll_intf.S) in
        if available then poll else aux xs
    in
    aux backends
  in
  create' ?num_events backend
;;

let backend t =
  let module P = (val t : Poll.S) in
  P.backend
;;

let set t fd event =
  let module P = (val t : Poll.S) in
  P.set P.poll fd event
;;

let wait t timeout =
  let module P = (val t : Poll.S) in
  P.wait P.poll timeout
;;

let iter_ready t ~f =
  let module P = (val t : Poll.S) in
  P.iter_ready P.poll ~f
;;

let clear t =
  let module P = (val t : Poll.S) in
  P.clear P.poll
;;

let close t =
  let module P = (val t : Poll.S) in
  P.close P.poll
;;