clogger RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <normalperson@yhbt.net>
To: clogger@librelist.org
Subject: [PATCH 6/6] use rb_thread_call_without_gvl for Ruby 2+
Date: Sat, 15 Feb 2014 23:09:08 +0000	[thread overview]
Message-ID: <1392505748-13740-6-git-send-email-normalperson@yhbt.net> (raw)
In-Reply-To: 1392505748-13740-1-git-send-email-normalperson@yhbt.net

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 <ruby/thread.h>
+#  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')
-- 
1.9.0.rc3.13.gda73b5f



      parent reply	other threads:[~2014-02-15 23:09 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-15 23:09 [PATCH 1/6] blocking_helpers: remove fstat wrapper Eric Wong
2014-02-15 23:09 ` [PATCH 2/6] remove unused RARRAY_PTR macro Eric Wong
2014-02-15 23:09 ` [PATCH 3/6] use RB_GC_GUARD instead of volatile Eric Wong
2014-02-15 23:09 ` [PATCH 4/6] prevent potential premature GC in byte_xs Eric Wong
2014-02-15 23:09 ` [PATCH 5/6] remove each_id, it was never used Eric Wong
2014-02-15 23:09 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/clogger/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1392505748-13740-6-git-send-email-normalperson@yhbt.net \
    --to=normalperson@yhbt.net \
    --cc=clogger@librelist.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/clogger.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).