qio.c 23 KB

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