qio.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  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. int
  1121. qiwrite(Queue *q, void *vp, int len)
  1122. {
  1123. int n, sofar, dowakeup;
  1124. Block *b;
  1125. uchar *p = vp;
  1126. dowakeup = 0;
  1127. sofar = 0;
  1128. do {
  1129. n = len-sofar;
  1130. if(n > Maxatomic)
  1131. n = Maxatomic;
  1132. b = iallocb(n);
  1133. if(b == nil)
  1134. break;
  1135. memmove(b->wp, p+sofar, n);
  1136. b->wp += n;
  1137. ilock(q);
  1138. /* don't queue over the limit, just lose the bytes */
  1139. if(q->len >= q->limit){
  1140. iunlock(q);
  1141. freeb(b);
  1142. break;
  1143. }
  1144. QDEBUG checkb(b, "qiwrite");
  1145. if(q->bfirst)
  1146. q->blast->next = b;
  1147. else
  1148. q->bfirst = b;
  1149. q->blast = b;
  1150. q->len += BALLOC(b);
  1151. q->dlen += n;
  1152. if(q->state & Qstarve){
  1153. q->state &= ~Qstarve;
  1154. dowakeup = 1;
  1155. }
  1156. iunlock(q);
  1157. if(dowakeup){
  1158. if(q->kick)
  1159. q->kick(q->arg);
  1160. wakeup(&q->rr);
  1161. }
  1162. sofar += n;
  1163. } while(sofar < len && (q->state & Qmsg) == 0);
  1164. return sofar;
  1165. }
  1166. /*
  1167. * be extremely careful when calling this,
  1168. * as there is no reference accounting
  1169. */
  1170. void
  1171. qfree(Queue *q)
  1172. {
  1173. qclose(q);
  1174. free(q);
  1175. }
  1176. /*
  1177. * Mark a queue as closed. No further IO is permitted.
  1178. * All blocks are released.
  1179. */
  1180. void
  1181. qclose(Queue *q)
  1182. {
  1183. Block *bfirst;
  1184. if(q == nil)
  1185. return;
  1186. /* mark it */
  1187. ilock(q);
  1188. q->state |= Qclosed;
  1189. q->state &= ~(Qflow|Qstarve);
  1190. strcpy(q->err, Ehungup);
  1191. bfirst = q->bfirst;
  1192. q->bfirst = 0;
  1193. q->len = 0;
  1194. q->dlen = 0;
  1195. q->noblock = 0;
  1196. iunlock(q);
  1197. /* free queued blocks */
  1198. freeblist(bfirst);
  1199. /* wake up readers/writers */
  1200. wakeup(&q->rr);
  1201. wakeup(&q->wr);
  1202. }
  1203. /*
  1204. * Mark a queue as closed. Wakeup any readers. Don't remove queued
  1205. * blocks.
  1206. */
  1207. void
  1208. qhangup(Queue *q, char *msg)
  1209. {
  1210. /* mark it */
  1211. ilock(q);
  1212. q->state |= Qclosed;
  1213. if(msg == 0 || *msg == 0)
  1214. strcpy(q->err, Ehungup);
  1215. else
  1216. strncpy(q->err, msg, ERRMAX-1);
  1217. iunlock(q);
  1218. /* wake up readers/writers */
  1219. wakeup(&q->rr);
  1220. wakeup(&q->wr);
  1221. }
  1222. /*
  1223. * return non-zero if the q is hungup
  1224. */
  1225. int
  1226. qisclosed(Queue *q)
  1227. {
  1228. return q->state & Qclosed;
  1229. }
  1230. /*
  1231. * mark a queue as no longer hung up
  1232. */
  1233. void
  1234. qreopen(Queue *q)
  1235. {
  1236. ilock(q);
  1237. q->state &= ~Qclosed;
  1238. q->state |= Qstarve;
  1239. q->eof = 0;
  1240. q->limit = q->inilim;
  1241. iunlock(q);
  1242. }
  1243. /*
  1244. * return bytes queued
  1245. */
  1246. int
  1247. qlen(Queue *q)
  1248. {
  1249. return q->dlen;
  1250. }
  1251. /*
  1252. * return space remaining before flow control
  1253. */
  1254. int
  1255. qwindow(Queue *q)
  1256. {
  1257. int l;
  1258. l = q->limit - q->len;
  1259. if(l < 0)
  1260. l = 0;
  1261. return l;
  1262. }
  1263. /*
  1264. * return true if we can read without blocking
  1265. */
  1266. int
  1267. qcanread(Queue *q)
  1268. {
  1269. return q->bfirst!=0;
  1270. }
  1271. /*
  1272. * change queue limit
  1273. */
  1274. void
  1275. qsetlimit(Queue *q, int limit)
  1276. {
  1277. q->limit = limit;
  1278. }
  1279. /*
  1280. * set blocking/nonblocking
  1281. */
  1282. void
  1283. qnoblock(Queue *q, int onoff)
  1284. {
  1285. q->noblock = onoff;
  1286. }
  1287. /*
  1288. * flush the output queue
  1289. */
  1290. void
  1291. qflush(Queue *q)
  1292. {
  1293. Block *bfirst;
  1294. /* mark it */
  1295. ilock(q);
  1296. bfirst = q->bfirst;
  1297. q->bfirst = 0;
  1298. q->len = 0;
  1299. q->dlen = 0;
  1300. iunlock(q);
  1301. /* free queued blocks */
  1302. freeblist(bfirst);
  1303. /* wake up readers/writers */
  1304. wakeup(&q->wr);
  1305. }
  1306. int
  1307. qfull(Queue *q)
  1308. {
  1309. return q->state & Qflow;
  1310. }
  1311. int
  1312. qstate(Queue *q)
  1313. {
  1314. return q->state;
  1315. }