-
Notifications
You must be signed in to change notification settings - Fork 2
/
ev-poll.sml
69 lines (53 loc) · 2.03 KB
/
ev-poll.sml
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
signature OS_IO_EV = sig
type evFd = int
datatype evFilter = evRead | evWrite
type evCb = (evFd * evFilter) -> unit
datatype evDesc = evAdd of evFd * evFilter * evCb | evDelete of evFd * evFilter
type ev
val evInit: unit -> ev
val evModify: ev -> evDesc list -> int
val evWait: ev -> Time.time option -> int
end
structure Ev :> OS_IO_EV =
struct
structure H = HashArrayInt
type evFd = int
datatype evFilter = evRead | evWrite
type evCb = (evFd * evFilter) -> unit
datatype evDesc = evAdd of evFd * evFilter * evCb | evDelete of evFd * evFilter
type ev = { rH: (OS.IO.poll_desc * evCb) H.hash, wH: (OS.IO.poll_desc * evCb) H.hash }
val hash_size = 100
fun evInit () = { rH = H.hash hash_size, wH = H.hash hash_size }
val intToPollDesc = Option.valOf o OS.IO.pollDesc o Posix.FileSys.fdToIOD o Posix.FileSys.wordToFD o SysWord.fromInt
val pollDescToInt = SysWord.toInt o Posix.FileSys.fdToWord o Option.valOf o Posix.FileSys.iodToFD o OS.IO.pollToIODesc o OS.IO.infoToPollDesc
fun evModify (ev:ev) ev_desc_list =
let
fun evFilterToH evRead = #rH ev
| evFilterToH evWrite = #wH ev
in
List.app (fn d => case d of
evAdd (fd, filter, cb) => H.update ((evFilterToH filter), fd, (intToPollDesc fd, cb))
| evDelete (fd, filter) => H.delete ((evFilterToH filter), fd)
) ev_desc_list;
List.length ev_desc_list
end
fun evWait (ev:ev) t =
let
open OS.IO
val ds:poll_desc list = H.fold (fn (k, (d, cb), r) => (pollIn d)::r) [] (#rH ev)
val ds:poll_desc list = H.fold (fn (k, (d, cb), r) => (pollIn d)::r) ds (#wH ev)
val pInfo = poll (ds, t)
fun doit (h, fd, f) = case H.sub (h, fd) of NONE => () | SOME (d, cb) => cb (fd, f)
in
List.app (fn i =>
let
val fd = pollDescToInt i
in
if isIn i then doit (#rH ev, fd, evRead) else
if isOut i then doit (#wH ev, fd, evWrite) else
()
end
) pInfo;
List.length pInfo
end
end