From 5b43edd70cad4944fc9eadf9477613daa931bc31 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 15 Feb 2014 23:04:52 +0000 Subject: use rb_thread_call_without_gvl for Ruby 2+ rb_thread_blocking_region is deprecated and will be removed --- ext/clogger_ext/blocking_helpers.h | 34 ++++++++++++++++++++++++++-------- ext/clogger_ext/extconf.rb | 1 + 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/ext/clogger_ext/blocking_helpers.h b/ext/clogger_ext/blocking_helpers.h index bb366ff..c03c2f5 100644 --- a/ext/clogger_ext/blocking_helpers.h +++ b/ext/clogger_ext/blocking_helpers.h @@ -1,27 +1,45 @@ -#ifdef HAVE_RB_THREAD_BLOCKING_REGION -struct stat_args { const char *path; struct stat *buf; }; -static VALUE ng_stat(void *ptr) +#if defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL) && defined(HAVE_RUBY_THREAD_H) +/* Ruby 2.0+ */ +# include +# define WITHOUT_GVL(fn,a,ubf,b) \ + rb_thread_call_without_gvl((fn),(a),(ubf),(b)) +#elif defined(HAVE_RB_THREAD_BLOCKING_REGION) +typedef VALUE (*my_blocking_fn_t)(void*); +# define WITHOUT_GVL(fn,a,ubf,b) \ + rb_thread_blocking_region((my_blocking_fn_t)(fn),(a),(ubf),(b)) +#endif + +#ifdef WITHOUT_GVL +struct stat_args { int err; const char *path; struct stat *buf; }; +static void * ng_stat(void *ptr) { struct stat_args *a = ptr; - return (VALUE)stat(a->path, a->buf); + a->err = stat(a->path, a->buf); + return NULL; } + static int my_stat(const char *path, struct stat *buf) { struct stat_args a; a.path = path; a.buf = buf; - return (int)rb_thread_blocking_region(ng_stat, &a, RUBY_UBF_IO, 0); + WITHOUT_GVL(ng_stat, &a, RUBY_UBF_IO, 0); + return a.err; } + #ifndef HAVE_RB_THREAD_IO_BLOCKING_REGION # define rb_thread_io_blocking_region(fn,data,fd) \ - rb_thread_blocking_region((fn),(data), RUBY_UBF_IO, 0) + WITHOUT_GVL((fn),(data), RUBY_UBF_IO, 0) +#else + VALUE rb_thread_io_blocking_region(VALUE(*)(void *), void *, int); #endif struct write_args { int fd; const void *buf; size_t count; }; static VALUE ng_write(void *ptr) { struct write_args *a = ptr; + return (VALUE)write(a->fd, a->buf, a->count); } static ssize_t my_write(int fd, const void *buf, size_t count) @@ -36,6 +54,6 @@ static ssize_t my_write(int fd, const void *buf, size_t count) return r; } -# define stat(fd,buf) my_stat((fd),(buf)) +# define stat(path,buf) my_stat((path),(buf)) # define write(fd,buf,count) my_write((fd),(buf),(count)) -#endif +#endif /* !WITHOUT_GVL */ diff --git a/ext/clogger_ext/extconf.rb b/ext/clogger_ext/extconf.rb index 99f7deb..523b314 100644 --- a/ext/clogger_ext/extconf.rb +++ b/ext/clogger_ext/extconf.rb @@ -19,6 +19,7 @@ begin have_func('gmtime_r', 'time.h') or raise "gmtime_r needed" have_struct_member('struct tm', 'tm_gmtoff', 'time.h') have_func('rb_str_set_len', 'ruby.h') + have_func('rb_thread_call_without_gvl', 'ruby/thread.h') have_func('rb_thread_blocking_region', 'ruby.h') have_func('rb_thread_io_blocking_region', 'ruby.h') create_makefile('clogger_ext') -- cgit v1.2.3-24-ge0c7