comparison 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
comparison
equal deleted inserted replaced
1307:d2ad997ca157 1308:714e8b84221b
68 } 68 }
69 69
70 return ctx; 70 return ctx;
71 } 71 }
72 72
73 static void *ticker(void *data) {
74 while (1) {
75 usleep(100000);
76 ++uw_time;
77 }
78
79 return NULL;
80 }
81
73 void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) { 82 void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) {
74 uw_context ctx; 83 uw_context ctx;
75 failure_kind fk; 84 failure_kind fk;
76 85
77 uw_global_init(); 86 uw_global_init();
78 uw_app_init(app); 87 uw_app_init(app);
88
89 {
90 pthread_t thread;
91
92 if (uw_time_max && pthread_create(&thread, NULL, ticker, NULL)) {
93 fprintf(stderr, "Error creating ticker thread\n");
94 exit(1);
95 }
96 }
79 97
80 ctx = uw_request_new_context(app, logger_data, log_error, log_debug); 98 ctx = uw_request_new_context(app, logger_data, log_error, log_debug);
81 99
82 if (!ctx) 100 if (!ctx)
83 exit(1); 101 exit(1);
346 if (path_len + 1 > rc->path_copy_size) { 364 if (path_len + 1 > rc->path_copy_size) {
347 rc->path_copy_size = path_len + 1; 365 rc->path_copy_size = path_len + 1;
348 rc->path_copy = realloc(rc->path_copy, rc->path_copy_size); 366 rc->path_copy = realloc(rc->path_copy, rc->path_copy_size);
349 } 367 }
350 strcpy(rc->path_copy, path); 368 strcpy(rc->path_copy, path);
369
370 uw_set_deadline(ctx, uw_time + uw_time_max);
351 fk = uw_begin(ctx, rc->path_copy); 371 fk = uw_begin(ctx, rc->path_copy);
352 } else 372 } else {
373 uw_set_deadline(ctx, uw_time + uw_time_max);
353 fk = uw_begin_onError(ctx, errmsg); 374 fk = uw_begin_onError(ctx, errmsg);
375 }
354 376
355 if (fk == SUCCESS || fk == RETURN_INDIRECTLY) { 377 if (fk == SUCCESS || fk == RETURN_INDIRECTLY) {
356 uw_commit(ctx); 378 uw_commit(ctx);
357 if (uw_has_error(ctx) && !had_error) { 379 if (uw_has_error(ctx) && !had_error) {
358 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx)); 380 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
359
360 uw_reset_keep_error_message(ctx); 381 uw_reset_keep_error_message(ctx);
361 on_failure(ctx); 382 on_failure(ctx);
362 383
363 if (uw_get_app(ctx)->on_error) { 384 if (uw_get_app(ctx)->on_error) {
364 had_error = 1; 385 had_error = 1;