qio.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. static ulong padblockcnt;
  8. static ulong concatblockcnt;
  9. static ulong pullupblockcnt;
  10. static ulong copyblockcnt;
  11. static ulong consumecnt;
  12. static ulong producecnt;
  13. static ulong qcopycnt;
  14. static int debugging;
  15. #define QDEBUG if(0)
  16. /*
  17. * IO queues
  18. */
  19. typedef struct Queue Queue;
  20. struct Queue
  21. {
  22. Lock;
  23. Block* bfirst; /* buffer */
  24. Block* blast;
  25. int len; /* bytes allocated to queue */
  26. int dlen; /* data bytes in queue */
  27. int limit; /* max bytes in queue */
  28. int inilim; /* initial limit */
  29. int state;
  30. int noblock; /* true if writes return immediately when q full */
  31. int eof; /* number of eofs read by user */
  32. void (*kick)(void*); /* restart output */
  33. void (*bypass)(void*, Block*); /* bypass queue altogether */
  34. void* arg; /* argument to kick */
  35. QLock rlock; /* mutex for reading processes */
  36. Rendez rr; /* process waiting to read */
  37. QLock wlock; /* mutex for writing processes */
  38. Rendez wr; /* process waiting to write */
  39. char err[ERRMAX];
  40. };
  41. enum
  42. {
  43. Maxatomic = 64*1024,
  44. };
  45. uint qiomaxatomic = Maxatomic;
  46. void
  47. ixsummary(void)
  48. {
  49. debugging ^= 1;
  50. iallocsummary();
  51. print("pad %lud, concat %lud, pullup %lud, copy %lud\n",
  52. padblockcnt, concatblockcnt, pullupblockcnt, copyblockcnt);
  53. print("consume %lud, produce %lud, qcopy %lud\n",
  54. consumecnt, producecnt, qcopycnt);
  55. }
  56. /*
  57. * free a list of blocks
  58. */
  59. void
  60. freeblist(Block *b)
  61. {
  62. Block *next;
  63. for(; b != 0; b = next){
  64. next = b->next;
  65. b->next = 0;
  66. freeb(b);
  67. }
  68. }
  69. /*
  70. * pad a block to the front (or the back if size is negative)
  71. */
  72. Block*
  73. padblock(Block *bp, int size)
  74. {
  75. int n;
  76. Block *nbp;
  77. QDEBUG checkb(bp, "padblock 1");
  78. if(size >= 0){
  79. if(bp->rp - bp->base >= size){
  80. bp->rp -= size;
  81. return bp;
  82. }
  83. if(bp->next)
  84. panic("padblock 0x%luX", getcallerpc(&bp));
  85. n = BLEN(bp);
  86. padblockcnt++;
  87. nbp = allocb(size+n);
  88. nbp->rp += size;
  89. nbp->wp = nbp->rp;
  90. memmove(nbp->wp, bp->rp, n);
  91. nbp->wp += n;
  92. freeb(bp);
  93. nbp->rp -= size;
  94. } else {
  95. size = -size;
  96. if(bp->next)
  97. panic("padblock 0x%luX", getcallerpc(&bp));
  98. if(bp->lim - bp->wp >= size)
  99. return bp;
  100. n = BLEN(bp);
  101. padblockcnt++;
  102. nbp = allocb(size+n);
  103. memmove(nbp->wp, bp->rp, n);
  104. nbp->wp += n;
  105. freeb(bp);
  106. }
  107. QDEBUG checkb(nbp, "padblock 1");
  108. return nbp;
  109. }
  110. /*
  111. * return count of bytes in a string of blocks
  112. */
  113. int
  114. blocklen(Block *bp)
  115. {
  116. int len;
  117. len = 0;
  118. while(bp) {
  119. len += BLEN(bp);
  120. bp = bp->next;
  121. }
  122. return len;
  123. }
  124. /*
  125. * return count of space in blocks
  126. */
  127. int
  128. blockalloclen(Block *bp)
  129. {
  130. int len;
  131. len = 0;
  132. while(bp) {
  133. len += BALLOC(bp);
  134. bp = bp->next;
  135. }
  136. return len;
  137. }
  138. /*
  139. * copy the string of blocks into
  140. * a single block and free the string
  141. */
  142. Block*
  143. concatblock(Block *bp)
  144. {
  145. int len;
  146. Block *nb, *f;
  147. if(bp->next == 0)
  148. return bp;
  149. nb = allocb(blocklen(bp));
  150. for(f = bp; f; f = f->next) {
  151. len = BLEN(f);
  152. memmove(nb->wp, f->rp, len);
  153. nb->wp += len;
  154. }
  155. concatblockcnt += BLEN(nb);
  156. freeblist(bp);
  157. QDEBUG checkb(nb, "concatblock 1");
  158. return nb;
  159. }
  160. /*
  161. * make sure the first block has at least n bytes
  162. */
  163. Block*
  164. pullupblock(Block *bp, int n)
  165. {
  166. int i;
  167. Block *nbp;
  168. /*
  169. * this should almost always be true, it's
  170. * just to avoid every caller checking.
  171. */
  172. if(BLEN(bp) >= n)
  173. return bp;
  174. /*
  175. * if not enough room in the first block,
  176. * add another to the front of the list.
  177. */
  178. if(bp->lim - bp->rp < n){
  179. nbp = allocb(n);
  180. nbp->next = bp;
  181. bp = nbp;
  182. }
  183. /*
  184. * copy bytes from the trailing blocks into the first
  185. */
  186. n -= BLEN(bp);
  187. while(nbp = bp->next){
  188. i = BLEN(nbp);
  189. if(i > n) {
  190. memmove(bp->wp, nbp->rp, n);
  191. pullupblockcnt++;
  192. bp->wp += n;
  193. nbp->rp += n;
  194. QDEBUG checkb(bp, "pullupblock 1");
  195. return bp;
  196. } else {
  197. /* shouldn't happen but why crash if it does */
  198. if(i < 0){
  199. print("pullup negative length packet, called from 0x%p\n",
  200. getcallerpc(&bp));
  201. i = 0;
  202. }
  203. memmove(bp->wp, nbp->rp, i);
  204. pullupblockcnt++;
  205. bp->wp += i;
  206. bp->next = nbp->next;
  207. nbp->next = 0;
  208. freeb(nbp);
  209. n -= i;
  210. if(n == 0){
  211. QDEBUG checkb(bp, "pullupblock 2");
  212. return bp;
  213. }
  214. }
  215. }
  216. freeb(bp);
  217. return 0;
  218. }
  219. /*
  220. * make sure the first block has at least n bytes
  221. */
  222. Block*
  223. pullupqueue(Queue *q, int n)
  224. {
  225. Block *b;
  226. if(BLEN(q->bfirst) >= n)
  227. return q->bfirst;
  228. q->bfirst = pullupblock(q->bfirst, n);
  229. for(b = q->bfirst; b != nil && b->next != nil; b = b->next)
  230. ;
  231. q->blast = b;
  232. return q->bfirst;
  233. }
  234. /*
  235. * trim to len bytes starting at offset
  236. */
  237. Block *
  238. trimblock(Block *bp, int offset, int len)
  239. {
  240. ulong l;
  241. Block *nb, *startb;
  242. QDEBUG checkb(bp, "trimblock 1");
  243. if(blocklen(bp) < offset+len) {
  244. freeblist(bp);
  245. return nil;
  246. }
  247. while((l = BLEN(bp)) < offset) {
  248. offset -= l;
  249. nb = bp->next;
  250. bp->next = nil;
  251. freeb(bp);
  252. bp = nb;
  253. }
  254. startb = bp;
  255. bp->rp += offset;
  256. while((l = BLEN(bp)) < len) {
  257. len -= l;
  258. bp = bp->next;
  259. }
  260. bp->wp -= (BLEN(bp) - len);
  261. if(bp->next) {
  262. freeblist(bp->next);
  263. bp->next = nil;
  264. }
  265. return startb;
  266. }
  267. /*
  268. * copy 'count' bytes into a new block
  269. */
  270. Block*
  271. copyblock(Block *bp, int count)
  272. {
  273. int l;
  274. Block *nbp;
  275. QDEBUG checkb(bp, "copyblock 0");
  276. nbp = allocb(count);
  277. for(; count > 0 && bp != 0; bp = bp->next){
  278. l = BLEN(bp);
  279. if(l > count)
  280. l = count;
  281. memmove(nbp->wp, bp->rp, l);
  282. nbp->wp += l;
  283. count -= l;
  284. }
  285. if(count > 0){
  286. memset(nbp->wp, 0, count);
  287. nbp->wp += count;
  288. }
  289. copyblockcnt++;
  290. QDEBUG checkb(nbp, "copyblock 1");
  291. return nbp;
  292. }
  293. Block*
  294. adjustblock(Block* bp, int len)
  295. {
  296. int n;
  297. Block *nbp;
  298. if(len < 0){
  299. freeb(bp);
  300. return nil;
  301. }
  302. if(bp->rp+len > bp->lim){
  303. nbp = copyblock(bp, len);
  304. freeblist(bp);
  305. QDEBUG checkb(nbp, "adjustblock 1");
  306. return nbp;
  307. }
  308. n = BLEN(bp);
  309. if(len > n)
  310. memset(bp->wp, 0, len-n);
  311. bp->wp = bp->rp+len;
  312. QDEBUG checkb(bp, "adjustblock 2");
  313. return bp;
  314. }
  315. /*
  316. * throw away up to count bytes from a
  317. * list of blocks. Return count of bytes
  318. * thrown away.
  319. */
  320. int
  321. pullblock(Block **bph, int count)
  322. {
  323. Block *bp;
  324. int n, bytes;
  325. bytes = 0;
  326. if(bph == nil)
  327. return 0;
  328. while(*bph != nil && count != 0) {
  329. bp = *bph;
  330. n = BLEN(bp);
  331. if(count < n)
  332. n = count;
  333. bytes += n;
  334. count -= n;
  335. bp->rp += n;
  336. QDEBUG checkb(bp, "pullblock ");
  337. if(BLEN(bp) == 0) {
  338. *bph = bp->next;
  339. bp->next = nil;
  340. freeb(bp);
  341. }
  342. }
  343. return bytes;
  344. }
  345. /*
  346. * get next block from a queue, return null if nothing there
  347. */
  348. Block*
  349. qget(Queue *q)
  350. {
  351. int dowakeup;
  352. Block *b;
  353. /* sync with qwrite */
  354. ilock(q);
  355. b = q->bfirst;
  356. if(b == nil){
  357. q->state |= Qstarve;
  358. iunlock(q);
  359. return nil;
  360. }
  361. q->bfirst = b->next;
  362. b->next = 0;
  363. q->len -= BALLOC(b);
  364. q->dlen -= BLEN(b);
  365. QDEBUG checkb(b, "qget");
  366. /* if writer flow controlled, restart */
  367. if((q->state & Qflow) && q->len < q->limit/2){
  368. q->state &= ~Qflow;
  369. dowakeup = 1;
  370. } else
  371. dowakeup = 0;
  372. iunlock(q);
  373. if(dowakeup)
  374. wakeup(&q->wr);
  375. return b;
  376. }
  377. /*
  378. * throw away the next 'len' bytes in the queue
  379. */
  380. int
  381. qdiscard(Queue *q, int len)
  382. {
  383. Block *b;
  384. int dowakeup, n, sofar;
  385. ilock(q);
  386. for(sofar = 0; sofar < len; sofar += n){
  387. b = q->bfirst;
  388. if(b == nil)
  389. break;
  390. QDEBUG checkb(b, "qdiscard");
  391. n = BLEN(b);
  392. if(n <= len - sofar){
  393. q->bfirst = b->next;
  394. b->next = 0;
  395. q->len -= BALLOC(b);
  396. q->dlen -= BLEN(b);
  397. freeb(b);
  398. } else {
  399. n = len - sofar;
  400. b->rp += n;
  401. q->dlen -= n;
  402. }
  403. }
  404. /*
  405. * if writer flow controlled, restart
  406. *
  407. * This used to be
  408. * q->len < q->limit/2
  409. * but it slows down tcp too much for certain write sizes.
  410. * I really don't understand it completely. It may be
  411. * due to the queue draining so fast that the transmission
  412. * stalls waiting for the app to produce more data. - presotto
  413. */
  414. if((q->state & Qflow) && q->len < q->limit){
  415. q->state &= ~Qflow;
  416. dowakeup = 1;
  417. } else
  418. dowakeup = 0;
  419. iunlock(q);
  420. if(dowakeup)
  421. wakeup(&q->wr);
  422. return sofar;
  423. }
  424. /*
  425. * Interrupt level copy out of a queue, return # bytes copied.
  426. */
  427. int
  428. qconsume(Queue *q, void *vp, int len)
  429. {
  430. Block *b;
  431. int n, dowakeup;
  432. uchar *p = vp;
  433. Block *tofree = nil;
  434. /* sync with qwrite */
  435. ilock(q);
  436. for(;;) {
  437. b = q->bfirst;
  438. if(b == 0){
  439. q->state |= Qstarve;
  440. iunlock(q);
  441. return -1;
  442. }
  443. QDEBUG checkb(b, "qconsume 1");
  444. n = BLEN(b);
  445. if(n > 0)
  446. break;
  447. q->bfirst = b->next;
  448. q->len -= BALLOC(b);
  449. /* remember to free this */
  450. b->next = tofree;
  451. tofree = b;
  452. };
  453. if(n < len)
  454. len = n;
  455. memmove(p, b->rp, len);
  456. consumecnt += n;
  457. b->rp += len;
  458. q->dlen -= len;
  459. /* discard the block if we're done with it */
  460. if((q->state & Qmsg) || len == n){
  461. q->bfirst = b->next;
  462. b->next = 0;
  463. q->len -= BALLOC(b);
  464. q->dlen -= BLEN(b);
  465. /* remember to free this */
  466. b->next = tofree;
  467. tofree = b;
  468. }
  469. /* if writer flow controlled, restart */
  470. if((q->state & Qflow) && q->len < q->limit/2){
  471. q->state &= ~Qflow;
  472. dowakeup = 1;
  473. } else
  474. dowakeup = 0;
  475. iunlock(q);
  476. if(dowakeup)
  477. wakeup(&q->wr);
  478. if(tofree != nil)
  479. freeblist(tofree);
  480. return len;
  481. }
  482. int
  483. qpass(Queue *q, Block *b)
  484. {
  485. int dlen, len, dowakeup;
  486. /* sync with qread */
  487. dowakeup = 0;
  488. ilock(q);
  489. if(q->len >= q->limit){
  490. freeblist(b);
  491. iunlock(q);
  492. return -1;
  493. }
  494. if(q->state & Qclosed){
  495. len = BALLOC(b);
  496. freeblist(b);
  497. iunlock(q);
  498. return len;
  499. }
  500. /* add buffer to queue */
  501. if(q->bfirst)
  502. q->blast->next = b;
  503. else
  504. q->bfirst = b;
  505. len = BALLOC(b);
  506. dlen = BLEN(b);
  507. QDEBUG checkb(b, "qpass");
  508. while(b->next){
  509. b = b->next;
  510. QDEBUG checkb(b, "qpass");
  511. len += BALLOC(b);
  512. dlen += BLEN(b);
  513. }
  514. q->blast = b;
  515. q->len += len;
  516. q->dlen += dlen;
  517. if(q->len >= q->limit/2)
  518. q->state |= Qflow;
  519. if(q->state & Qstarve){
  520. q->state &= ~Qstarve;
  521. dowakeup = 1;
  522. }
  523. iunlock(q);
  524. if(dowakeup)
  525. wakeup(&q->rr);
  526. return len;
  527. }
  528. int
  529. qpassnolim(Queue *q, Block *b)
  530. {
  531. int dlen, len, dowakeup;
  532. /* sync with qread */
  533. dowakeup = 0;
  534. ilock(q);
  535. if(q->state & Qclosed){
  536. freeblist(b);
  537. iunlock(q);
  538. return BALLOC(b);
  539. }
  540. /* add buffer to queue */
  541. if(q->bfirst)
  542. q->blast->next = b;
  543. else
  544. q->bfirst = b;
  545. len = BALLOC(b);
  546. dlen = BLEN(b);
  547. QDEBUG checkb(b, "qpass");
  548. while(b->next){
  549. b = b->next;
  550. QDEBUG checkb(b, "qpass");
  551. len += BALLOC(b);
  552. dlen += BLEN(b);
  553. }
  554. q->blast = b;
  555. q->len += len;
  556. q->dlen += dlen;
  557. if(q->len >= q->limit/2)
  558. q->state |= Qflow;
  559. if(q->state & Qstarve){
  560. q->state &= ~Qstarve;
  561. dowakeup = 1;
  562. }
  563. iunlock(q);
  564. if(dowakeup)
  565. wakeup(&q->rr);
  566. return len;
  567. }
  568. /*
  569. * if the allocated space is way out of line with the used
  570. * space, reallocate to a smaller block
  571. */
  572. Block*
  573. packblock(Block *bp)
  574. {
  575. Block **l, *nbp;
  576. int n;
  577. for(l = &bp; *l; l = &(*l)->next){
  578. nbp = *l;
  579. n = BLEN(nbp);
  580. if((n<<2) < BALLOC(nbp)){
  581. *l = allocb(n);
  582. memmove((*l)->wp, nbp->rp, n);
  583. (*l)->wp += n;
  584. (*l)->next = nbp->next;
  585. freeb(nbp);
  586. }
  587. }
  588. return bp;
  589. }
  590. int
  591. qproduce(Queue *q, void *vp, int len)
  592. {
  593. Block *b;
  594. int dowakeup;
  595. uchar *p = vp;
  596. /* sync with qread */
  597. dowakeup = 0;
  598. ilock(q);
  599. /* no waiting receivers, room in buffer? */
  600. if(q->len >= q->limit){
  601. q->state |= Qflow;
  602. iunlock(q);
  603. return -1;
  604. }
  605. /* save in buffer */
  606. b = iallocb(len);
  607. if(b == 0){
  608. iunlock(q);
  609. return 0;
  610. }
  611. memmove(b->wp, p, len);
  612. producecnt += len;
  613. b->wp += len;
  614. if(q->bfirst)
  615. q->blast->next = b;
  616. else
  617. q->bfirst = b;
  618. q->blast = b;
  619. /* b->next = 0; done by iallocb() */
  620. q->len += BALLOC(b);
  621. q->dlen += BLEN(b);
  622. QDEBUG checkb(b, "qproduce");
  623. if(q->state & Qstarve){
  624. q->state &= ~Qstarve;
  625. dowakeup = 1;
  626. }
  627. if(q->len >= q->limit)
  628. q->state |= Qflow;
  629. iunlock(q);
  630. if(dowakeup)
  631. wakeup(&q->rr);
  632. return len;
  633. }
  634. /*
  635. * copy from offset in the queue
  636. */
  637. Block*
  638. qcopy(Queue *q, int len, ulong offset)
  639. {
  640. int sofar;
  641. int n;
  642. Block *b, *nb;
  643. uchar *p;
  644. nb = allocb(len);
  645. ilock(q);
  646. /* go to offset */
  647. b = q->bfirst;
  648. for(sofar = 0; ; sofar += n){
  649. if(b == nil){
  650. iunlock(q);
  651. return nb;
  652. }
  653. n = BLEN(b);
  654. if(sofar + n > offset){
  655. p = b->rp + offset - sofar;
  656. n -= offset - sofar;
  657. break;
  658. }
  659. QDEBUG checkb(b, "qcopy");
  660. b = b->next;
  661. }
  662. /* copy bytes from there */
  663. for(sofar = 0; sofar < len;){
  664. if(n > len - sofar)
  665. n = len - sofar;
  666. memmove(nb->wp, p, n);
  667. qcopycnt += n;
  668. sofar += n;
  669. nb->wp += n;
  670. b = b->next;
  671. if(b == nil)
  672. break;
  673. n = BLEN(b);
  674. p = b->rp;
  675. }
  676. iunlock(q);
  677. return nb;
  678. }
  679. /*
  680. * called by non-interrupt code
  681. */
  682. Queue*
  683. qopen(int limit, int msg, void (*kick)(void*), void *arg)
  684. {
  685. Queue *q;
  686. q = malloc(sizeof(Queue));
  687. if(q == 0)
  688. return 0;
  689. q->limit = q->inilim = limit;
  690. q->kick = kick;
  691. q->arg = arg;
  692. q->state = msg;
  693. q->state |= Qstarve;
  694. q->eof = 0;
  695. q->noblock = 0;
  696. return q;
  697. }
  698. /* open a queue to be bypassed */
  699. Queue*
  700. qbypass(void (*bypass)(void*, Block*), void *arg)
  701. {
  702. Queue *q;
  703. q = malloc(sizeof(Queue));
  704. if(q == 0)
  705. return 0;
  706. q->limit = 0;
  707. q->arg = arg;
  708. q->bypass = bypass;
  709. q->state = 0;
  710. return q;
  711. }
  712. static int
  713. notempty(void *a)
  714. {
  715. Queue *q = a;
  716. return (q->state & Qclosed) || q->bfirst != 0;
  717. }
  718. /*
  719. * wait for the queue to be non-empty or closed.
  720. * called with q ilocked.
  721. */
  722. static int
  723. qwait(Queue *q)
  724. {
  725. /* wait for data */
  726. for(;;){
  727. if(q->bfirst != nil)
  728. break;
  729. if(q->state & Qclosed){
  730. if(++q->eof > 3)
  731. return -1;
  732. if(*q->err && strcmp(q->err, Ehungup) != 0)
  733. return -1;
  734. return 0;
  735. }
  736. q->state |= Qstarve; /* flag requesting producer to wake me */
  737. iunlock(q);
  738. sleep(&q->rr, notempty, q);
  739. ilock(q);
  740. }
  741. return 1;
  742. }
  743. /*
  744. * add a block list to a queue
  745. */
  746. void
  747. qaddlist(Queue *q, Block *b)
  748. {
  749. /* queue the block */
  750. if(q->bfirst)
  751. q->blast->next = b;
  752. else
  753. q->bfirst = b;
  754. q->len += blockalloclen(b);
  755. q->dlen += blocklen(b);
  756. while(b->next)
  757. b = b->next;
  758. q->blast = b;
  759. }
  760. /*
  761. * called with q ilocked
  762. */
  763. Block*
  764. qremove(Queue *q)
  765. {
  766. Block *b;
  767. b = q->bfirst;
  768. if(b == nil)
  769. return nil;
  770. q->bfirst = b->next;
  771. b->next = nil;
  772. q->dlen -= BLEN(b);
  773. q->len -= BALLOC(b);
  774. QDEBUG checkb(b, "qremove");
  775. return b;
  776. }
  777. /*
  778. * copy the contents of a string of blocks into
  779. * memory. emptied blocks are freed. return
  780. * pointer to first unconsumed block.
  781. */
  782. Block*
  783. bl2mem(uchar *p, Block *b, int n)
  784. {
  785. int i;
  786. Block *next;
  787. for(; b != nil; b = next){
  788. i = BLEN(b);
  789. if(i > n){
  790. memmove(p, b->rp, n);
  791. b->rp += n;
  792. return b;
  793. }
  794. memmove(p, b->rp, i);
  795. n -= i;
  796. p += i;
  797. b->rp += i;
  798. next = b->next;
  799. freeb(b);
  800. }
  801. return nil;
  802. }
  803. /*
  804. * copy the contents of memory into a string of blocks.
  805. * return nil on error.
  806. */
  807. Block*
  808. mem2bl(uchar *p, int len)
  809. {
  810. int n;
  811. Block *b, *first, **l;
  812. first = nil;
  813. l = &first;
  814. if(waserror()){
  815. freeblist(first);
  816. nexterror();
  817. }
  818. do {
  819. n = len;
  820. if(n > Maxatomic)
  821. n = Maxatomic;
  822. *l = b = allocb(n);
  823. setmalloctag(b, (up->text[0]<<24)|(up->text[1]<<16)|(up->text[2]<<8)|up->text[3]);
  824. memmove(b->wp, p, n);
  825. b->wp += n;
  826. p += n;
  827. len -= n;
  828. l = &b->next;
  829. } while(len > 0);
  830. poperror();
  831. return first;
  832. }
  833. /*
  834. * put a block back to the front of the queue
  835. * called with q ilocked
  836. */
  837. void
  838. qputback(Queue *q, Block *b)
  839. {
  840. b->next = q->bfirst;
  841. if(q->bfirst == nil)
  842. q->blast = b;
  843. q->bfirst = b;
  844. q->len += BALLOC(b);
  845. q->dlen += BLEN(b);
  846. }
  847. /*
  848. * flow control, get producer going again
  849. * called with q ilocked
  850. */
  851. static void
  852. qwakeup_iunlock(Queue *q)
  853. {
  854. int dowakeup = 0;
  855. /* if writer flow controlled, restart */
  856. if((q->state & Qflow) && q->len < q->limit/2){
  857. q->state &= ~Qflow;
  858. dowakeup = 1;
  859. }
  860. iunlock(q);
  861. /* wakeup flow controlled writers */
  862. if(dowakeup){
  863. if(q->kick)
  864. q->kick(q->arg);
  865. wakeup(&q->wr);
  866. }
  867. }
  868. /*
  869. * get next block from a queue (up to a limit)
  870. */
  871. Block*
  872. qbread(Queue *q, int len)
  873. {
  874. Block *b, *nb;
  875. int n;
  876. qlock(&q->rlock);
  877. if(waserror()){
  878. qunlock(&q->rlock);
  879. nexterror();
  880. }
  881. ilock(q);
  882. switch(qwait(q)){
  883. case 0:
  884. /* queue closed */
  885. iunlock(q);
  886. qunlock(&q->rlock);
  887. poperror();
  888. return nil;
  889. case -1:
  890. /* multiple reads on a closed queue */
  891. iunlock(q);
  892. error(q->err);
  893. }
  894. /* if we get here, there's at least one block in the queue */
  895. b = qremove(q);
  896. n = BLEN(b);
  897. /* split block if it's too big and this is not a message queue */
  898. nb = b;
  899. if(n > len){
  900. if((q->state&Qmsg) == 0){
  901. n -= len;
  902. b = allocb(n);
  903. memmove(b->wp, nb->rp+len, n);
  904. b->wp += n;
  905. qputback(q, b);
  906. }
  907. nb->wp = nb->rp + len;
  908. }
  909. /* restart producer */
  910. qwakeup_iunlock(q);
  911. poperror();
  912. qunlock(&q->rlock);
  913. return nb;
  914. }
  915. /*
  916. * read a queue. if no data is queued, post a Block
  917. * and wait on its Rendez.
  918. */
  919. long
  920. qread(Queue *q, void *vp, int len)
  921. {
  922. Block *b, *first, **l;
  923. int m, n;
  924. qlock(&q->rlock);
  925. if(waserror()){
  926. qunlock(&q->rlock);
  927. nexterror();
  928. }
  929. ilock(q);
  930. again:
  931. switch(qwait(q)){
  932. case 0:
  933. /* queue closed */
  934. iunlock(q);
  935. qunlock(&q->rlock);
  936. poperror();
  937. return 0;
  938. case -1:
  939. /* multiple reads on a closed queue */
  940. iunlock(q);
  941. error(q->err);
  942. }
  943. /* if we get here, there's at least one block in the queue */
  944. if(q->state & Qcoalesce){
  945. /* when coalescing, 0 length blocks just go away */
  946. b = q->bfirst;
  947. if(BLEN(b) <= 0){
  948. freeb(qremove(q));
  949. goto again;
  950. }
  951. /* grab the first block plus as many
  952. * following blocks as will completely
  953. * fit in the read.
  954. */
  955. n = 0;
  956. l = &first;
  957. m = BLEN(b);
  958. for(;;) {
  959. *l = qremove(q);
  960. l = &b->next;
  961. n += m;
  962. b = q->bfirst;
  963. if(b == nil)
  964. break;
  965. m = BLEN(b);
  966. if(n+m > len)
  967. break;
  968. }
  969. } else {
  970. first = qremove(q);
  971. n = BLEN(first);
  972. }
  973. /* copy to user space outside of the ilock */
  974. iunlock(q);
  975. b = bl2mem(vp, first, len);
  976. ilock(q);
  977. /* take care of any left over partial block */
  978. if(b != nil){
  979. n -= BLEN(b);
  980. if(q->state & Qmsg)
  981. freeb(b);
  982. else
  983. qputback(q, b);
  984. }
  985. /* restart producer */
  986. qwakeup_iunlock(q);
  987. poperror();
  988. qunlock(&q->rlock);
  989. return n;
  990. }
  991. static int
  992. qnotfull(void *a)
  993. {
  994. Queue *q = a;
  995. return q->len < q->limit || (q->state & Qclosed);
  996. }
  997. ulong noblockcnt;
  998. /*
  999. * add a block to a queue obeying flow control
  1000. */
  1001. long
  1002. qbwrite(Queue *q, Block *b)
  1003. {
  1004. int n, dowakeup;
  1005. Proc *p;
  1006. n = BLEN(b);
  1007. if(q->bypass){
  1008. (*q->bypass)(q->arg, b);
  1009. return n;
  1010. }
  1011. dowakeup = 0;
  1012. qlock(&q->wlock);
  1013. if(waserror()){
  1014. if(b != nil)
  1015. freeb(b);
  1016. qunlock(&q->wlock);
  1017. nexterror();
  1018. }
  1019. ilock(q);
  1020. /* give up if the queue is closed */
  1021. if(q->state & Qclosed){
  1022. iunlock(q);
  1023. error(q->err);
  1024. }
  1025. /* if nonblocking, don't queue over the limit */
  1026. if(q->len >= q->limit){
  1027. if(q->noblock){
  1028. iunlock(q);
  1029. freeb(b);
  1030. noblockcnt += n;
  1031. qunlock(&q->wlock);
  1032. poperror();
  1033. return n;
  1034. }
  1035. }
  1036. /* queue the block */
  1037. if(q->bfirst)
  1038. q->blast->next = b;
  1039. else
  1040. q->bfirst = b;
  1041. q->blast = b;
  1042. b->next = 0;
  1043. q->len += BALLOC(b);
  1044. q->dlen += n;
  1045. QDEBUG checkb(b, "qbwrite");
  1046. b = nil;
  1047. /* make sure other end gets awakened */
  1048. if(q->state & Qstarve){
  1049. q->state &= ~Qstarve;
  1050. dowakeup = 1;
  1051. }
  1052. iunlock(q);
  1053. /* get output going again */
  1054. if(q->kick && (dowakeup || (q->state&Qkick)))
  1055. q->kick(q->arg);
  1056. /* wakeup anyone consuming at the other end */
  1057. if(dowakeup){
  1058. p = wakeup(&q->rr);
  1059. /* if we just wokeup a higher priority process, let it run */
  1060. if(p != nil && p->priority > up->priority)
  1061. sched();
  1062. }
  1063. /*
  1064. * flow control, wait for queue to get below the limit
  1065. * before allowing the process to continue and queue
  1066. * more. We do this here so that postnote can only
  1067. * interrupt us after the data has been queued. This
  1068. * means that things like 9p flushes and ssl messages
  1069. * will not be disrupted by software interrupts.
  1070. *
  1071. * Note - this is moderately dangerous since a process
  1072. * that keeps getting interrupted and rewriting will
  1073. * queue infinite crud.
  1074. */
  1075. for(;;){
  1076. if(q->noblock || qnotfull(q))
  1077. break;
  1078. ilock(q);
  1079. q->state |= Qflow;
  1080. iunlock(q);
  1081. sleep(&q->wr, qnotfull, q);
  1082. }
  1083. USED(b);
  1084. qunlock(&q->wlock);
  1085. poperror();
  1086. return n;
  1087. }
  1088. /*
  1089. * write to a queue. only Maxatomic bytes at a time is atomic.
  1090. */
  1091. int
  1092. qwrite(Queue *q, void *vp, int len)
  1093. {
  1094. int n, sofar;
  1095. Block *b;
  1096. uchar *p = vp;
  1097. QDEBUG if(!islo())
  1098. print("qwrite hi %lux\n", getcallerpc(&q));
  1099. sofar = 0;
  1100. do {
  1101. n = len-sofar;
  1102. if(n > Maxatomic)
  1103. n = Maxatomic;
  1104. b = allocb(n);
  1105. setmalloctag(b, (up->text[0]<<24)|(up->text[1]<<16)|(up->text[2]<<8)|up->text[3]);
  1106. if(waserror()){
  1107. freeb(b);
  1108. nexterror();
  1109. }
  1110. memmove(b->wp, p+sofar, n);
  1111. poperror();
  1112. b->wp += n;
  1113. qbwrite(q, b);
  1114. sofar += n;
  1115. } while(sofar < len && (q->state & Qmsg) == 0);
  1116. return len;
  1117. }
  1118. /*
  1119. * used by print() to write to a queue. Since we may be splhi or not in
  1120. * a process, don't qlock.
  1121. *
  1122. * this routine merges adjacent blocks if block n+1 will fit into
  1123. * the free space of block n.
  1124. */
  1125. int
  1126. qiwrite(Queue *q, void *vp, int len)
  1127. {
  1128. int n, sofar, dowakeup;
  1129. Block *b;
  1130. uchar *p = vp;
  1131. dowakeup = 0;
  1132. sofar = 0;
  1133. do {
  1134. n = len-sofar;
  1135. if(n > Maxatomic)
  1136. n = Maxatomic;
  1137. b = iallocb(n);
  1138. if(b == nil)
  1139. break;
  1140. memmove(b->wp, p+sofar, n);
  1141. b->wp += n;
  1142. ilock(q);
  1143. /* we use an artificially high limit for kernel prints since anything
  1144. * over the limit gets dropped
  1145. */
  1146. if(q->dlen >= 16*1024){
  1147. iunlock(q);
  1148. freeb(b);
  1149. break;
  1150. }
  1151. QDEBUG checkb(b, "qiwrite");
  1152. if(q->bfirst)
  1153. q->blast->next = b;
  1154. else
  1155. q->bfirst = b;
  1156. q->blast = b;
  1157. q->len += BALLOC(b);
  1158. q->dlen += n;
  1159. if(q->state & Qstarve){
  1160. q->state &= ~Qstarve;
  1161. dowakeup = 1;
  1162. }
  1163. iunlock(q);
  1164. if(dowakeup){
  1165. if(q->kick)
  1166. q->kick(q->arg);
  1167. wakeup(&q->rr);
  1168. }
  1169. sofar += n;
  1170. } while(sofar < len && (q->state & Qmsg) == 0);
  1171. return sofar;
  1172. }
  1173. /*
  1174. * be extremely careful when calling this,
  1175. * as there is no reference accounting
  1176. */
  1177. void
  1178. qfree(Queue *q)
  1179. {
  1180. qclose(q);
  1181. free(q);
  1182. }
  1183. /*
  1184. * Mark a queue as closed. No further IO is permitted.
  1185. * All blocks are released.
  1186. */
  1187. void
  1188. qclose(Queue *q)
  1189. {
  1190. Block *bfirst;
  1191. if(q == nil)
  1192. return;
  1193. /* mark it */
  1194. ilock(q);
  1195. q->state |= Qclosed;
  1196. q->state &= ~(Qflow|Qstarve);
  1197. strcpy(q->err, Ehungup);
  1198. bfirst = q->bfirst;
  1199. q->bfirst = 0;
  1200. q->len = 0;
  1201. q->dlen = 0;
  1202. q->noblock = 0;
  1203. iunlock(q);
  1204. /* free queued blocks */
  1205. freeblist(bfirst);
  1206. /* wake up readers/writers */
  1207. wakeup(&q->rr);
  1208. wakeup(&q->wr);
  1209. }
  1210. /*
  1211. * Mark a queue as closed. Wakeup any readers. Don't remove queued
  1212. * blocks.
  1213. */
  1214. void
  1215. qhangup(Queue *q, char *msg)
  1216. {
  1217. /* mark it */
  1218. ilock(q);
  1219. q->state |= Qclosed;
  1220. if(msg == 0 || *msg == 0)
  1221. strcpy(q->err, Ehungup);
  1222. else
  1223. strncpy(q->err, msg, ERRMAX-1);
  1224. iunlock(q);
  1225. /* wake up readers/writers */
  1226. wakeup(&q->rr);
  1227. wakeup(&q->wr);
  1228. }
  1229. /*
  1230. * return non-zero if the q is hungup
  1231. */
  1232. int
  1233. qisclosed(Queue *q)
  1234. {
  1235. return q->state & Qclosed;
  1236. }
  1237. /*
  1238. * mark a queue as no longer hung up
  1239. */
  1240. void
  1241. qreopen(Queue *q)
  1242. {
  1243. ilock(q);
  1244. q->state &= ~Qclosed;
  1245. q->state |= Qstarve;
  1246. q->eof = 0;
  1247. q->limit = q->inilim;
  1248. iunlock(q);
  1249. }
  1250. /*
  1251. * return bytes queued
  1252. */
  1253. int
  1254. qlen(Queue *q)
  1255. {
  1256. return q->dlen;
  1257. }
  1258. /*
  1259. * return space remaining before flow control
  1260. */
  1261. int
  1262. qwindow(Queue *q)
  1263. {
  1264. int l;
  1265. l = q->limit - q->len;
  1266. if(l < 0)
  1267. l = 0;
  1268. return l;
  1269. }
  1270. /*
  1271. * return true if we can read without blocking
  1272. */
  1273. int
  1274. qcanread(Queue *q)
  1275. {
  1276. return q->bfirst!=0;
  1277. }
  1278. /*
  1279. * change queue limit
  1280. */
  1281. void
  1282. qsetlimit(Queue *q, int limit)
  1283. {
  1284. q->limit = limit;
  1285. }
  1286. /*
  1287. * set blocking/nonblocking
  1288. */
  1289. void
  1290. qnoblock(Queue *q, int onoff)
  1291. {
  1292. q->noblock = onoff;
  1293. }
  1294. /*
  1295. * flush the output queue
  1296. */
  1297. void
  1298. qflush(Queue *q)
  1299. {
  1300. Block *bfirst;
  1301. /* mark it */
  1302. ilock(q);
  1303. bfirst = q->bfirst;
  1304. q->bfirst = 0;
  1305. q->len = 0;
  1306. q->dlen = 0;
  1307. iunlock(q);
  1308. /* free queued blocks */
  1309. freeblist(bfirst);
  1310. /* wake up readers/writers */
  1311. wakeup(&q->wr);
  1312. }
  1313. int
  1314. qfull(Queue *q)
  1315. {
  1316. return q->state & Qflow;
  1317. }
  1318. int
  1319. qstate(Queue *q)
  1320. {
  1321. return q->state;
  1322. }