Skip to content

Instantly share code, notes, and snippets.

@hsbt
Created February 15, 2014 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsbt/9026952 to your computer and use it in GitHub Desktop.
Save hsbt/9026952 to your computer and use it in GitHub Desktop.
diff --git a/ext/mysql2/client.c b/ext/mysql2/client.c
index 28f9ced..effa077 100644
--- a/ext/mysql2/client.c
+++ b/ext/mysql2/client.c
@@ -26,7 +26,7 @@ static ID intern_merge, intern_error_number_eql, intern_sql_state_eql;
/*
* used to pass all arguments to mysql_real_connect while inside
- * rb_thread_blocking_region
+ * rb_thread_call_without_gvl
*/
struct nogvl_connect_args {
MYSQL *mysql;
@@ -41,7 +41,7 @@ struct nogvl_connect_args {
/*
* used to pass all arguments to mysql_send_query while inside
- * rb_thread_blocking_region
+ * rb_thread_call_without_gvl
*/
struct nogvl_send_query_args {
MYSQL *mysql;
@@ -212,11 +212,11 @@ static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE po
args.mysql = wrapper->client;
args.client_flag = NUM2ULONG(flags);
- rv = rb_thread_blocking_region(nogvl_connect, &args, RUBY_UBF_IO, 0);
+ rv = rb_thread_call_without_gvl(nogvl_connect, &args, RUBY_UBF_IO, 0);
if (rv == Qfalse) {
while (rv == Qfalse && errno == EINTR) {
errno = 0;
- rv = rb_thread_blocking_region(nogvl_connect, &args, RUBY_UBF_IO, 0);
+ rv = rb_thread_call_without_gvl(nogvl_connect, &args, RUBY_UBF_IO, 0);
}
if (rv == Qfalse)
return rb_raise_mysql2_error(wrapper);
@@ -235,7 +235,7 @@ static VALUE rb_mysql_client_close(VALUE self) {
GET_CLIENT(self);
if (!wrapper->closed) {
- rb_thread_blocking_region(nogvl_close, wrapper, RUBY_UBF_IO, 0);
+ rb_thread_call_without_gvl(nogvl_close, wrapper, RUBY_UBF_IO, 0);
}
return Qnil;
@@ -258,7 +258,7 @@ static VALUE nogvl_send_query(void *ptr) {
static VALUE do_send_query(void *args) {
struct nogvl_send_query_args *query_args = args;
mysql_client_wrapper *wrapper = query_args->wrapper;
- if (rb_thread_blocking_region(nogvl_send_query, args, RUBY_UBF_IO, 0) == Qfalse) {
+ if (rb_thread_call_without_gvl(nogvl_send_query, args, RUBY_UBF_IO, 0) == Qfalse) {
// an error occurred, we're not active anymore
MARK_CONN_INACTIVE(self);
return rb_raise_mysql2_error(wrapper);
@@ -306,13 +306,13 @@ static VALUE rb_mysql_client_async_result(VALUE self) {
return Qnil;
REQUIRE_OPEN_DB(wrapper);
- if (rb_thread_blocking_region(nogvl_read_query_result, wrapper->client, RUBY_UBF_IO, 0) == Qfalse) {
+ if (rb_thread_call_without_gvl(nogvl_read_query_result, wrapper->client, RUBY_UBF_IO, 0) == Qfalse) {
// an error occurred, mark this connection inactive
MARK_CONN_INACTIVE(self);
return rb_raise_mysql2_error(wrapper);
}
- result = (MYSQL_RES *)rb_thread_blocking_region(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
+ result = (MYSQL_RES *)rb_thread_call_without_gvl(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
if (result == NULL) {
if (mysql_errno(wrapper->client) != 0) {
@@ -412,7 +412,7 @@ static VALUE finish_and_mark_inactive(void *args) {
// if we got here, the result hasn't been read off the wire yet
// so lets do that and then throw it away because we have no way
// of getting it back up to the caller from here
- result = (MYSQL_RES *)rb_thread_blocking_region(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
+ result = (MYSQL_RES *)rb_thread_call_without_gvl(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
mysql_free_result(result);
wrapper->active = 0;
@@ -633,7 +633,7 @@ static VALUE rb_mysql_client_ping(VALUE self) {
if (wrapper->closed) {
return Qfalse;
} else {
- return rb_thread_blocking_region(nogvl_ping, wrapper->client, RUBY_UBF_IO, 0);
+ return rb_thread_call_without_gvl(nogvl_ping, wrapper->client, RUBY_UBF_IO, 0);
}
}
@@ -725,7 +725,7 @@ static VALUE set_ssl_options(VALUE self, VALUE key, VALUE cert, VALUE ca, VALUE
static VALUE init_connection(VALUE self) {
GET_CLIENT(self);
- if (rb_thread_blocking_region(nogvl_init, wrapper->client, RUBY_UBF_IO, 0) == Qfalse) {
+ if (rb_thread_call_without_gvl(nogvl_init, wrapper->client, RUBY_UBF_IO, 0) == Qfalse) {
/* TODO: warning - not enough memory? */
return rb_raise_mysql2_error(wrapper);
}
diff --git a/ext/mysql2/client.h b/ext/mysql2/client.h
index af4f763..fbd3b1e 100644
--- a/ext/mysql2/client.h
+++ b/ext/mysql2/client.h
@@ -1,34 +1,6 @@
#ifndef MYSQL2_CLIENT_H
#define MYSQL2_CLIENT_H
-/*
- * partial emulation of the 1.9 rb_thread_blocking_region under 1.8,
- * this is enough for dealing with blocking I/O functions in the
- * presence of threads.
- */
-#ifndef HAVE_RB_THREAD_BLOCKING_REGION
-
-#include <rubysig.h>
-#define RUBY_UBF_IO ((rb_unblock_function_t *)-1)
-typedef void rb_unblock_function_t(void *);
-typedef VALUE rb_blocking_function_t(void *);
-static VALUE
-rb_thread_blocking_region(
- rb_blocking_function_t *func, void *data1,
- RB_MYSQL_UNUSED rb_unblock_function_t *ubf,
- RB_MYSQL_UNUSED void *data2)
-{
- VALUE rv;
-
- TRAP_BEG;
- rv = func(data1);
- TRAP_END;
-
- return rv;
-}
-
-#endif /* ! HAVE_RB_THREAD_BLOCKING_REGION */
-
void init_mysql2_client();
typedef struct {
@@ -39,4 +11,4 @@ typedef struct {
MYSQL *client;
} mysql_client_wrapper;
-#endif
\ No newline at end of file
+#endif
diff --git a/ext/mysql2/extconf.rb b/ext/mysql2/extconf.rb
index 388520b..b1d5cef 100644
--- a/ext/mysql2/extconf.rb
+++ b/ext/mysql2/extconf.rb
@@ -5,8 +5,9 @@ def asplode lib
abort "-----\n#{lib} is missing. please check your installation of mysql and try again.\n-----"
end
+have_header('ruby/thread.h') && have_func('rb_thread_call_without_gvl', 'ruby/thread.h')
+
# 1.9-only
-have_func('rb_thread_blocking_region')
have_func('rb_wait_for_single_fd')
# borrowed from mysqlplus
diff --git a/ext/mysql2/mysql2_ext.h b/ext/mysql2/mysql2_ext.h
index 8bb08c4..cb7cdb8 100644
--- a/ext/mysql2/mysql2_ext.h
+++ b/ext/mysql2/mysql2_ext.h
@@ -30,6 +30,10 @@ typedef unsigned int uint;
#include <ruby/encoding.h>
#endif
+#ifdef HAVE_RUBY_THREAD_H
+#include <ruby/thread.h>
+#endif
+
#if defined(__GNUC__) && (__GNUC__ >= 3)
#define RB_MYSQL_UNUSED __attribute__ ((unused))
#else
diff --git a/ext/mysql2/result.c b/ext/mysql2/result.c
index 8a2834b..be67b41 100644
--- a/ext/mysql2/result.c
+++ b/ext/mysql2/result.c
@@ -183,7 +183,7 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo
#endif
ptr = wrapper->result;
- row = (MYSQL_ROW)rb_thread_blocking_region(nogvl_fetch_row, ptr, RUBY_UBF_IO, 0);
+ row = (MYSQL_ROW)rb_thread_call_without_gvl(nogvl_fetch_row, ptr, RUBY_UBF_IO, 0);
if (row == NULL) {
return Qnil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment