Commit 5ea6e60b authored by Tollef Fog Heen's avatar Tollef Fog Heen

Merge r3905: Small facility for background threads

Add a miniature facility for starting a background thread with its
own session and worker strutures all set up and ready.

Use this for the cache-timeout thread, instead of home-rolling it.



git-svn-id: http://www.varnish-cache.org/svn/branches/2.0@4257 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent c33f40a7
......@@ -543,6 +543,9 @@ unsigned WRW_WriteH(struct worker *w, const txt *hh, const char *suf);
void WRW_Sendfile(struct worker *w, int fd, off_t off, unsigned len);
#endif /* SENDFILE_WORKS */
typedef void *bgthread_t(struct sess *);
void WRK_BgThread(pthread_t *thr, const char *name, bgthread_t *func);
/* cache_session.c [SES] */
void SES_Init(void);
struct sess *SES_New(const struct sockaddr *addr, unsigned len);
......
......@@ -275,24 +275,11 @@ EXP_Rearm(const struct object *o)
*/
static void *
exp_timer(void *arg)
exp_timer(struct sess *sp)
{
struct worker ww;
struct objexp *oe;
struct object *o;
double t;
struct sess *sp;
unsigned char logbuf[1024]; /* XXX size ? */
THR_SetName("cache-timeout");
(void)arg;
sp = SES_New(NULL, 0);
XXXAN(sp);
sp->wrk = &ww;
ww.magic = WORKER_MAGIC;
ww.wlp = ww.wlb = logbuf;
ww.wle = logbuf + sizeof logbuf;
AZ(sleep(10)); /* XXX: Takes time for VCL to arrive */
VCL_Get(&sp->vcl);
......@@ -303,7 +290,7 @@ exp_timer(void *arg)
CHECK_OBJ_ORNULL(oe, OBJEXP_MAGIC);
if (oe == NULL || oe->timer_when > t) { /* XXX: > or >= ? */
Lck_Unlock(&exp_mtx);
WSL_Flush(&ww, 0);
WSL_Flush(sp->wrk, 0);
AZ(sleep(1));
VCL_Refresh(&sp->vcl);
t = TIM_real();
......@@ -327,7 +314,7 @@ exp_timer(void *arg)
assert(oe->on_lru);
Lck_Unlock(&exp_mtx);
WSL(&ww, SLT_ExpPick, 0, "%u %s", o->xid, oe->timer_what);
WSL(sp->wrk, SLT_ExpPick, 0, "%u %s", o->xid, oe->timer_what);
if (oe->timer_what == tmr_prefetch) {
o->prefetch = 0.0;
......@@ -335,7 +322,7 @@ exp_timer(void *arg)
VCL_prefetch_method(sp);
sp->obj = NULL;
if (sp->handling == VCL_RET_FETCH) {
WSL(&ww, SLT_Debug, 0, "Attempt Prefetch %u",
WSL(sp->wrk, SLT_Debug, 0, "Attempt Prefetch %u",
o->xid);
}
Lck_Lock(&exp_mtx);
......@@ -351,7 +338,7 @@ exp_timer(void *arg)
sp->obj = NULL;
assert(sp->handling == VCL_RET_DISCARD);
WSL(&ww, SLT_ExpKill, 0,
WSL(sp->wrk, SLT_ExpKill, 0,
"%u %d", o->xid, (int)(o->ttl - t));
Lck_Lock(&exp_mtx);
assert(oe->timer_idx == BINHEAP_NOIDX);
......@@ -481,5 +468,5 @@ EXP_Init(void)
Lck_New(&exp_mtx);
exp_heap = binheap_new(NULL, object_cmp, object_update);
XXXAN(exp_heap);
AZ(pthread_create(&exp_thread, NULL, exp_timer, NULL));
WRK_BgThread(&exp_thread, "cache-timeout", exp_timer);
}
......@@ -618,6 +618,56 @@ wrk_herder_thread(void *priv)
}
}
/*--------------------------------------------------------------------
* Create and starte a back-ground thread which as its own worker and
* session data structures;
*/
struct bgthread {
unsigned magic;
#define BGTHREAD_MAGIC 0x23b5152b
const char *name;
bgthread_t *func;
};
static void *
wrk_bgthread(void *arg)
{
struct bgthread *bt;
struct worker ww;
struct sess *sp;
unsigned char logbuf[1024]; /* XXX: size ? */
CAST_OBJ_NOTNULL(bt, arg, BGTHREAD_MAGIC);
THR_SetName(bt->name);
sp = SES_New(NULL, 0);
XXXAN(sp);
memset(&ww, 0, sizeof ww);
sp->wrk = &ww;
ww.magic = WORKER_MAGIC;
ww.wlp = ww.wlb = logbuf;
ww.wle = logbuf + sizeof logbuf;
(void)bt->func(sp);
WRONG("BgThread terminated");
return (NULL);
}
void
WRK_BgThread(pthread_t *thr, const char *name, bgthread_t *func)
{
struct bgthread *bt;
ALLOC_OBJ(bt, BGTHREAD_MAGIC);
AN(bt);
bt->name = name;
bt->func = func;
AZ(pthread_create(thr, NULL, wrk_bgthread, bt));
}
/*--------------------------------------------------------------------*/
void
......
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