qio 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. .TH QIO 9
  2. .SH NAME
  3. qio: qget, qdiscard, qconsume, qpass, qproduce, qcopy, qopen, qbread, qread, qbwrite, qwrite, qiwrite, qfree, qclose, qhangup, qreopen, qlen, qwindow, qcanread, qsetlimit, qnoblock, qflush, qfull \- queued I/O for devices
  4. .SH SYNOPSIS
  5. .ta \w'\fLQueue* 'u
  6. .B
  7. Queue* qopen(int limit,int msg, void (*kick)(void*),void *arg)
  8. .PP
  9. .B
  10. void qhangup(Queue *q, char *reason)
  11. .PP
  12. .B
  13. void qclose(Queue *q)
  14. .PP
  15. .B
  16. void qreopen(Queue *q)
  17. .PP
  18. .B
  19. void qfree(Queue *q)
  20. .PP
  21. .B
  22. long qbwrite(Queue *q, Block *b)
  23. .PP
  24. .B
  25. long qwrite(Queue *q, void *buf, int len)
  26. .PP
  27. .B
  28. int qpass(Queue *q, Block *b)
  29. .PP
  30. .B
  31. int qpassnolim(Queue *q, Block *b)
  32. .PP
  33. .B
  34. int qproduce(Queue *q, void *buf, int len)
  35. .PP
  36. .B
  37. int qiwrite(Queue *q, void *buf, int len)
  38. .PP
  39. .B
  40. Block* qbread(Queue *q, int len)
  41. .PP
  42. .B
  43. long qread(Queue *q, void *buf, int len)
  44. .PP
  45. .B
  46. Block* qcopy(Queue *q, int len, ulong offset)
  47. .PP
  48. .B
  49. Block* qget(Queue *q)
  50. .PP
  51. .B
  52. int qconsume(Queue *q, void *buf, int len)
  53. .PP
  54. .B
  55. int qdiscard(Queue *q, int len)
  56. .PP
  57. .B
  58. void qflush(Queue *q)
  59. .PP
  60. .B
  61. int qlen(Queue *q)
  62. .PP
  63. .B
  64. int qwindow(Queue *q)
  65. .PP
  66. .B
  67. int qcanread(Queue *q)
  68. .PP
  69. .B
  70. void qsetlimit(Queue *q, int limit)
  71. .PP
  72. .B
  73. void qnoblock(Queue *q, int nonblock)
  74. .PP
  75. .B
  76. int qfull(Queue *q)
  77. .SH DESCRIPTION
  78. This suite of functions provides serial data buffering for device drivers.
  79. Data is stored in a
  80. .B Queue
  81. structure as a sequence of variable-sized
  82. .BR Blocks ;
  83. see
  84. .IR allocb (9).
  85. .PP
  86. .I Qopen
  87. initialises and returns a pointer to a new
  88. .BR Queue ,
  89. configuring it according to the following parameters:
  90. .TF limit
  91. .PD
  92. .TP
  93. .I limit
  94. Set the queue limit (high water mark) in bytes.
  95. .TP
  96. .I msg
  97. Set message mode if non-zero; otherwise, stream mode (discussed below).
  98. .TP
  99. .I kick
  100. Optional flow-control function called by
  101. .I qbread
  102. to restart writers, and by
  103. .I qbwrite
  104. (also
  105. .IR qiwrite )
  106. to restart readers.
  107. .TP
  108. .I arg
  109. Argument to pass to
  110. .I kick
  111. .PP
  112. .I Qhangup
  113. marks
  114. .I q
  115. as `hung up'
  116. for the given
  117. .IR reason
  118. .RB ( Ehungup
  119. by default).
  120. Subsequent attempts to write to the queue raise an
  121. .IR error (9).
  122. .I Qhangup
  123. does not flush the queue: subsequent read requests are
  124. handled normally until the queue empties.
  125. .I Qread
  126. and the other functions then return their conventional values
  127. for a hungup stream: 0, -1 or a null pointer, depending on the function.
  128. After a few such attempts by any process, an
  129. .IR error (9)
  130. is raised (typically
  131. .BR Ehungup )
  132. on each subsequent read.
  133. .PP
  134. If queued data is left unread, and not flushed by
  135. .I qflush
  136. or
  137. .IR qclose ,
  138. the data will again be readable following a subsequent
  139. .IR qreopen .
  140. .PP
  141. .I Qclose
  142. also marks a given
  143. .I q
  144. as `hung up',
  145. but removes and frees any queued data Blocks.
  146. .I Qclose
  147. ignores calls when
  148. .I q
  149. is null.
  150. .PP
  151. .I Qreopen
  152. makes a closed or hung up queue available for use again.
  153. The queue's data limit is reset to the
  154. .I limit
  155. value given when the queue was first created by
  156. .IR qopen ,
  157. cancelling the effect of any previous call to
  158. .IR qsetlimit .
  159. .PP
  160. .I Qfree
  161. closes
  162. .I q
  163. with
  164. .I qclose
  165. and frees it.
  166. The caller must ensure that no references remain;
  167. these functions do not keep a reference count.
  168. .SS "Flow control"
  169. The queue I/O routines provide a flow control mechanism to coordinate producers and consumers.
  170. Each queue has a limit on the number of bytes queued, its `high water mark',
  171. initially set when the queue is created, but adjustable by
  172. .IR qsetlimit ,
  173. below.
  174. The low water mark is not set explicitly:
  175. it is always half the current queue limit.
  176. When the high water mark is exceeded, writes normally block until a reader drains the
  177. queue below its low water mark; the writer is then allowed to proceed.
  178. Conversely, readers normally block when the queue is empty, until a writer
  179. arrives with data, or the queue is closed.
  180. .PP
  181. A queue can be given a
  182. .I kick
  183. function when the queue is created by
  184. .IR qopen .
  185. The function is invoked by
  186. .IR qread
  187. and
  188. .IR qbread ,
  189. to prod an output routine when the queue falls below the low-water mark, and by
  190. .IR qwrite ,
  191. .IR qbwrite
  192. and
  193. .IR qiwrite ,
  194. to notify a reader that a queue is no longer empty.
  195. Because
  196. .I kick
  197. is called from the reading (or writing) process, or an interrupt handler, it
  198. must not block.
  199. .PP
  200. Interrupt handlers must not
  201. .IR sleep (9),
  202. and are therefore restricted to using only the non-blocking functions described below.
  203. .SS "Stream mode and message mode"
  204. In stream mode,
  205. no read will return more than one
  206. block
  207. of data, but
  208. a read can split a block that contains more data than requested, leaving the remainder
  209. in a new block at the front of the Queue.
  210. Writes of more than the maximum
  211. .B Block
  212. size (currently 128k bytes)
  213. are split into as many Blocks as required, each written separately to the queue,
  214. in order, but with possible flow-control between them.
  215. The queue is locked meanwhile, however, so that data from other writers is not intermingled.
  216. .PP
  217. In message mode, by contrast, a read will return at most
  218. one block's worth of data, but the remainder of a partially-read block will be discarded,
  219. not returned to the queue.
  220. If a write count exceeds the maximum
  221. .B Block
  222. size, the excess data is discarded:
  223. at most a single block can be queued.
  224. .PP
  225. The mode of the queue should be taken into account in the descriptions below
  226. of the following functions:
  227. .IR qwrite ,
  228. .IR qiwrite ,
  229. .IR qbread
  230. and
  231. .IR qconsume .
  232. No other functions are aware of the distinction.
  233. .SS "Write operations (flow controlled)"
  234. .I Qwrite
  235. copies
  236. .I len
  237. bytes of data from
  238. .I buf
  239. into one or more
  240. .B Blocks
  241. which it places on the
  242. .IR q .
  243. .I Qwrite
  244. always returns
  245. .IR len .
  246. It can implement message mode.
  247. .PP
  248. .I Qbwrite
  249. places the single Block
  250. .I b
  251. on the tail of
  252. .IR q ,
  253. waking any sleeping reader.
  254. If the queue is full, the
  255. writing process blocks until a reader
  256. has reduced the queued data to
  257. the low-water mark;
  258. if the queue is non-blocking
  259. (see
  260. .I qnoblock
  261. below),
  262. the data is discarded without notice.
  263. .I Qbwrite
  264. normally returns
  265. .IR len ,
  266. but raises an
  267. .IR error (9)
  268. if the queue is closed (see
  269. .I qhangup
  270. and
  271. .IR qclose ).
  272. The block
  273. .I b
  274. is always freed.
  275. Note that
  276. .I b
  277. can be empty (zero-length), to punctuate the data in a queue.
  278. .I Qbwrite
  279. cannot handle a list of Blocks;
  280. .I qpass
  281. must be used instead.
  282. .SS Non-blocking writes
  283. .PP
  284. .I Qproduce
  285. returns -1immediately if
  286. .I q
  287. is full.
  288. Otherwise, it queues
  289. .I len
  290. bytes of data from
  291. .I buf
  292. in a single
  293. .B Block
  294. on
  295. .I q
  296. and returns the number of bytes written.
  297. .PP
  298. .I Qpass
  299. attempts to place the list of Blocks headed by
  300. .I b
  301. on
  302. .IR q ,
  303. returning the number of bytes written if successful.
  304. If
  305. .I q
  306. was full, it
  307. frees the Block list
  308. .I b
  309. and returns -1.
  310. .PP
  311. .I Qpassnolim
  312. puts the Block list
  313. .I b
  314. on
  315. .I q
  316. regardless of flow control; it returns the number of bytes in the list
  317. .IR b .
  318. .PP
  319. .I Qiwrite
  320. is a variant of
  321. .I qwrite
  322. used exclusively by the kernel print function,
  323. to allow printing by interrupt handlers;
  324. .I qiwrite
  325. could be used with care by other routines, but
  326. .IR qproduce
  327. is preferable.
  328. .I Qiwrite
  329. writes the
  330. .I len
  331. bytes of data at
  332. .I buf
  333. into the
  334. .I q
  335. without regard to flow control;
  336. the writer never blocks.
  337. The queue is assumed to be open.
  338. .I Qiwrite
  339. always returns
  340. .IR len .
  341. It can implement message mode.
  342. .SS "Read operations (flow controlled)"
  343. .I Qbread
  344. blocks until data arrives on
  345. .IR q ,
  346. then
  347. returns the first
  348. .BR Block ;
  349. it limits the data returned
  350. to
  351. .I len
  352. bytes (in the manner depending on the mode of
  353. .IR q ).
  354. It returns a null pointer if the queue has hung up.
  355. .PP
  356. .I Qread
  357. reads a Block of up to
  358. .I len
  359. bytes from
  360. .I q
  361. using
  362. .IR qbread ,
  363. and copies the data in the Block into
  364. .IR buf ,
  365. then frees the Block and returns
  366. the number of bytes read.
  367. .I Qread
  368. returns 0 on end of file or error (hangup).
  369. It can implement message mode.
  370. .PP
  371. .I Qcopy
  372. returns a Block with a copy of data from the queue (the data remains on the queue).
  373. The copy begins
  374. .I offset
  375. bytes into the queue's data and proceeds until
  376. .I len
  377. bytes have been copied or no more data remains.
  378. The Block's read and write pointers delimit the data copied into it.
  379. .I Qcopy
  380. can be used by a reliable transport protocol to copy a packet for transmission,
  381. leaving the data queued for possible retransmission, if unacknowledged.
  382. .SS Non-blocking reads
  383. .PP
  384. .I Qconsume
  385. returns -1 immediately if
  386. .I q
  387. is empty.
  388. Otherwise, it
  389. copies up to
  390. .I len
  391. bytes from the first
  392. .B Block
  393. on the queue into
  394. .IR buf ,
  395. returning the number of bytes copied.
  396. It can implement message mode.
  397. .PP
  398. .I Qget
  399. returns a null pointer immediately if
  400. .I q
  401. is empty or closed.
  402. Otherwise, it
  403. returns the first
  404. .B Block
  405. on the queue.
  406. .SS "Discard and flush"
  407. .I Qdiscard
  408. removes the first
  409. .I len
  410. data bytes from
  411. .IR q ;
  412. it returns the number of bytes actually discarded, in case
  413. the queue is shorter than
  414. .IR len .
  415. If the queue drains below the low-water mark,
  416. .I qdiscard
  417. wakes any sleeping writers.
  418. Since it does not block,
  419. .I qdiscard
  420. can safely be called from interrupt handlers.
  421. It is useful in transport protocol drivers to remove data from the queue
  422. once acknowledged.
  423. .PP
  424. .I Qflush
  425. discards all data waiting on
  426. .IR q ,
  427. waking any waiting writer.
  428. .SS "Queue status"
  429. The following functions return a Queue's status.
  430. Note that between a call to one of these functions and another operation,
  431. the state can change if a driver allows concurrent access by
  432. either another process or an interrupt handler.
  433. .PP
  434. .I Qlen
  435. returns the number of bytes queued on
  436. .IR q .
  437. .PP
  438. .I Qwindow
  439. returns the number of bytes that can be written before reaching the queue's high-water mark.
  440. A return of 0 means that a write operation will certainly block;
  441. a non-zero return gives no guarantees (see
  442. .IR qfull ,
  443. below).
  444. .PP
  445. .I Qcanread
  446. returns 1 if any data queued is queued. A subsequent read operation will not block.
  447. .PP
  448. .I Qfull
  449. returns non-zero if
  450. .I q
  451. is flow-controlled and a write would block or a non-blocking write would return an error.
  452. (Note that the implementation allows
  453. .I qwindow
  454. to return non-zero yet
  455. .I qfull
  456. to return true.)
  457. .SS "Queue control"
  458. .I Qsetlimit
  459. sets the high water mark for the queue to
  460. .IR limit .
  461. Note that
  462. .I qopen
  463. saves the initial queue limit.
  464. If the queue is closed and reopened (by
  465. .IR qreopen )
  466. that initial limit is restored.
  467. .PP
  468. .I Qnoblock
  469. sets or resets non-blocking mode.
  470. If
  471. .I nonblock
  472. is non-zero,
  473. the queue becomes non-blocking, and
  474. data written to a queue beyond its high water mark is discarded
  475. by calls that would otherwise block.
  476. .SH SOURCE
  477. .B /sys/src/9/port/qio.c
  478. .SH SEE ALSO
  479. .IR allocb (9),
  480. .IR ref (9)