lib9p: do not override Srv.end in listensrv(), simplify srvclose() and recounting

listensrv() used to override Srv.end() with its own handler
to free the malloc'd Srv structure and close the fd. this
makes it impossible to register your own cleanup handler.
instead, we introduce the private Srv.free() handler that
is used by listensrv to register its cleanup code. Srv.free()
is called once all the srv procs have been exited and all
requests on that srv have been responded to while Srv.end()
is called once all the procs exited the srv loop regardless
of the requests still being in flight.
front
cinap_lenrek 2015-10-15 13:21:30 +02:00
parent 9112daa764
commit a034e629f7
3 changed files with 22 additions and 15 deletions

View File

@ -233,8 +233,10 @@ struct Srv {
char* addr; char* addr;
QLock slock; QLock slock;
Ref sref; Ref sref; /* srvwork procs */
Ref rref; Ref rref; /* requests in flight */
void (*free)(Srv*);
}; };
void srv(Srv*); void srv(Srv*);

View File

@ -7,7 +7,7 @@
static void listenproc(void*); static void listenproc(void*);
static void srvproc(void*); static void srvproc(void*);
static void srvend(Srv *); static void srvfree(Srv *);
static char *getremotesys(char*); static char *getremotesys(char*);
void void
@ -58,7 +58,7 @@ listenproc(void *v)
s->rpool = nil; s->rpool = nil;
s->rbuf = nil; s->rbuf = nil;
s->wbuf = nil; s->wbuf = nil;
s->end = srvend; s->free = srvfree;
_forker(srvproc, s, 0); _forker(srvproc, s, 0);
} }
free(os->addr); free(os->addr);
@ -72,7 +72,7 @@ srvproc(void *v)
} }
static void static void
srvend(Srv *s) srvfree(Srv *s)
{ {
close(s->infd); close(s->infd);
free(s->addr); free(s->addr);

View File

@ -167,7 +167,7 @@ walkandclone(Req *r, char *(*walk1)(Fid*, char*, void*), char *(*clone)(Fid*, Fi
static void static void
sversion(Srv *srv, Req *r) sversion(Srv *srv, Req *r)
{ {
if(srv->rref.ref != 2){ if(srv->rref.ref != 1){
respond(r, Ebotch); respond(r, Ebotch);
return; return;
} }
@ -724,8 +724,6 @@ srvwork(void *v)
Srv *srv = v; Srv *srv = v;
Req *r; Req *r;
incref(&srv->rref);
incref(&srv->sref);
while(r = getreq(srv)){ while(r = getreq(srv)){
incref(&srv->rref); incref(&srv->rref);
if(r->error){ if(r->error){
@ -753,14 +751,17 @@ srvwork(void *v)
} }
qunlock(&srv->slock); qunlock(&srv->slock);
} }
decref(&srv->sref);
srvclose(srv); if(srv->end && srv->sref.ref == 1)
srv->end(srv);
if(decref(&srv->sref) == 0)
srvclose(srv);
} }
static void static void
srvclose(Srv *srv) srvclose(Srv *srv)
{ {
if(decref(&srv->rref)) if(srv->rref.ref || srv->sref.ref)
return; return;
if(chatty9p) if(chatty9p)
@ -776,8 +777,8 @@ srvclose(Srv *srv)
freereqpool(srv->rpool); freereqpool(srv->rpool);
srv->rpool = nil; srv->rpool = nil;
if(srv->end) if(srv->free)
srv->end(srv); srv->free(srv);
} }
void void
@ -790,8 +791,10 @@ srvacquire(Srv *srv)
void void
srvrelease(Srv *srv) srvrelease(Srv *srv)
{ {
if(decref(&srv->sref) == 0) if(decref(&srv->sref) == 0){
incref(&srv->sref);
_forker(srvwork, srv, 0); _forker(srvwork, srv, 0);
}
qunlock(&srv->slock); qunlock(&srv->slock);
} }
@ -819,6 +822,7 @@ srv(Srv *srv)
if(srv->start) if(srv->start)
srv->start(srv); srv->start(srv);
incref(&srv->sref);
srvwork(srv); srvwork(srv);
} }
@ -896,7 +900,8 @@ if(chatty9p)
else else
free(r); free(r);
srvclose(srv); if(decref(&srv->rref) == 0)
srvclose(srv);
} }
void void