]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/timer.inl
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / civetweb / src / timer.inl
1
2 #if !defined(MAX_TIMERS)
3 #define MAX_TIMERS MAX_WORKER_THREADS
4 #endif
5
6 typedef int (*taction)(void *arg);
7
8 struct ttimer {
9 double time;
10 double period;
11 taction action;
12 void *arg;
13 };
14
15 struct ttimers {
16 pthread_t threadid; /* Timer thread ID */
17 pthread_mutex_t mutex; /* Protects timer lists */
18 struct ttimer timers[MAX_TIMERS]; /* List of timers */
19 unsigned timer_count; /* Current size of timer list */
20 };
21
22 static int
23 timer_add(struct mg_context *ctx,
24 double next_time,
25 double period,
26 int is_relative,
27 taction action,
28 void *arg)
29 {
30 unsigned u, v;
31 int error = 0;
32 struct timespec now;
33
34 if (ctx->stop_flag) {
35 return 0;
36 }
37
38 if (is_relative) {
39 clock_gettime(CLOCK_MONOTONIC, &now);
40 next_time += now.tv_sec;
41 next_time += now.tv_nsec * 1.0E-9;
42 }
43
44 pthread_mutex_lock(&ctx->timers->mutex);
45 if (ctx->timers->timer_count == MAX_TIMERS) {
46 error = 1;
47 } else {
48 for (u = 0; u < ctx->timers->timer_count; u++) {
49 if (ctx->timers->timers[u].time < next_time) {
50 for (v = ctx->timers->timer_count; v > u; v--) {
51 ctx->timers->timers[v] = ctx->timers->timers[v - 1];
52 }
53 break;
54 }
55 }
56 ctx->timers->timers[u].time = next_time;
57 ctx->timers->timers[u].period = period;
58 ctx->timers->timers[u].action = action;
59 ctx->timers->timers[u].arg = arg;
60 ctx->timers->timer_count++;
61 }
62 pthread_mutex_unlock(&ctx->timers->mutex);
63 return error;
64 }
65
66 static void
67 timer_thread_run(void *thread_func_param)
68 {
69 struct mg_context *ctx = (struct mg_context *)thread_func_param;
70 struct timespec now;
71 double d;
72 unsigned u;
73 int re_schedule;
74 struct ttimer t;
75
76 mg_set_thread_name("timer");
77
78 if (ctx->callbacks.init_thread) {
79 /* Timer thread */
80 ctx->callbacks.init_thread(ctx, 2);
81 }
82
83 #if defined(HAVE_CLOCK_NANOSLEEP) /* Linux with librt */
84 /* TODO */
85 while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &request, &request)
86 == EINTR) { /*nop*/
87 ;
88 }
89 #else
90 clock_gettime(CLOCK_MONOTONIC, &now);
91 d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9;
92 while (ctx->stop_flag == 0) {
93 pthread_mutex_lock(&ctx->timers->mutex);
94 if (ctx->timers->timer_count > 0 && d >= ctx->timers->timers[0].time) {
95 t = ctx->timers->timers[0];
96 for (u = 1; u < ctx->timers->timer_count; u++) {
97 ctx->timers->timers[u - 1] = ctx->timers->timers[u];
98 }
99 ctx->timers->timer_count--;
100 pthread_mutex_unlock(&ctx->timers->mutex);
101 re_schedule = t.action(t.arg);
102 if (re_schedule && (t.period > 0)) {
103 timer_add(ctx, t.time + t.period, t.period, 0, t.action, t.arg);
104 }
105 continue;
106 } else {
107 pthread_mutex_unlock(&ctx->timers->mutex);
108 }
109 mg_sleep(1);
110 clock_gettime(CLOCK_MONOTONIC, &now);
111 d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9;
112 }
113 #endif
114 }
115
116 #ifdef _WIN32
117 static unsigned __stdcall timer_thread(void *thread_func_param)
118 {
119 timer_thread_run(thread_func_param);
120 return 0;
121 }
122 #else
123 static void *
124 timer_thread(void *thread_func_param)
125 {
126 timer_thread_run(thread_func_param);
127 return NULL;
128 }
129 #endif /* _WIN32 */
130
131 static int
132 timers_init(struct mg_context *ctx)
133 {
134 ctx->timers = (struct ttimers *)mg_calloc(sizeof(struct ttimers), 1);
135 (void)pthread_mutex_init(&ctx->timers->mutex, NULL);
136
137 /* Start timer thread */
138 mg_start_thread_with_id(timer_thread, ctx, &ctx->timers->threadid);
139
140 return 0;
141 }
142
143 static void
144 timers_exit(struct mg_context *ctx)
145 {
146 if (ctx->timers) {
147 (void)pthread_mutex_destroy(&ctx->timers->mutex);
148 mg_free(ctx->timers);
149 }
150 }