| 1 |
(* modified from SML/NJ sources by Stephen Weeks 1998-06-25 *) |
| 2 |
(* modified by Matthew Fluet 2002-10-11 *) |
| 3 |
(* modified by Matthew Fluet 2002-11-21 *) |
| 4 |
(* modified by Matthew Fluet 2006-04-30 *) |
| 5 |
(* modified by Matthew Fluet 2008-04-06 *) |
| 6 |
(* modified by Matthew Fluet 2013-06-18 *) |
| 7 |
(* modified by Matthew Fluet 2019-11-05 *) |
| 8 |
|
| 9 |
(* os-io.sml |
| 10 |
* |
| 11 |
* COPYRIGHT (c) 1995 AT&T Bell Laboratories. |
| 12 |
* |
| 13 |
* NOTE: this interface has been proposed, but not yet adopted by the |
| 14 |
* Standard basis committee. |
| 15 |
* |
| 16 |
*) |
| 17 |
|
| 18 |
structure OS_IO: OS_IO = |
| 19 |
struct |
| 20 |
structure Error = PosixError |
| 21 |
|
| 22 |
(* an iodesc is an abstract descriptor for an OS object that |
| 23 |
* supports I/O (e.g., file, tty device, socket, ...). |
| 24 |
*) |
| 25 |
type iodesc = PreOS.IODesc.t |
| 26 |
|
| 27 |
datatype iodesc_kind = K of string |
| 28 |
|
| 29 |
val iodToFd = PrePosix.FileDesc.fromRep o PreOS.IODesc.toRep |
| 30 |
val fdToIod = PreOS.IODesc.fromRep o PrePosix.FileDesc.toRep |
| 31 |
|
| 32 |
val iodescToWord = C_Fd.castToSysWord o PreOS.IODesc.toRep |
| 33 |
|
| 34 |
(* return a hash value for the I/O descriptor. *) |
| 35 |
val hash = SysWord.toWord o iodescToWord |
| 36 |
|
| 37 |
(* compare two I/O descriptors *) |
| 38 |
fun compare (i, i') = SysWord.compare (iodescToWord i, iodescToWord i') |
| 39 |
|
| 40 |
structure Kind = |
| 41 |
struct |
| 42 |
val file = K "FILE" |
| 43 |
val dir = K "DIR" |
| 44 |
val symlink = K "LINK" |
| 45 |
val tty = K "TTY" |
| 46 |
val pipe = K "PIPE" |
| 47 |
val socket = K "SOCK" |
| 48 |
val device = K "DEV" |
| 49 |
end |
| 50 |
|
| 51 |
(* return the kind of I/O descriptor *) |
| 52 |
fun kind (iod) = let |
| 53 |
val stat = Posix.FileSys.fstat (iodToFd iod) |
| 54 |
in |
| 55 |
if (Posix.FileSys.ST.isReg stat) then Kind.file |
| 56 |
else if (Posix.FileSys.ST.isDir stat) then Kind.dir |
| 57 |
else if (Posix.FileSys.ST.isChr stat) then Kind.tty |
| 58 |
else if (Posix.FileSys.ST.isBlk stat) then Kind.device (* ?? *) |
| 59 |
else if (Posix.FileSys.ST.isLink stat) then Kind.symlink |
| 60 |
else if (Posix.FileSys.ST.isFIFO stat) then Kind.pipe |
| 61 |
else if (Posix.FileSys.ST.isSock stat) then Kind.socket |
| 62 |
else K "UNKNOWN" |
| 63 |
end |
| 64 |
|
| 65 |
type poll_flags = {rd: bool, wr: bool, pri: bool} |
| 66 |
datatype poll_desc = PollDesc of iodesc * poll_flags |
| 67 |
datatype poll_info = PollInfo of iodesc * poll_flags |
| 68 |
|
| 69 |
(* create a polling operation on the given descriptor; note that |
| 70 |
* not all I/O devices support polling, but for the time being, we |
| 71 |
* don't test for this. |
| 72 |
*) |
| 73 |
fun pollDesc iod = SOME (PollDesc (iod, {rd=false, wr=false, pri=false})) |
| 74 |
|
| 75 |
(* return the I/O descriptor that is being polled *) |
| 76 |
fun pollToIODesc (PollDesc (iod, _)) = iod |
| 77 |
|
| 78 |
exception Poll |
| 79 |
|
| 80 |
(* set polling events; if the polling operation is not appropriate |
| 81 |
* for the underlying I/O device, then the Poll exception is raised. |
| 82 |
*) |
| 83 |
fun pollIn (PollDesc (iod, {wr, pri, ...}: poll_flags)) = |
| 84 |
PollDesc (iod, {rd=true, wr=wr, pri=pri}) |
| 85 |
fun pollOut (PollDesc (iod, {rd, pri, ...}: poll_flags)) = |
| 86 |
PollDesc (iod, {rd=rd, wr=true, pri=pri}) |
| 87 |
fun pollPri (PollDesc (iod, {rd, wr, ...}: poll_flags)) = |
| 88 |
PollDesc (iod, {rd=rd, wr=wr, pri=true}) |
| 89 |
|
| 90 |
(* polling function *) |
| 91 |
local |
| 92 |
structure Prim = PrimitiveFFI.OS.IO |
| 93 |
fun join (false, _, w) = w |
| 94 |
| join (true, b, w) = C_Short.orb(w, b) |
| 95 |
fun test (w, b) = (C_Short.andb(w, b) <> 0) |
| 96 |
val rdBit = PrimitiveFFI.OS.IO.POLLIN |
| 97 |
and wrBit = PrimitiveFFI.OS.IO.POLLOUT |
| 98 |
and priBit = PrimitiveFFI.OS.IO.POLLPRI |
| 99 |
fun fromPollDesc (PollDesc (iod, {rd, wr, pri})) = |
| 100 |
( iodToFd iod, |
| 101 |
join (rd, rdBit, |
| 102 |
join (wr, wrBit, |
| 103 |
join (pri, priBit, 0))) |
| 104 |
) |
| 105 |
fun toPollInfo (fd, i) = |
| 106 |
PollInfo (fdToIod fd, { |
| 107 |
rd = test(i, rdBit), |
| 108 |
wr = test(i, wrBit), |
| 109 |
pri = test(i, priBit) |
| 110 |
}) |
| 111 |
in |
| 112 |
fun poll (pds, timeOut) = let |
| 113 |
val (fds, events) = ListPair.unzip (List.map fromPollDesc pds) |
| 114 |
val fds = Vector.fromList fds |
| 115 |
val n = Vector.length fds |
| 116 |
val events = Vector.fromList events |
| 117 |
val timeOut = |
| 118 |
case timeOut of |
| 119 |
NONE => ~1 |
| 120 |
| SOME t => |
| 121 |
if Time.< (t, Time.zeroTime) |
| 122 |
then Error.raiseSys Error.inval |
| 123 |
else (C_Int.fromLarge (Time.toMilliseconds t) |
| 124 |
handle Overflow => Error.raiseSys Error.inval) |
| 125 |
val revents = Array.array (n, 0: C_Short.t) |
| 126 |
val _ = Posix.Error.SysCall.simple |
| 127 |
(fn () => Prim.poll (PrePosix.FileDesc.vectorToRep fds, |
| 128 |
events, |
| 129 |
C_NFds.fromInt n, |
| 130 |
timeOut, |
| 131 |
revents)) |
| 132 |
in |
| 133 |
Array.foldri |
| 134 |
(fn (i, w, l) => |
| 135 |
if w <> 0 |
| 136 |
then (toPollInfo (Vector.sub (fds, i), w))::l |
| 137 |
else l) |
| 138 |
[] |
| 139 |
revents |
| 140 |
end |
| 141 |
end (* local *) |
| 142 |
|
| 143 |
(* check for conditions *) |
| 144 |
fun isIn (PollInfo(_, flgs)) = #rd flgs |
| 145 |
fun isOut (PollInfo(_, flgs)) = #wr flgs |
| 146 |
fun isPri (PollInfo(_, flgs)) = #pri flgs |
| 147 |
fun infoToPollDesc (PollInfo arg) = PollDesc arg |
| 148 |
end (* OS_IO *) |
| 149 |
|
| 150 |
|
| 151 |
(* |
| 152 |
* $Log: os-io.sml, v $ |
| 153 |
* Revision 1.4 1997/07/31 17:25:26 jhr |
| 154 |
* We are now using 32-bit ints to represent the seconds portion of a |
| 155 |
* time value. This was required to handle the change in the type of |
| 156 |
* Time.{to, from}{Seconds, Milliseconds, Microseconds}. |
| 157 |
* |
| 158 |
* Revision 1.3 1997/06/07 15:27:51 jhr |
| 159 |
* SML'97 Basis Library changes (phase 3; Posix changes) |
| 160 |
* |
| 161 |
* Revision 1.2 1997/06/02 19:16:19 jhr |
| 162 |
* SML'97 Basis Library changes (phase 2) |
| 163 |
* |
| 164 |
* Revision 1.1.1.1 1997/01/14 01:38:25 george |
| 165 |
* Version 109.24 |
| 166 |
* |
| 167 |
*) |