Commit 83ea35df authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Segregate the WRW related fields in their own structure for code

clarity.
parent d9037ddf
......@@ -248,6 +248,16 @@ struct exp {
/*--------------------------------------------------------------------*/
/* WRW related fields */
struct wrw {
int *wfd;
unsigned werr; /* valid after WRK_Flush() */
struct iovec *iov;
unsigned siov;
unsigned niov;
ssize_t liov;
};
struct worker {
unsigned magic;
#define WORKER_MAGIC 0x6391adcf
......@@ -259,18 +269,13 @@ struct worker {
double lastused;
struct wrw wrw;
pthread_cond_t cond;
VTAILQ_ENTRY(worker) list;
struct workreq *wrq;
int *wfd;
unsigned werr; /* valid after WRK_Flush() */
struct iovec *iov;
unsigned siov;
unsigned niov;
ssize_t liov;
struct VCL_conf *vcl;
uint32_t *wlb, *wlp, *wle;
......@@ -781,6 +786,7 @@ int WRK_Queue(struct workreq *wrq);
int WRK_QueueSession(struct sess *sp);
void WRK_SumStat(struct worker *w);
#define WRW_IsReleased(w) ((w)->wrw.wfd == NULL)
void WRW_Reserve(struct worker *w, int *fd);
unsigned WRW_Flush(struct worker *w);
unsigned WRW_FlushRelease(struct worker *w);
......
......@@ -240,7 +240,7 @@ cnt_deliver(struct sess *sp)
RES_WriteObj(sp);
AZ(sp->wrk->wfd);
assert(WRW_IsReleased(sp->wrk));
(void)HSH_Deref(sp->wrk, NULL, &sp->obj);
http_Setup(sp->wrk->resp, NULL);
sp->step = STP_DONE;
......@@ -749,7 +749,7 @@ cnt_fetchbody(struct sess *sp)
http_Setup(sp->wrk->bereq, NULL);
http_Setup(sp->wrk->beresp, NULL);
sp->wrk->vfp = NULL;
AZ(sp->wrk->wfd);
assert(WRW_IsReleased(sp->wrk));
AZ(sp->vbc);
AN(sp->director);
......@@ -1121,7 +1121,7 @@ cnt_pipe(struct sess *sp)
assert(sp->handling == VCL_RET_PIPE);
PipeSession(sp);
AZ(sp->wrk->wfd);
assert(WRW_IsReleased(sp->wrk));
http_Setup(sp->wrk->bereq, NULL);
sp->step = STP_DONE;
return (0);
......@@ -1396,7 +1396,7 @@ CNT_Session(struct sess *sp)
CHECK_OBJ_ORNULL(w->nobjhead, OBJHEAD_MAGIC);
}
WSL_Flush(w, 0);
AZ(w->wfd);
assert(WRW_IsReleased(w));
}
/*
......
......@@ -1054,10 +1054,11 @@ unsigned
http_Write(struct worker *w, const struct http *hp, int resp)
{
unsigned u, l;
int fd = *(w->wrw.wfd);
if (resp) {
l = WRW_WriteH(w, &hp->hd[HTTP_HDR_PROTO], " ");
WSLH(w, *w->wfd, hp, HTTP_HDR_PROTO);
WSLH(w, fd, hp, HTTP_HDR_PROTO);
hp->hd[HTTP_HDR_STATUS].b = WS_Alloc(w->ws, 4);
AN(hp->hd[HTTP_HDR_STATUS].b);
......@@ -1066,18 +1067,18 @@ http_Write(struct worker *w, const struct http *hp, int resp)
hp->hd[HTTP_HDR_STATUS].e = hp->hd[HTTP_HDR_STATUS].b + 3;
l += WRW_WriteH(w, &hp->hd[HTTP_HDR_STATUS], " ");
WSLH(w, *w->wfd, hp, HTTP_HDR_STATUS);
WSLH(w, fd, hp, HTTP_HDR_STATUS);
l += WRW_WriteH(w, &hp->hd[HTTP_HDR_RESPONSE], "\r\n");
WSLH(w, *w->wfd, hp, HTTP_HDR_RESPONSE);
WSLH(w, fd, hp, HTTP_HDR_RESPONSE);
} else {
AN(hp->hd[HTTP_HDR_URL].b);
l = WRW_WriteH(w, &hp->hd[HTTP_HDR_REQ], " ");
WSLH(w, *w->wfd, hp, HTTP_HDR_REQ);
WSLH(w, fd, hp, HTTP_HDR_REQ);
l += WRW_WriteH(w, &hp->hd[HTTP_HDR_URL], " ");
WSLH(w, *w->wfd, hp, HTTP_HDR_URL);
WSLH(w, fd, hp, HTTP_HDR_URL);
l += WRW_WriteH(w, &hp->hd[HTTP_HDR_PROTO], "\r\n");
WSLH(w, *w->wfd, hp, HTTP_HDR_PROTO);
WSLH(w, fd, hp, HTTP_HDR_PROTO);
}
for (u = HTTP_HDR_FIRST; u < hp->nhd; u++) {
if (hp->hd[u].b == NULL)
......@@ -1085,7 +1086,7 @@ http_Write(struct worker *w, const struct http *hp, int resp)
AN(hp->hd[u].b);
AN(hp->hd[u].e);
l += WRW_WriteH(w, &hp->hd[u], "\r\n");
WSLH(w, *w->wfd, hp, u);
WSLH(w, fd, hp, u);
}
l += WRW_Write(w, "\r\n", -1);
return (l);
......
......@@ -143,8 +143,8 @@ wrk_thread_real(struct wq *qp, unsigned shm_workspace, unsigned sess_workspace,
w->bereq = HTTP_create(http0, nhttp);
w->beresp = HTTP_create(http1, nhttp);
w->resp = HTTP_create(http2, nhttp);
w->iov = iov;
w->siov = siov;
w->wrw.iov = iov;
w->wrw.siov = siov;
AZ(pthread_cond_init(&w->cond, NULL));
WS_Init(w->ws, "wrk", ws, sess_workspace);
......@@ -189,7 +189,7 @@ wrk_thread_real(struct wq *qp, unsigned shm_workspace, unsigned sess_workspace,
AZ(w->bereq->ws);
AZ(w->beresp->ws);
AZ(w->resp->ws);
AZ(w->wfd);
AZ(w->wrw.wfd);
AZ(w->storage_hint);
assert(w->wlp == w->wlb);
w->wrq = NULL;
......
......@@ -72,45 +72,51 @@ SVNID("$Id$")
void
WRW_Reserve(struct worker *w, int *fd)
{
struct wrw *wrw;
CHECK_OBJ_NOTNULL(w, WORKER_MAGIC);
AZ(w->wfd);
w->werr = 0;
w->liov = 0;
w->niov = 0;
w->wfd = fd;
wrw = &w->wrw;
AZ(wrw->wfd);
wrw->werr = 0;
wrw->liov = 0;
wrw->niov = 0;
wrw->wfd = fd;
}
static void
WRW_Release(struct worker *w)
{
struct wrw *wrw;
CHECK_OBJ_NOTNULL(w, WORKER_MAGIC);
w->werr = 0;
w->liov = 0;
w->niov = 0;
w->wfd = NULL;
wrw = &w->wrw;
wrw->werr = 0;
wrw->liov = 0;
wrw->niov = 0;
wrw->wfd = NULL;
}
unsigned
WRW_Flush(struct worker *w)
{
ssize_t i;
struct wrw *wrw;
CHECK_OBJ_NOTNULL(w, WORKER_MAGIC);
AN(w->wfd);
if (*w->wfd >= 0 && w->niov > 0 && w->werr == 0) {
i = writev(*w->wfd, w->iov, w->niov);
if (i != w->liov) {
w->werr++;
WSL(w, SLT_Debug, *w->wfd,
wrw = &w->wrw;
AN(wrw->wfd);
if (*wrw->wfd >= 0 && wrw->niov > 0 && wrw->werr == 0) {
i = writev(*wrw->wfd, wrw->iov, wrw->niov);
if (i != wrw->liov) {
wrw->werr++;
WSL(w, SLT_Debug, *wrw->wfd,
"Write error, retval = %d, len = %d, errno = %s",
i, w->liov, strerror(errno));
i, wrw->liov, strerror(errno));
}
}
w->liov = 0;
w->niov = 0;
return (w->werr);
wrw->liov = 0;
wrw->niov = 0;
return (wrw->werr);
}
unsigned
......@@ -119,7 +125,7 @@ WRW_FlushRelease(struct worker *w)
unsigned u;
CHECK_OBJ_NOTNULL(w, WORKER_MAGIC);
AN(w->wfd);
AN(w->wrw.wfd);
u = WRW_Flush(w);
WRW_Release(w);
return (u);
......@@ -131,7 +137,7 @@ WRW_WriteH(struct worker *w, const txt *hh, const char *suf)
unsigned u;
CHECK_OBJ_NOTNULL(w, WORKER_MAGIC);
AN(w->wfd);
AN(w->wrw.wfd);
AN(w);
AN(hh);
AN(hh->b);
......@@ -145,19 +151,21 @@ WRW_WriteH(struct worker *w, const txt *hh, const char *suf)
unsigned
WRW_Write(struct worker *w, const void *ptr, int len)
{
struct wrw *wrw;
CHECK_OBJ_NOTNULL(w, WORKER_MAGIC);
AN(w->wfd);
if (len == 0 || *w->wfd < 0)
wrw = &w->wrw;
AN(wrw->wfd);
if (len == 0 || *wrw->wfd < 0)
return (0);
if (len == -1)
len = strlen(ptr);
if (w->niov == w->siov)
if (wrw->niov == wrw->siov)
(void)WRW_Flush(w);
w->iov[w->niov].iov_base = TRUST_ME(ptr);
w->iov[w->niov].iov_len = len;
w->liov += len;
w->niov++;
wrw->iov[wrw->niov].iov_base = TRUST_ME(ptr);
wrw->iov[wrw->niov].iov_len = len;
wrw->liov += len;
wrw->niov++;
return (len);
}
......@@ -165,9 +173,11 @@ WRW_Write(struct worker *w, const void *ptr, int len)
void
WRW_Sendfile(struct worker *w, int fd, off_t off, unsigned len)
{
struct wrw *wrw;
CHECK_OBJ_NOTNULL(w, WORKER_MAGIC);
AN(w->wfd);
wrw = &w->wrw;
AN(wrw->wfd);
assert(fd >= 0);
assert(len > 0);
......@@ -175,31 +185,31 @@ WRW_Sendfile(struct worker *w, int fd, off_t off, unsigned len)
do {
struct sf_hdtr sfh;
memset(&sfh, 0, sizeof sfh);
if (w->niov > 0) {
sfh.headers = w->iov;
sfh.hdr_cnt = w->niov;
if (wrw->niov > 0) {
sfh.headers = wrw->iov;
sfh.hdr_cnt = wrw->niov;
}
if (sendfile(fd, *w->wfd, off, len, &sfh, NULL, 0) != 0)
w->werr++;
w->liov = 0;
w->niov = 0;
if (sendfile(fd, *wrw->wfd, off, len, &sfh, NULL, 0) != 0)
wrw->werr++;
wrw->liov = 0;
wrw->niov = 0;
} while (0);
#elif defined(__linux__)
do {
if (WRK_Flush(w) == 0 &&
sendfile(*w->wfd, fd, &off, len) != len)
w->werr++;
sendfile(*wrw->wfd, fd, &off, len) != len)
wrw->werr++;
} while (0);
#elif defined(__sun) && defined(HAVE_SENDFILEV)
do {
sendfilevec_t svvec[params->http_headers * 2 + 1];
size_t xferred = 0, expected = 0;
int i;
for (i = 0; i < w->niov; i++) {
for (i = 0; i < wrw->niov; i++) {
svvec[i].sfv_fd = SFV_FD_SELF;
svvec[i].sfv_flag = 0;
svvec[i].sfv_off = (off_t) w->iov[i].iov_base;
svvec[i].sfv_len = w->iov[i].iov_len;
svvec[i].sfv_off = (off_t) wrw->iov[i].iov_base;
svvec[i].sfv_len = wrw->iov[i].iov_len;
expected += svvec[i].sfv_len;
}
svvec[i].sfv_fd = fd;
......@@ -207,17 +217,17 @@ WRW_Sendfile(struct worker *w, int fd, off_t off, unsigned len)
svvec[i].sfv_off = off;
svvec[i].sfv_len = len;
expected += svvec[i].sfv_len;
if (sendfilev(*w->wfd, svvec, i, &xferred) == -1 ||
if (sendfilev(*wrw->wfd, svvec, i, &xferred) == -1 ||
xferred != expected)
w->werr++;
w->liov = 0;
w->niov = 0;
wrw->werr++;
wrw->liov = 0;
wrw->niov = 0;
} while (0);
#elif defined(__sun) && defined(HAVE_SENDFILE)
do {
if (WRK_Flush(w) == 0 &&
sendfile(*w->wfd, fd, &off, len) != len)
w->werr++;
sendfile(*wrw->wfd, fd, &off, len) != len)
wrw->werr++;
} while (0);
#else
#error Unknown sendfile() implementation
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment