changeset 1308:714e8b84221b

-limit for running time
author Adam Chlipala <adam@chlipala.net>
date Thu, 14 Oct 2010 11:35:56 -0400
parents d2ad997ca157
children 127561e4aef1
files include/urweb.h src/c/request.c src/c/urweb.c src/settings.sml tests/thog.ur tests/thog.urp tests/thog.urs
diffstat 7 files changed, 44 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Thu Oct 14 11:06:26 2010 -0400
+++ b/include/urweb.h	Thu Oct 14 11:35:56 2010 -0400
@@ -284,4 +284,6 @@
 
 uw_Basis_int uw_Basis_rand(uw_context);
 
+extern int uw_time_max;
+
 #endif
--- 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);
 
--- a/src/c/urweb.c	Thu Oct 14 11:06:26 2010 -0400
+++ b/src/c/urweb.c	Thu Oct 14 11:35:56 2010 -0400
@@ -348,7 +348,7 @@
   app->client_init();
 }
 
-int uw_time = 0;
+int uw_time = 0, uw_time_max = 0;
 
 
 // Single-request state
--- a/src/settings.sml	Thu Oct 14 11:06:26 2010 -0400
+++ b/src/settings.sml	Thu Oct 14 11:35:56 2010 -0400
@@ -492,12 +492,16 @@
 
 val limits = ["messages", "clients", "headers", "page", "heap", "script",
               "inputs", "subinputs", "cleanup", "deltas", "transactionals",
-              "globals", "database"]
+              "globals", "database", "time"]
 
 val limitsList = ref ([] : (string * int) list)
 fun addLimit (v as (name, _)) =
     if List.exists (fn name' => name' = name) limits then
-        limitsList := v :: !limitsList
+        (limitsList := v :: !limitsList;
+         if name = "time" then
+             setDeadlines true
+         else
+             ())
     else
         raise Fail ("Unknown limit category '" ^ name ^ "'")
 fun limits () = !limitsList
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/thog.ur	Thu Oct 14 11:35:56 2010 -0400
@@ -0,0 +1,9 @@
+fun ack (m, n) =
+    if m = 0 then
+        n + 1
+    else if n = 0 then
+        ack (m - 1, 1)
+    else
+        ack (m - 1, ack (m, n - 1))
+
+fun main n = return <xml>{[ack (n, 4)]}</xml>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/thog.urp	Thu Oct 14 11:35:56 2010 -0400
@@ -0,0 +1,2 @@
+$/list
+thog
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/thog.urs	Thu Oct 14 11:35:56 2010 -0400
@@ -0,0 +1,1 @@
+val main : int -> transaction page