r4257 - branches/2.0/varnish-cache/bin/varnishd

tfheen at projects.linpro.no tfheen at projects.linpro.no
Mon Sep 28 16:00:24 CEST 2009


Author: tfheen
Date: 2009-09-28 16:00:23 +0200 (Mon, 28 Sep 2009)
New Revision: 4257

Modified:
   branches/2.0/varnish-cache/bin/varnishd/cache.h
   branches/2.0/varnish-cache/bin/varnishd/cache_expire.c
   branches/2.0/varnish-cache/bin/varnishd/cache_pool.c
Log:
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.



Modified: branches/2.0/varnish-cache/bin/varnishd/cache.h
===================================================================
--- branches/2.0/varnish-cache/bin/varnishd/cache.h	2009-09-28 12:32:39 UTC (rev 4256)
+++ branches/2.0/varnish-cache/bin/varnishd/cache.h	2009-09-28 14:00:23 UTC (rev 4257)
@@ -543,6 +543,9 @@
 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);

Modified: branches/2.0/varnish-cache/bin/varnishd/cache_expire.c
===================================================================
--- branches/2.0/varnish-cache/bin/varnishd/cache_expire.c	2009-09-28 12:32:39 UTC (rev 4256)
+++ branches/2.0/varnish-cache/bin/varnishd/cache_expire.c	2009-09-28 14:00:23 UTC (rev 4257)
@@ -275,25 +275,12 @@
  */
 
 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);
 	t = TIM_real();
@@ -303,7 +290,7 @@
 		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 @@
 		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 @@
 			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 @@
 			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 @@
 	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);
 }

Modified: branches/2.0/varnish-cache/bin/varnishd/cache_pool.c
===================================================================
--- branches/2.0/varnish-cache/bin/varnishd/cache_pool.c	2009-09-28 12:32:39 UTC (rev 4256)
+++ branches/2.0/varnish-cache/bin/varnishd/cache_pool.c	2009-09-28 14:00:23 UTC (rev 4257)
@@ -618,6 +618,56 @@
 	}
 }
 
+/*--------------------------------------------------------------------
+ * 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



More information about the varnish-commit mailing list