# HG changeset patch # User Adam Chlipala # Date 1287070556 14400 # Node ID 714e8b84221b2d0075d7333f6d500a772229c6fc # Parent d2ad997ca1574cdc25eb8f11c37a8ffdcfdcfb2d -limit for running time diff -r d2ad997ca157 -r 714e8b84221b include/urweb.h --- 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 diff -r d2ad997ca157 -r 714e8b84221b src/c/request.c --- 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); diff -r d2ad997ca157 -r 714e8b84221b src/c/urweb.c --- 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 diff -r d2ad997ca157 -r 714e8b84221b src/settings.sml --- 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 diff -r d2ad997ca157 -r 714e8b84221b tests/thog.ur --- /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 {[ack (n, 4)]} diff -r d2ad997ca157 -r 714e8b84221b tests/thog.urp --- /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 diff -r d2ad997ca157 -r 714e8b84221b tests/thog.urs --- /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