1 /* This file is part of the CivetWeb web server.
2 * See https://github.com/civetweb/civetweb/
3 * (C) 2014-2021 by the CivetWeb authors, MIT license.
6 #if !defined(MAX_TIMERS)
7 #define MAX_TIMERS MAX_WORKER_THREADS
9 #if !defined(TIMER_RESOLUTION)
10 /* Timer resolution in ms */
11 #define TIMER_RESOLUTION (10)
14 typedef int (*taction)(void *arg);
15 typedef void (*tcancelaction)(void *arg);
26 pthread_t threadid; /* Timer thread ID */
27 pthread_mutex_t mutex; /* Protects timer lists */
28 struct ttimer *timers; /* List of timers */
29 unsigned timer_count; /* Current size of timer list */
30 unsigned timer_capacity; /* Capacity of timer list */
39 timer_getcurrenttime(struct mg_context *ctx)
42 /* GetTickCount returns milliseconds since system start as
43 * unsigned 32 bit value. It will wrap around every 49.7 days.
44 * We need to use a 64 bit counter (will wrap in 500 mio. years),
45 * by adding the 32 bit difference since the last call to a
46 * 64 bit counter. This algorithm will only work, if this
47 * function is called at least once every 7 weeks. */
48 uint64_t now_tick64 = 0;
49 DWORD now_tick = GetTickCount();
52 pthread_mutex_lock(&ctx->timers->mutex);
53 ctx->timers->now_tick64 += now_tick - ctx->timers->last_tick;
54 now_tick64 = ctx->timers->now_tick64;
55 ctx->timers->last_tick = now_tick;
56 pthread_mutex_unlock(&ctx->timers->mutex);
58 return (double)now_tick64 * 1.0E-3;
60 struct timespec now_ts;
63 clock_gettime(CLOCK_MONOTONIC, &now_ts);
64 return (double)now_ts.tv_sec + (double)now_ts.tv_nsec * 1.0E-9;
70 timer_add(struct mg_context *ctx,
85 now = timer_getcurrenttime(ctx);
87 /* HCP24: if is_relative = 0 and next_time < now
88 * action will be called so fast as possible
89 * if additional period > 0
90 * action will be called so fast as possible
91 * n times until (next_time + (n * period)) > now
92 * then the period is working
94 * if next_time < now then we set next_time = now.
95 * The first callback will be so fast as possible (now)
96 * but the next callback on period
102 /* You can not set timers into the past */
103 if (next_time < now) {
107 pthread_mutex_lock(&ctx->timers->mutex);
108 if (ctx->timers->timer_count == MAX_TIMERS) {
110 } else if (ctx->timers->timer_count == ctx->timers->timer_capacity) {
111 unsigned capacity = (ctx->timers->timer_capacity * 2) + 1;
112 struct ttimer *timers =
113 (struct ttimer *)mg_realloc_ctx(ctx->timers->timers,
114 capacity * sizeof(struct ttimer),
117 ctx->timers->timers = timers;
118 ctx->timers->timer_capacity = capacity;
124 /* Insert new timer into a sorted list. */
125 /* The linear list is still most efficient for short lists (small
126 * number of timers) - if there are many timers, different
127 * algorithms will work better. */
128 unsigned u = ctx->timers->timer_count;
129 for (; (u > 0) && (ctx->timers->timers[u - 1].time > next_time); u--) {
130 ctx->timers->timers[u] = ctx->timers->timers[u - 1];
132 ctx->timers->timers[u].time = next_time;
133 ctx->timers->timers[u].period = period;
134 ctx->timers->timers[u].action = action;
135 ctx->timers->timers[u].arg = arg;
136 ctx->timers->timers[u].cancel = cancel;
137 ctx->timers->timer_count++;
139 pthread_mutex_unlock(&ctx->timers->mutex);
145 timer_thread_run(void *thread_func_param)
147 struct mg_context *ctx = (struct mg_context *)thread_func_param;
153 mg_set_thread_name("timer");
155 if (ctx->callbacks.init_thread) {
157 ctx->callbacks.init_thread(ctx, 2);
160 /* Timer main loop */
161 d = timer_getcurrenttime(ctx);
162 while (STOP_FLAG_IS_ZERO(&ctx->stop_flag)) {
163 pthread_mutex_lock(&ctx->timers->mutex);
164 if ((ctx->timers->timer_count > 0)
165 && (d >= ctx->timers->timers[0].time)) {
166 /* Timer list is sorted. First action should run now. */
167 /* Store active timer in "t" */
168 t = ctx->timers->timers[0];
170 /* Shift all other timers */
171 for (u = 1; u < ctx->timers->timer_count; u++) {
172 ctx->timers->timers[u - 1] = ctx->timers->timers[u];
174 ctx->timers->timer_count--;
176 pthread_mutex_unlock(&ctx->timers->mutex);
178 /* Call timer action */
179 action_res = t.action(t.arg);
181 /* action_res == 1: reschedule */
182 /* action_res == 0: do not reschedule, free(arg) */
183 if ((action_res > 0) && (t.period > 0)) {
184 /* Should schedule timer again */
193 /* Allow user to free timer argument */
194 if (t.cancel != NULL) {
200 pthread_mutex_unlock(&ctx->timers->mutex);
203 /* TIMER_RESOLUTION = 10 ms seems reasonable.
204 * A faster loop (smaller sleep value) increases CPU load,
205 * a slower loop (higher sleep value) decreases timer accuracy.
207 mg_sleep(TIMER_RESOLUTION);
209 d = timer_getcurrenttime(ctx);
212 /* Remove remaining timers */
213 for (u = 0; u < ctx->timers->timer_count; u++) {
214 t = ctx->timers->timers[u];
215 if (t.cancel != NULL) {
223 static unsigned __stdcall timer_thread(void *thread_func_param)
225 timer_thread_run(thread_func_param);
230 timer_thread(void *thread_func_param)
235 memset(&sa, 0, sizeof(sa));
236 sa.sa_handler = SIG_IGN;
237 sigaction(SIGPIPE, &sa, NULL);
239 timer_thread_run(thread_func_param);
246 timers_init(struct mg_context *ctx)
248 /* Initialize timers data structure */
250 (struct ttimers *)mg_calloc_ctx(sizeof(struct ttimers), 1, ctx);
255 ctx->timers->timers = NULL;
257 /* Initialize mutex */
258 if (0 != pthread_mutex_init(&ctx->timers->mutex, NULL)) {
259 mg_free(ctx->timers);
264 /* For some systems timer_getcurrenttime does some initialization
265 * during the first call. Call it once now, ignore the result. */
266 (void)timer_getcurrenttime(ctx);
268 /* Start timer thread */
269 if (mg_start_thread_with_id(timer_thread, ctx, &ctx->timers->threadid)
271 (void)pthread_mutex_destroy(&ctx->timers->mutex);
272 mg_free(ctx->timers);
282 timers_exit(struct mg_context *ctx)
285 mg_join_thread(ctx->timers->threadid);
286 (void)pthread_mutex_destroy(&ctx->timers->mutex);
287 mg_free(ctx->timers->timers);
288 mg_free(ctx->timers);
294 /* End of timer.inl */