comparison 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
comparison
equal deleted inserted replaced
1348:8a169fc0838b 1349:87156c44824f
77 } 77 }
78 78
79 return NULL; 79 return NULL;
80 } 80 }
81 81
82 typedef struct {
83 uw_app *app;
84 void *logger_data;
85 uw_logger log_error, log_debug;
86 } loggers;
87
88 typedef struct {
89 loggers *ls;
90 uw_periodic pdic;
91 } periodic;
92
93 static void *periodic_loop(void *data) {
94 periodic *p = (periodic *)data;
95 uw_context ctx = uw_request_new_context(p->ls->app, p->ls->logger_data, p->ls->log_error, p->ls->log_debug);
96
97 if (!ctx)
98 exit(1);
99
100 while (1) {
101 failure_kind r;
102 do {
103 r = uw_runCallback(ctx, p->pdic.callback);
104 } while (r == UNLIMITED_RETRY);
105
106 sleep(p->pdic.period);
107 };
108 }
109
82 void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) { 110 void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) {
83 uw_context ctx; 111 uw_context ctx;
84 failure_kind fk; 112 failure_kind fk;
113 uw_periodic *ps;
114 loggers *ls = malloc(sizeof(loggers));
115
116 ls->app = app;
117 ls->logger_data = logger_data;
118 ls->log_error = log_error;
119 ls->log_debug = log_debug;
85 120
86 uw_global_init(); 121 uw_global_init();
87 uw_app_init(app); 122 uw_app_init(app);
88 123
89 { 124 {
111 uw_rollback(ctx, 0); 146 uw_rollback(ctx, 0);
112 exit(1); 147 exit(1);
113 } 148 }
114 149
115 uw_free(ctx); 150 uw_free(ctx);
151
152 for (ps = app->periodics; ps->callback; ++ps) {
153 pthread_t thread;
154 periodic *arg = malloc(sizeof(periodic));
155 arg->ls = ls;
156 arg->pdic = *ps;
157
158 if (pthread_create(&thread, NULL, periodic_loop, arg)) {
159 fprintf(stderr, "Error creating periodic thread\n");
160 exit(1);
161 }
162 }
116 } 163 }
117 164
118 165
119 typedef struct uw_rc { 166 typedef struct uw_rc {
120 size_t path_copy_size; 167 size_t path_copy_size;
466 513
467 uw_reset_keep_request(ctx); 514 uw_reset_keep_request(ctx);
468 } 515 }
469 } 516 }
470 517
471 typedef struct {
472 uw_app *app;
473 void *logger_data;
474 uw_logger log_error, log_debug;
475 } loggers;
476
477 void *client_pruner(void *data) { 518 void *client_pruner(void *data) {
478 loggers *ls = (loggers *)data; 519 loggers *ls = (loggers *)data;
479 uw_context ctx = uw_request_new_context(ls->app, ls->logger_data, ls->log_error, ls->log_debug); 520 uw_context ctx = uw_request_new_context(ls->app, ls->logger_data, ls->log_error, ls->log_debug);
480 521
481 if (!ctx) 522 if (!ctx)