Debian Codegraph @

lib/cml/core-cml/io-manager.sml

raw
1 (* io-manager.sml
2 * Ported to MLton threads.
3 *)
4
5 (* io-manager.sml
6 *
7 * COPYRIGHT (c) 1995 AT&T Bell Laboratories.
8 * COPYRIGHT (c) 1989-1991 John H. Reppy
9 *
10 * This is a generic I/O manager for CML. It uses the OS.IO polling
11 * mechanism.
12 * NOTE: it currently does not work if more than one thread blocks on the same
13 * descriptor.
14 *)
15 structure IOManager: IO_MANAGER =
16 struct
17 structure Assert = LocalAssert(val assert = false)
18 structure Debug = LocalDebug(val debug = false)
19
20 structure E = Event
21 structure S = Scheduler
22
23 type iodesc = OS.IO.iodesc
24 type poll_desc = OS.IO.poll_desc
25 type poll_info = OS.IO.poll_info
26
27 fun debug msg =
28 Debug.sayDebug ([S.atomicMsg, S.tidMsg], msg)
29 fun debug' msg =
30 debug (fn () => msg)
31
32 datatype preempt_type = READIED | INQUEUE of poll_desc list | EMPTYQUEUE
33 datatype trans_id = datatype TransID.trans_id
34 datatype trans_id_state = datatype TransID.trans_id_state
35
36 type io_wait_item =
37 { pd: poll_desc
38 , tid: trans_id
39 , cleanUp: unit -> unit
40 , thrd: poll_info S.thread
41 }
42
43 val waiting = ref ([] : io_wait_item list)
44
45 fun poll l =
46 OS.IO.poll (l, SOME Time.zeroTime)
47 handle _ => []
48
49 fun ioEvt pd =
50 let
51 fun blockFn {transId, cleanUp, next} =
52 let
53 val () = Assert.assertAtomic' ("IOManager.ioEvt.blockFn", NONE)
54 val () = debug' "ioEvt(1.1)" (* Atomic 1 *)
55 val () = Assert.assertAtomic' ("IOManager.ioEvt(1.1)", SOME 1)
56 val pi = S.atomicSwitch (fn thrd =>
57 let
58 val item =
59 {pd = pd, tid = transId, cleanUp = cleanUp, thrd = thrd}
60 in
61 waiting := item :: !waiting;
62 next ()
63 end)
64 val () = debug' "ioEvt(1.2)" (* NonAtomic *)
65 val () = Assert.assertNonAtomic' "IOManager.ioEvt(1.2)"
66 in
67 pi
68 end
69 fun pollFn () =
70 let
71 val () = Assert.assertAtomic' ("IOManager.ioEvt.pollFn", NONE)
72 val () = debug' "IOManager.ioEvt(2)" (* Atomic 1 *)
73 val () = Assert.assertAtomic' ("IOManager.ioEvt(2)", SOME 1)
74 in
75 case poll [pd] of
76 [pi] =>
77 E.enabled {prio = ~1, doitFn = fn () => (S.atomicEnd (); pi)}
78 | _ => E.blocked blockFn
79 end
80 in
81 E.bevt pollFn
82 end
83
84 fun sameDesc (pi, pd) = OS.IO.infoToPollDesc pi = pd
85
86 fun clean wq =
87 let
88 fun cl ([]: io_wait_item list, pds, q) = (pds, q)
89 | cl ({tid = TXID (ref CANCEL), ...} :: r, pds, wq) = cl (r, pds, wq)
90 | cl ((item as {pd, ...}) :: r, pds, wq) =
91 cl (r, pd :: pds, item :: wq)
92 in
93 cl (wq, [], [])
94 end
95
96 fun enqueue
97 ( readied
98 , {tid as TXID (ref TRANS), cleanUp, thrd, ...}: io_wait_item
99 , pi: poll_info
100 ) =
101 ( readied := true
102 ; TransID.force tid
103 ; cleanUp ()
104 ; S.ready (S.prepVal (thrd, pi))
105 )
106 | enqueue (_, {tid = TXID (ref CANCEL), ...}, _) = ()
107
108 fun preempt () : preempt_type =
109 let
110 val () = Assert.assertAtomic' ("IOManager.preempt", NONE)
111 val () = debug' "IOManager.preempt" (* Atomic 1 *)
112 val () = Assert.assertAtomic' ("IOManager.preempt", SOME 1)
113 val readied = ref false
114 in
115 case clean (!waiting) of
116 ([], _) => waiting := []
117 | (pds, wq) =>
118 (case poll pds of
119 [] => waiting := List.rev wq
120 | l =>
121 let
122 fun filter ([], r, wq) =
123 waiting := List.revAppend (r, wq)
124 | filter (pi :: pis, (item: io_wait_item) :: r, wq) =
125 if sameDesc (pi, #pd item) then
126 (enqueue (readied, item, pi); filter (pis, r, wq))
127 else
128 filter (pi :: pis, r, item :: wq)
129 | filter _ = raise Fail "preempt: unreachable case"
130 in
131 filter (l, wq, [])
132 end);
133 if !readied then
134 READIED
135 else
136 case !waiting of
137 [] => EMPTYQUEUE
138 | items => INQUEUE (List.map #pd items)
139 end
140 end