r3905 - trunk/varnish-cache/bin/varnishd

phk at projects.linpro.no phk at projects.linpro.no
Mon Mar 9 14:46:22 CET 2009


Author: phk
Date: 2009-03-09 14:46:22 +0100 (Mon, 09 Mar 2009)
New Revision: 3905

Modified:
   trunk/varnish-cache/bin/varnishd/cache.h
   trunk/varnish-cache/bin/varnishd/cache_expire.c
   trunk/varnish-cache/bin/varnishd/cache_pool.c
Log:
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: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h	2009-03-09 13:25:38 UTC (rev 3904)
+++ trunk/varnish-cache/bin/varnishd/cache.h	2009-03-09 13:46:22 UTC (rev 3905)
@@ -573,6 +573,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: trunk/varnish-cache/bin/varnishd/cache_expire.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_expire.c	2009-03-09 13:25:38 UTC (rev 3904)
+++ trunk/varnish-cache/bin/varnishd/cache_expire.c	2009-03-09 13:46:22 UTC (rev 3905)
@@ -209,29 +209,12 @@
  */
 
 static void *
-exp_timer(void *arg)
+exp_timer(struct sess *sp)
 {
-	struct worker ww;
 	struct objcore *oc;
 	struct object *o;
 	double t;
-	struct sess *sp;
-	unsigned char logbuf[1024];		/* XXX size ? */
-	struct dstat stats;
 
-	THR_SetName("cache-timeout");
-	(void)arg;
-
-	sp = SES_New(NULL, 0);
-	XXXAN(sp);
-	memset(&ww, 0, sizeof ww);
-	memset(&stats, 0, sizeof stats);
-	sp->wrk = &ww;
-	ww.magic = WORKER_MAGIC;
-	ww.wlp = ww.wlb = logbuf;
-	ww.wle = logbuf + sizeof logbuf;
-	ww.stats = &stats;
-
 	AZ(sleep(10));		/* XXX: Takes time for VCL to arrive */
 	VCL_Get(&sp->vcl);
 	t = TIM_real();
@@ -241,8 +224,8 @@
 		CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC);
 		if (oc == NULL || oc->timer_when > t) { /* XXX: > or >= ? */
 			Lck_Unlock(&exp_mtx);
-			WSL_Flush(&ww, 0);
-			WRK_SumStat(&ww);
+			WSL_Flush(sp->wrk, 0);
+			WRK_SumStat(sp->wrk);
 			AZ(sleep(1));
 			VCL_Refresh(&sp->vcl);
 			t = TIM_real();
@@ -267,14 +250,14 @@
 		assert(oc->flags & OC_F_ONLRU);
 		Lck_Unlock(&exp_mtx);
 
-		WSL(&ww, SLT_ExpPick, 0, "%u TTL", o->xid);
+		WSL(sp->wrk, SLT_ExpPick, 0, "%u TTL", o->xid);
 
 		sp->obj = o;
 		VCL_timeout_method(sp);
 		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(oc->timer_idx == BINHEAP_NOIDX);
@@ -283,7 +266,7 @@
 		oc->flags &= ~OC_F_ONLRU;
 		VSL_stats->n_expired++;
 		Lck_Unlock(&exp_mtx);
-		HSH_Deref(&ww, &o);
+		HSH_Deref(sp->wrk, &o);
 	}
 }
 
@@ -402,5 +385,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: trunk/varnish-cache/bin/varnishd/cache_pool.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_pool.c	2009-03-09 13:25:38 UTC (rev 3904)
+++ trunk/varnish-cache/bin/varnishd/cache_pool.c	2009-03-09 13:46:22 UTC (rev 3905)
@@ -677,6 +677,59 @@
 	}
 }
 
+/*--------------------------------------------------------------------
+ * 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 ? */
+	struct dstat stats;
+
+	CAST_OBJ_NOTNULL(bt, arg, BGTHREAD_MAGIC);
+	THR_SetName(bt->name);
+	sp = SES_New(NULL, 0);
+	XXXAN(sp);
+	memset(&ww, 0, sizeof ww);
+	memset(&stats, 0, sizeof stats);
+	sp->wrk = &ww;
+	ww.magic = WORKER_MAGIC;
+	ww.wlp = ww.wlb = logbuf;
+	ww.wle = logbuf + sizeof logbuf;
+	ww.stats = &stats;
+
+	(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