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
|
# File 'ext/extreme_timeout/extreme_timeout.c', line 21
VALUE
timeout(int argc, VALUE *argv, VALUE self)
{
int exitcode = 1;
unsigned int timeout_sec = 0;
VALUE timeout_sec_value, exitcode_value, block;
pthread_t thread;
struct wait_args arg;
VALUE retval;
rb_scan_args(argc, argv, "11&", &timeout_sec_value, &exitcode_value, &block);
if (!FIXNUM_P(timeout_sec_value)) {
rb_raise(rb_eArgError, "the timeout argument should be Fixnum");
}
timeout_sec = FIX2UINT(timeout_sec_value);
exitcode = 1;
if (exitcode_value != Qnil) {
if (!FIXNUM_P(exitcode_value)) {
rb_raise(rb_eArgError, "the exitcode argument should be Fixnum");
}
exitcode = FIX2INT(exitcode_value);
}
if (block == Qnil) {
rb_raise(rb_eArgError, "expects block");
}
arg.timeout_sec = timeout_sec;
arg.exitcode = exitcode;
if (pthread_create(&thread, NULL, sleep_thread_main, &arg) != 0) {
rb_raise(rb_eRuntimeError, "pthread_create was failed");
}
retval = rb_funcall(block, rb_intern("call"), 0);
pthread_cancel(thread);
pthread_join(thread, NULL);
return retval;
}
|