diff src/c/request.c @ 1308:714e8b84221b

-limit for running time
author Adam Chlipala <adam@chlipala.net>
date Thu, 14 Oct 2010 11:35:56 -0400
parents b4480a56cab7
children add5ae41969e
line wrap: on
line diff
--- a/src/c/request.c	Thu Oct 14 11:06:26 2010 -0400
+++ b/src/c/request.c	Thu Oct 14 11:35:56 2010 -0400
@@ -70,6 +70,15 @@
   return ctx;
 }
 
+static void *ticker(void *data) {
+  while (1) {
+    usleep(100000);
+    ++uw_time;
+  }
+
+  return NULL;
+}
+
 void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) {
   uw_context ctx;
   failure_kind fk;
@@ -77,6 +86,15 @@
   uw_global_init();
   uw_app_init(app);
 
+  {
+    pthread_t thread;
+    
+    if (uw_time_max && pthread_create(&thread, NULL, ticker, NULL)) {
+      fprintf(stderr, "Error creating ticker thread\n");
+      exit(1);
+    }
+  }
+
   ctx = uw_request_new_context(app, logger_data, log_error, log_debug);
 
   if (!ctx)
@@ -348,15 +366,18 @@
         rc->path_copy = realloc(rc->path_copy, rc->path_copy_size);
       }
       strcpy(rc->path_copy, path);
+
+      uw_set_deadline(ctx, uw_time + uw_time_max);
       fk = uw_begin(ctx, rc->path_copy);
-    } else
+    } else {
+      uw_set_deadline(ctx, uw_time + uw_time_max);
       fk = uw_begin_onError(ctx, errmsg);
+    }
 
     if (fk == SUCCESS || fk == RETURN_INDIRECTLY) {
       uw_commit(ctx);
       if (uw_has_error(ctx) && !had_error) {
         log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
-
         uw_reset_keep_error_message(ctx);
         on_failure(ctx);