io.txt 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. [This is somewhat out of date. It predates the switch to using optimistic
  2. concurrency for mutual exclusion. -RK 5/21/01]
  3. There are two types of I/O objects in Scheme 48, channels and ports.
  4. Channels are the raw, unbuffered ports of the operating system. The
  5. only I/O operations the VM supports for channels are block reads and
  6. writes. Ports are the actual Scheme ports and are implemented in Scheme,
  7. with some support from the VM for READ-CHAR, PEEK-CHAR, and WRITE-CHAR
  8. for efficiency. The run-time system provides ports that are buffered
  9. versions of channels. Other sorts of ports are in big/more-port.scm.
  10. Source files:
  11. rts/port.scm port operations and port handlers
  12. rts/current-port.scm current-input-port, etc.
  13. rts/channel.scm blocking on channels and handling i/o interrupts
  14. rts/channel-port.scm ports that read and write to channels
  15. rts/low.scm CHANNEL-READ and CHANNEL-WRITE
  16. big/more-port.scm additional kinds of ports
  17. vm/arch.scm fields of ports and channels
  18. vm/prim-io.scm VM i/o opcodes
  19. vm/vmio.scm implementation of channels
  20. ----------------------------------------------------------------
  21. CHANNELS
  22. The VM instructions that deal with channels are:
  23. (OPEN-CHANNEL <spec> <mode>) -> channel
  24. <mode> is a from the enumeration OPEN-CHANNEL-OPTION in arch.scm.
  25. <spec> is either a filename (as a string) or an OS port (as a one-word
  26. code-vector), depending on the mode.
  27. (CLOSE-CHANNEL <channel>) -> unspecific
  28. (CHANNEL-MAYBE-READ <string-or-code-vector> <start-index> <count> <wait?>
  29. <channel>)
  30. -> number of bytes read or the eof-object
  31. (CHANNEL-MAYBE-WRITE <string-or-code-vector> <start-index> <count> <channel>)
  32. -> number of bytes written
  33. These read or write up to the specified number of characters or bytes
  34. from or to the string or code-vector, with the first character or byte
  35. going at <start-index>.
  36. (CHANNEL-ABORT <channel>) -> number of bytes read or written or
  37. the eof-object
  38. This aborts any pending read or write operation on the channel. The return
  39. value reflects any partial completion.
  40. CHANNEL-MAYBE-READ and CHANNEL-MAYBE-WRITE do not block. If the read or
  41. write cannot be completed immediately a PENDING-CHANNEL-I/O exception is
  42. raised. It is then up to the run-time system to either wait or run some
  43. other thread. The VM raises an I/O-COMPLETION interrupt whenever an i/o
  44. operation completes.
  45. Because CHANNEL-MAYBE-READ and CHANNEL-MAYBE-WRITE are awkward to use,
  46. the RTS defines somewhat simpler versions:
  47. (CHANNEL-READ <buffer> <start> <needed> <channel>)
  48. -> number of bytes read or the eof-object
  49. (CHANNEL-WRITE <buffer> <start> <count> <channel>)
  50. -> unspecified
  51. <Buffer> is either a string or code vector and <start> is the index of the
  52. first character read or written. <Needed> is one of:
  53. N > 0 : the call returns when this many characters has been read or
  54. an EOF is reached.
  55. 'IMMEDIATE : the call reads as many characters as are available and
  56. returns immediately.
  57. 'ANY : the call returns as soon as at least one character has been read
  58. or an EOF is reached.
  59. <Count> is the number of characters to be written. CHANNEL-READ will read
  60. the requested number of characters unless an EOF is reached. CHANNEL-WRITE
  61. will write the requested number of characters.
  62. ----------------------------------------------------------------
  63. PORTS
  64. Ports are actual Scheme port and are (usually) buffered. They are fully
  65. exposed to the run-time system. The VM instructions on ports could be
  66. implemented in Scheme; they are in the VM for efficiency. Buffers are
  67. code-vectors (this is a micro-hack; strings have a slightly higher overhead
  68. because of the null terminating byte for C compatibility) (code-vectors are
  69. just vectors of bytes).
  70. The fields of a port are:
  71. PORT-STATUS: a bit set represented as a fixnum.
  72. Indices into this bit set are from the PORT-STATUS-OPTIONS
  73. enumeration in arch.scm. The current bits are: input, output,
  74. open-for-input, open-for-output (the last two are for things like
  75. sockets, on which you need to block but which do not support
  76. normal reading or writing).
  77. PORT-HANDLER: a record containing three procedures. These handle
  78. printing the port, closing the port, and filling (for input ports)
  79. or emptying (for output ports) buffers.
  80. PORT-DATA: ?
  81. Whatever stuff the handler needs.
  82. PORT-LOCKED?, PORT-LOCK: used by the system to guarentee the atomicity
  83. of i/o operations.
  84. PORT-BUFFER: a code-vector. The input or output buffer of the port.
  85. PORT-INDEX: a fixnum. The index of the next byte to read or written.
  86. PORT-LIMIT: a fixnum. One past the end of the valid/available buffer space.
  87. PORT-PENDING-EOF?: true if the next read to this port should return EOF.
  88. Additional operations on ports:
  89. (READ-BLOCK string-or-code-vector start count input-port)
  90. Read COUNT bytes into STRING-OR-CODE-VECTOR starting at index START.
  91. Returns the number of bytes read. Only an end-of-file will prevent
  92. the requested number of bytes from being read.
  93. (WRITE-STRING string output-port)
  94. Write the characters in the string to the port.
  95. (WRITE-BLOCK string-or-code-vector start count output-port)
  96. The output counterpart to READ-BLOCK. This always writes out the
  97. requested number of bytes. Its return value is unspecified.
  98. (FORCE_OUTPUT output-port)
  99. Causes any buffered characters to be written out.
  100. (CURRENT-ERROR-PORT)
  101. The current error port, analogous to Scheme's CURRENT-INPUT-PORT
  102. and CURRENT-OUTPUT-PORT.
  103. The system maintains a list of output ports whose buffers should be
  104. periodically flushed. The default output port and ports made by
  105. OPEN-OUTPUT-FILE are on this list. (PERIODICALLY-FORCE-OUTPUT! <output-port>)
  106. may be used to add others.
  107. ----------------------------------------------------------------
  108. PORT HANDLERS
  109. Every port has a handler with three procedures. The first two are used
  110. for printing and closing ports and have the same type for all ports:
  111. (DISCLOSE port-data) -> disclose list
  112. (CLOSE port-data) -> unspecific
  113. For CLOSE, The system takes care of modifying the port's status.
  114. The third procedure is used to fill and empty buffers. Its arguments
  115. and return values depend on the kind of port:
  116. Buffered output ports:
  117. (BUFFER-PROC port-data buffer start-index byte-count) -> unspecific
  118. BYTE-COUNT bytes should be copied from the buffer beginning at
  119. START-INDEX. The buffer may be either a string or a code-vector.
  120. Unbuffered output ports:
  121. (BUFFER-PROC port-data char) -> unspecific
  122. Write out the given character. The system uses this for the default
  123. error port.
  124. Input ports:
  125. (BUFFER-PROC data buffer start-index needed-bytes)
  126. -> EOF or number of bytes read (before an EOF)
  127. Bytes should be copied into the buffer starting at START-INDEX. The
  128. buffer may be either a string or a code-vector. NEEDED-BYTES is one of:
  129. 'IMMEDIATE
  130. The call should return immediately after transfering whatever number
  131. of bytes are currently available, possibly none (this is used for
  132. CHAR-READY?). The maximum number of characters is determined by the
  133. length of BUFFER.
  134. 'ANY
  135. The call should wait until at least one byte is available or an EOF
  136. occurs (used for READ-CHAR and PEEK-CHAR). The maximum number of
  137. characters is determined by the length of BUFFER.
  138. N > 0
  139. The call should wait until N bytes have been copied into the buffer
  140. or an EOF occurs. If the return value is less than NEEDED-BYTES the
  141. port code inserts an EOF after the last byte.
  142. ----------------------------------------------------------------
  143. Ports and the Virtual Machine
  144. Ports could be implemented entirely in Scheme, with no support from
  145. the VM. For efficiency reasons VM instructions are supplied for
  146. three port operations:
  147. (READ-CHAR <port>)
  148. (PEEK-CHAR <port>)
  149. (WRITE-CHAR <char> <port>)
  150. For each of these, if there is sufficient data or space in the
  151. appropriate buffer the VM performs the operation. Otherwise a
  152. buffer-full/empty exception is raised and the exception handler
  153. uses the buffer procedure from the port's handler to fill or
  154. empty the buffer.