-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsystem_timer_native.c
More file actions
325 lines (277 loc) · 10.8 KB
/
system_timer_native.c
File metadata and controls
325 lines (277 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* SystemTimer native implementation relying on ITIMER_REAL
*
* Copyright 2008 David Vollbracht & Philippe Hanrigou
*/
#include "ruby.h"
#include "rubysig.h"
#include <signal.h>
#include <errno.h>
#include <stdarg.h>
#define DISPLAY_ERRNO 1
#define DO_NOT_DISPLAY_ERRNO 0
#define MICRO_SECONDS 1000000.0
#define MINIMUM_TIMER_INTERVAL_IN_SECONDS 0.2
static VALUE rb_cSystemTimer;
// Ignore most of this for Rubinius
#ifndef RUBINIUS
static sigset_t original_mask;
static sigset_t sigalarm_mask;
static struct sigaction original_signal_handler;
static struct itimerval original_timer_interval;
static int debug_enabled = 0;
static void clear_pending_sigalrm_for_ruby_threads();
static void install_ruby_sigalrm_handler(VALUE);
static void restore_original_ruby_sigalrm_handler(VALUE);
static void restore_original_sigalrm_mask_when_blocked();
static void restore_original_timer_interval();
static void set_itimerval_with_minimum_1s_interval(struct itimerval *, VALUE);
static void set_itimerval(struct itimerval *, double);
static void restore_sigalrm_mask(sigset_t *previous_mask);
static void log_debug(char*, ...);
static void log_error(char*, int);
static VALUE install_first_timer_and_save_original_configuration(VALUE self, VALUE seconds)
{
struct itimerval timer_interval;
if (debug_enabled) {
log_debug("[install_first_timer] %.2lfs\n", NUM2DBL(seconds));
}
/*
* Block SIG_ALRM for safe processing of SIG_ALRM configuration and save mask.
*/
if (0 != sigprocmask(SIG_BLOCK, &sigalarm_mask, &original_mask)) {
log_error("[install_first_timer] Could not block SIG_ALRM\n", DISPLAY_ERRNO);
return Qnil;
}
clear_pending_sigalrm_for_ruby_threads();
log_debug("[install_first_timer] Successfully blocked SIG_ALRM at O.S. level\n");
/*
* Save previous signal handler.
*/
log_debug("[install_first_timer] Saving original system handler\n");
original_signal_handler.sa_handler = NULL;
if (0 != sigaction(SIGALRM, NULL, &original_signal_handler)) {
log_error("[install_first_timer] Could not save existing handler for SIG_ALRM\n", DISPLAY_ERRNO);
restore_original_sigalrm_mask_when_blocked();
return Qnil;
}
log_debug("[install_first_timer] Successfully saved existing SIG_ALRM handler\n");
/*
* Install Ruby Level SIG_ALRM handler
*/
install_ruby_sigalrm_handler(self);
/*
* Save original real time interval timer and aet new real time interval timer.
*/
set_itimerval(&original_timer_interval, 0.0);
set_itimerval_with_minimum_1s_interval(&timer_interval, seconds);
if (0 != setitimer(ITIMER_REAL, &timer_interval, &original_timer_interval)) {
log_error("[install_first_timer] Could not install our own timer, timeout will not work", DISPLAY_ERRNO);
restore_original_ruby_sigalrm_handler(self);
restore_original_sigalrm_mask_when_blocked();
return Qnil;
}
if (debug_enabled) {
log_debug("[install_first_timer] Successfully installed timer (%ds)\n",
timer_interval.it_value.tv_sec);
}
/*
* Unblock SIG_ALRM
*/
if (0 != sigprocmask(SIG_UNBLOCK, &sigalarm_mask, NULL)) {
log_error("[install_first_timer] Could not unblock SIG_ALRM, timeout will not work", DISPLAY_ERRNO);
restore_original_timer_interval();
restore_original_ruby_sigalrm_handler(self);
restore_original_sigalrm_mask_when_blocked();
}
log_debug("[install_first_timer] Successfully unblocked SIG_ALRM.\n");
return Qnil;
}
static VALUE install_next_timer(VALUE self, VALUE seconds)
{
struct itimerval timer_interval;
sigset_t previous_sigalarm_mask;
if (debug_enabled) {
log_debug("[install_next_timer] %.2lfs\n", NUM2DBL(seconds));
}
/*
* Block SIG_ALRM for safe processing of SIG_ALRM configuration and save mask.
*/
if (0 != sigprocmask(SIG_BLOCK, &sigalarm_mask, &previous_sigalarm_mask)) {
log_error("[install_next_timer] Could not block SIG_ALRM\n", DISPLAY_ERRNO);
return Qnil;
}
clear_pending_sigalrm_for_ruby_threads();
log_debug("[install_next_timer] Successfully blocked SIG_ALRM at O.S. level\n");
/*
* Set new real time interval timer.
*/
set_itimerval_with_minimum_1s_interval(&timer_interval, seconds);
if (0 != setitimer(ITIMER_REAL, &timer_interval, NULL)) {
log_error("[install_next_timer] Could not install our own timer, timeout will not work", DISPLAY_ERRNO);
restore_sigalrm_mask(&previous_sigalarm_mask);
return Qnil;
}
if (debug_enabled) {
log_debug("[install_next_timer] Successfully installed timer (%ds + %dus)\n",
timer_interval.it_value.tv_sec, timer_interval.it_value.tv_usec);
}
/*
* Unblock SIG_ALRM
*/
if (0 != sigprocmask(SIG_UNBLOCK, &sigalarm_mask, NULL)) {
log_error("[install_next_timer] Could not unblock SIG_ALRM, timeout will not work", DISPLAY_ERRNO);
restore_sigalrm_mask(&previous_sigalarm_mask);
}
log_debug("[install_next_timer] Successfully unblocked SIG_ALRM.\n");
return Qnil;
}
static VALUE restore_original_configuration(VALUE self)
{
/*
* Block SIG_ALRM for safe processing of SIG_ALRM configuration.
*/
if (0 != sigprocmask(SIG_BLOCK, &sigalarm_mask, NULL)) {
log_error("restore_original_configuration: Could not block SIG_ALRM", errno);
}
clear_pending_sigalrm_for_ruby_threads();
log_debug("[restore_original_configuration] Blocked SIG_ALRM\n");
/*
* Install Ruby Level SIG_ALRM handler
*/
restore_original_ruby_sigalrm_handler(self);
if (original_signal_handler.sa_handler == NULL) {
log_error("[restore_original_configuration] Previous SIG_ALRM handler not initialized!", DO_NOT_DISPLAY_ERRNO);
} else if (0 == sigaction(SIGALRM, &original_signal_handler, NULL)) {
log_debug("[restore_original_configuration] Successfully restored previous handler for SIG_ALRM\n");
} else {
log_error("[restore_original_configuration] Could not restore previous handler for SIG_ALRM", DISPLAY_ERRNO);
}
original_signal_handler.sa_handler = NULL;
restore_original_timer_interval();
restore_original_sigalrm_mask_when_blocked();
}
/*
* Restore original timer the way it was originally set. **WARNING** Breaks original timer semantics
*
* Not bothering to calculate how much time is left or if the timer already expired
* based on when the original timer was set and how much time is passed, just resetting
* the original timer as is for the sake of simplicity.
*
*/
static void restore_original_timer_interval() {
if (0 != setitimer(ITIMER_REAL, &original_timer_interval, NULL)) {
log_error("[restore_original_configuration] Could not restore original timer", DISPLAY_ERRNO);
}
log_debug("[restore_original_configuration] Successfully restored original timer\n");
}
static void restore_sigalrm_mask(sigset_t *previous_mask)
{
if (!sigismember(previous_mask, SIGALRM)) {
sigprocmask(SIG_UNBLOCK, &sigalarm_mask, NULL);
log_debug("[restore_sigalrm_mask] Unblocked SIG_ALRM\n");
} else {
log_debug("[restore_sigalrm_mask] No Need to unblock SIG_ALRM\n");
}
}
static void restore_original_sigalrm_mask_when_blocked()
{
restore_sigalrm_mask(&original_mask);
}
static void install_ruby_sigalrm_handler(VALUE self) {
rb_thread_critical = 1;
rb_funcall(self, rb_intern("install_ruby_sigalrm_handler"), 0);
rb_thread_critical = 0;
}
static void restore_original_ruby_sigalrm_handler(VALUE self) {
rb_thread_critical = 1;
rb_funcall(self, rb_intern("restore_original_ruby_sigalrm_handler"), 0);
rb_thread_critical = 0;
}
static VALUE debug_enabled_p(VALUE self) {
return debug_enabled ? Qtrue : Qfalse;
}
static VALUE enable_debug(VALUE self) {
debug_enabled = 1;
return Qnil;
}
static VALUE disable_debug(VALUE self) {
debug_enabled = 0;
return Qnil;
}
static void log_debug(char* message, ...)
{
va_list argp;
if (0 != debug_enabled) {
va_start(argp, message);
vfprintf(stdout, message, argp);
va_end(argp);
}
return;
}
static void log_error(char* message, int display_errno)
{
fprintf(stderr, "%s: %s\n", message, display_errno ? strerror(errno) : "");
return;
}
/*
* The intent is to clear SIG_ALRM signals at the Ruby level (green threads),
* eventually triggering existing SIG_ALRM handler as a courtesy.
*
* As we cannot access trap_pending_list outside of signal.c our best fallback option
* is to trigger all pending signals at the Ruby level (potentially triggering
* green thread scheduling).
*/
static void clear_pending_sigalrm_for_ruby_threads()
{
CHECK_INTS;
log_debug("[native] Successfully triggered all pending signals at Green Thread level\n");
}
static void init_sigalarm_mask()
{
sigemptyset(&sigalarm_mask);
sigaddset(&sigalarm_mask, SIGALRM);
return;
}
static void set_itimerval_with_minimum_1s_interval(struct itimerval *value,
VALUE seconds) {
double sanitized_second_interval;
sanitized_second_interval = NUM2DBL(seconds) + MINIMUM_TIMER_INTERVAL_IN_SECONDS;
if (sanitized_second_interval < MINIMUM_TIMER_INTERVAL_IN_SECONDS ) {
sanitized_second_interval = MINIMUM_TIMER_INTERVAL_IN_SECONDS;
}
set_itimerval(value, sanitized_second_interval);
}
static void set_itimerval(struct itimerval *value, double seconds) {
if (debug_enabled) {
log_debug("[set_itimerval] %.3lfs\n", seconds);
}
value->it_interval.tv_usec = 0;
value->it_interval.tv_sec = 0;
value->it_value.tv_sec = (long int) (seconds);
value->it_value.tv_usec = (long int) ((seconds - value->it_value.tv_sec) \
* MICRO_SECONDS);
if (debug_enabled) {
log_debug("[set_itimerval] Set to %ds + %dus\n", value->it_value.tv_sec,
value->it_value.tv_usec);
}
return;
}
void Init_system_timer_native()
{
init_sigalarm_mask();
rb_cSystemTimer = rb_define_module("SystemTimer");
rb_define_singleton_method(rb_cSystemTimer, "install_first_timer_and_save_original_configuration", install_first_timer_and_save_original_configuration, 1);
rb_define_singleton_method(rb_cSystemTimer, "install_next_timer", install_next_timer, 1);
rb_define_singleton_method(rb_cSystemTimer, "restore_original_configuration", restore_original_configuration, 0);
rb_define_singleton_method(rb_cSystemTimer, "debug_enabled?", debug_enabled_p, 0);
rb_define_singleton_method(rb_cSystemTimer, "enable_debug", enable_debug, 0);
rb_define_singleton_method(rb_cSystemTimer, "disable_debug", disable_debug, 0);
}
#else
// Exists just to make things happy
void Init_system_timer_native()
{
rb_cSystemTimer = rb_define_module("SystemTimer");
}
#endif