qio.c 23 KB

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