1 /* Copyright (C) 2003-2006 Russ Cox, Massachusetts Institute of Technology */
5 * Generic RPC packet multiplexor. Inspired by but not derived from
6 * Plan 9 kernel. Originally developed as part of Tra, later used in
7 * libnventi, and then finally split out into a generic library.
14 static int gettag(Mux*, Muxrpc*);
15 static void puttag(Mux*, Muxrpc*);
16 static void enqueue(Mux*, Muxrpc*);
17 static void dequeue(Mux*, Muxrpc*);
22 memset(&mux->lk, 0, sizeof(Mux)-offsetof(Mux, lk));
23 mux->tagrend.l = &mux->lk;
24 mux->rpcfork.l = &mux->lk;
25 mux->sleep.next = &mux->sleep;
26 mux->sleep.prev = &mux->sleep;
34 /* must malloc because stack could be private */
35 r = mallocz(sizeof(Muxrpc), 1);
37 werrstr("mallocz: %r");
48 tagmuxrpc(Muxrpc *r, void *tx)
54 /* assign the tag, add selves to response queue */
57 /*print("gettag %p %d\n", r, tag); */
61 /* actually send the packet */
62 if(tag < 0 || mux->settag(mux, tx, tag) < 0 || _muxsend(mux, tx) < 0){
63 werrstr("settag/send tag %d: %r", tag);
75 muxmsgandqlock(Mux *mux, void *p)
80 tag = mux->gettag(mux, p) - mux->mintag;
81 /*print("mux tag %d\n", tag); */
83 /* hand packet to correct sleeper */
84 if(tag < 0 || tag >= mux->mwait){
85 fprint(2, "%s: bad rpc tag %ux\n", argv0, tag);
86 /* must leak packet! don't know how to free it! */
90 if(r2 == nil || r2->prev == nil){
91 fprint(2, "%s: bad rpc tag %ux (no one waiting on that tag)\n", argv0, tag);
92 /* must leak packet! don't know how to free it! */
105 /* if there is anyone else sleeping, wake them to mux */
106 for(rpc=mux->sleep.next; rpc != &mux->sleep; rpc = rpc->next){
117 muxrpc(Mux *mux, void *tx)
123 if((r = allocmuxrpc(mux)) == nil)
126 if((tag = tagmuxrpc(r, tx)) < 0)
130 /* wait for our packet */
131 while(mux->muxer && mux->muxer != r && !r->p)
134 /* if not done, there's no muxer: start muxing */
136 if(mux->muxer != nil && mux->muxer != r)
141 _muxrecv(mux, 1, &p);
143 /* eof -- just give up and pass the buck */
148 muxmsgandqlock(mux, p);
156 werrstr("unexpected eof");
161 muxrpcstart(Mux *mux, void *tx)
166 if((r = allocmuxrpc(mux)) == nil)
169 if((tag = tagmuxrpc(r, tx)) < 0)
175 muxrpccanfinish(Muxrpc *r, void **vp)
184 if(!r->p && !mux->muxer){
189 if(!_muxrecv(mux, 0, &p))
195 muxmsgandqlock(mux, p);
208 enqueue(Mux *mux, Muxrpc *r)
210 r->next = mux->sleep.next;
211 r->prev = &mux->sleep;
217 dequeue(Mux *mux, Muxrpc *r)
219 r->next->prev = r->prev;
220 r->prev->next = r->next;
226 gettag(Mux *mux, Muxrpc *r)
232 /* wait for a free tag */
233 while(mux->nwait == mux->mwait){
234 if(mux->mwait < mux->maxtag-mux->mintag){
240 w = realloc(mux->wait, mw*sizeof(w[0]));
243 memset(w+mux->mwait, 0, (mw-mux->mwait)*sizeof(w[0]));
245 mux->freetag = mux->mwait;
249 rsleep(&mux->tagrend);
253 if(mux->wait[i] == 0)
255 for(; i<mux->mwait; i++)
256 if(mux->wait[i] == 0)
258 for(i=0; i<mux->freetag; i++)
259 if(mux->wait[i] == 0)
261 /* should not fall out of while without free tag */
262 fprint(2, "libfs: nwait botch\n");
269 r->tag = i+mux->mintag;
274 puttag(Mux *mux, Muxrpc *r)
278 i = r->tag - mux->mintag;
279 assert(mux->wait[i] == r);
283 rwakeup(&mux->tagrend);