diff src/c/request.c @ 1349:87156c44824f

Periodic tasks
author Adam Chlipala <adam@chlipala.net>
date Sat, 18 Dec 2010 15:17:09 -0500
parents b106ca8200b1
children 44a12a321150
line wrap: on
line diff
--- a/src/c/request.c	Sat Dec 18 14:17:45 2010 -0500
+++ b/src/c/request.c	Sat Dec 18 15:17:09 2010 -0500
@@ -79,9 +79,44 @@
   return NULL;
 }
 
+typedef struct {
+  uw_app *app;
+  void *logger_data;
+  uw_logger log_error, log_debug;
+} loggers;
+
+typedef struct {
+  loggers *ls;
+  uw_periodic pdic;
+} periodic;
+
+static void *periodic_loop(void *data) {
+  periodic *p = (periodic *)data;
+  uw_context ctx = uw_request_new_context(p->ls->app, p->ls->logger_data, p->ls->log_error, p->ls->log_debug);
+
+  if (!ctx)
+    exit(1);
+
+  while (1) {
+    failure_kind r;
+    do {
+      r = uw_runCallback(ctx, p->pdic.callback);
+    } while (r == UNLIMITED_RETRY);
+
+    sleep(p->pdic.period);
+  };
+}
+
 void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) {
   uw_context ctx;
   failure_kind fk;
+  uw_periodic *ps;
+  loggers *ls = malloc(sizeof(loggers));
+
+  ls->app = app;
+  ls->logger_data = logger_data;
+  ls->log_error = log_error;
+  ls->log_debug = log_debug;
 
   uw_global_init();
   uw_app_init(app);
@@ -113,6 +148,18 @@
   }
 
   uw_free(ctx);
+
+  for (ps = app->periodics; ps->callback; ++ps) {
+    pthread_t thread;
+    periodic *arg = malloc(sizeof(periodic));
+    arg->ls = ls;
+    arg->pdic = *ps;
+    
+    if (pthread_create(&thread, NULL, periodic_loop, arg)) {
+      fprintf(stderr, "Error creating periodic thread\n");
+      exit(1);
+    }
+  }  
 }
 
 
@@ -468,12 +515,6 @@
   }
 }
 
-typedef struct {
-  uw_app *app;
-  void *logger_data;
-  uw_logger log_error, log_debug;
-} loggers;
-
 void *client_pruner(void *data) {
   loggers *ls = (loggers *)data;
   uw_context ctx = uw_request_new_context(ls->app, ls->logger_data, ls->log_error, ls->log_debug);