From 23444302d9200bcc41ce279a529f73cad63c3f05 Mon Sep 17 00:00:00 2001 From: normal Date: Fri, 4 Jan 2019 13:14:11 +0000 Subject: introduce rb_nogvl C-API to mark ubf as async-signal-safe zlib and bignum both contain unblocking functions which are async-signal-safe and do not require spawning additional threads. We can execute those functions directly in signal handlers without incurring overhead of extra threads, so provide C-API users the ability to deal with that. Other C-API users may have similar need. This flexible API can supercede existing uses of rb_thread_call_without_gvl and rb_thread_call_without_gvl2 by introducing a flags argument to control behavior. Note: this API is NOT finalized. It needs approval from other committers. I prefer shorter name than previous rb_thread_call_without_gvl* functions because my eyes requires big fonts. [Bug #15499] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66712 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/-test-/gvl/call_without_gvl/call_without_gvl.c | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'ext/-test-') diff --git a/ext/-test-/gvl/call_without_gvl/call_without_gvl.c b/ext/-test-/gvl/call_without_gvl/call_without_gvl.c index f3071d5768..654c979479 100644 --- a/ext/-test-/gvl/call_without_gvl/call_without_gvl.c +++ b/ext/-test-/gvl/call_without_gvl/call_without_gvl.c @@ -27,8 +27,49 @@ thread_runnable_sleep(VALUE thread, VALUE timeout) return thread; } +struct loop_ctl { + int notify_fd; + volatile int stop; +}; + +static void * +do_loop(void *p) +{ + struct loop_ctl *ctl = p; + + /* tell the waiting process they can interrupt us, now */ + write(ctl->notify_fd, "", 1); + + while (!ctl->stop) { + struct timeval tv = { 0, 10000 }; + select(0, NULL, NULL, NULL, &tv); + } + return 0; +} + +static void +stop_set(void *p) +{ + struct loop_ctl *ctl = p; + + ctl->stop = 1; +} + +static VALUE +thread_ubf_async_safe(VALUE thread, VALUE notify_fd) +{ + struct loop_ctl ctl; + + ctl.notify_fd = NUM2INT(notify_fd); + ctl.stop = 0; + + rb_nogvl(do_loop, &ctl, stop_set, &ctl, RB_NOGVL_UBF_ASYNC_SAFE); + return thread; +} + void Init_call_without_gvl(void) { rb_define_method(rb_cThread, "__runnable_sleep__", thread_runnable_sleep, 1); + rb_define_method(rb_cThread, "__ubf_async_safe__", thread_ubf_async_safe, 1); } -- cgit v1.2.3