Skip to content

Commit 21c30c0

Browse files
committed
fix deprecation warning: migrate rb_thread_blocking_region to rb_thread_call_without_gvl
1 parent 78b43e4 commit 21c30c0

File tree

5 files changed

+58
-45
lines changed

5 files changed

+58
-45
lines changed

ext/mysql2/client.c

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static VALUE rb_hash_dup(VALUE other) {
5757

5858
/*
5959
* used to pass all arguments to mysql_real_connect while inside
60-
* rb_thread_blocking_region
60+
* rb_thread_call_without_gvl
6161
*/
6262
struct nogvl_connect_args {
6363
MYSQL *mysql;
@@ -72,7 +72,7 @@ struct nogvl_connect_args {
7272

7373
/*
7474
* used to pass all arguments to mysql_send_query while inside
75-
* rb_thread_blocking_region
75+
* rb_thread_call_without_gvl
7676
*/
7777
struct nogvl_send_query_args {
7878
MYSQL *mysql;
@@ -84,7 +84,7 @@ struct nogvl_send_query_args {
8484

8585
/*
8686
* used to pass all arguments to mysql_select_db while inside
87-
* rb_thread_blocking_region
87+
* rb_thread_call_without_gvl
8888
*/
8989
struct nogvl_select_db_args {
9090
MYSQL *mysql;
@@ -144,15 +144,15 @@ static VALUE rb_raise_mysql2_error(mysql_client_wrapper *wrapper) {
144144
return Qnil;
145145
}
146146

147-
static VALUE nogvl_init(void *ptr) {
147+
static void *nogvl_init(void *ptr) {
148148
MYSQL *client;
149149

150150
/* may initialize embedded server and read /etc/services off disk */
151151
client = mysql_init((MYSQL *)ptr);
152-
return client ? Qtrue : Qfalse;
152+
return (void*)(client ? Qtrue : Qfalse);
153153
}
154154

155-
static VALUE nogvl_connect(void *ptr) {
155+
static void *nogvl_connect(void *ptr) {
156156
struct nogvl_connect_args *args = ptr;
157157
MYSQL *client;
158158

@@ -161,10 +161,10 @@ static VALUE nogvl_connect(void *ptr) {
161161
args->db, args->port, args->unix_socket,
162162
args->client_flag);
163163

164-
return client ? Qtrue : Qfalse;
164+
return (void *)(client ? Qtrue : Qfalse);
165165
}
166166

167-
static VALUE nogvl_close(void *ptr) {
167+
static void *nogvl_close(void *ptr) {
168168
mysql_client_wrapper *wrapper;
169169
#ifndef _WIN32
170170
int flags;
@@ -191,7 +191,7 @@ static VALUE nogvl_close(void *ptr) {
191191
mysql_close(wrapper->client);
192192
}
193193

194-
return Qnil;
194+
return NULL;
195195
}
196196

197197
static void rb_mysql_client_free(void *ptr) {
@@ -293,11 +293,11 @@ static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE po
293293
args.mysql = wrapper->client;
294294
args.client_flag = NUM2ULONG(flags);
295295

296-
rv = rb_thread_blocking_region(nogvl_connect, &args, RUBY_UBF_IO, 0);
296+
rv = (VALUE) rb_thread_call_without_gvl(nogvl_connect, &args, RUBY_UBF_IO, 0);
297297
if (rv == Qfalse) {
298298
while (rv == Qfalse && errno == EINTR && !mysql_errno(wrapper->client)) {
299299
errno = 0;
300-
rv = rb_thread_blocking_region(nogvl_connect, &args, RUBY_UBF_IO, 0);
300+
rv = (VALUE) rb_thread_call_without_gvl(nogvl_connect, &args, RUBY_UBF_IO, 0);
301301
}
302302
if (rv == Qfalse)
303303
return rb_raise_mysql2_error(wrapper);
@@ -317,7 +317,7 @@ static VALUE rb_mysql_client_close(VALUE self) {
317317
GET_CLIENT(self);
318318

319319
if (wrapper->connected) {
320-
rb_thread_blocking_region(nogvl_close, wrapper, RUBY_UBF_IO, 0);
320+
rb_thread_call_without_gvl(nogvl_close, wrapper, RUBY_UBF_IO, 0);
321321
}
322322

323323
return Qnil;
@@ -328,19 +328,19 @@ static VALUE rb_mysql_client_close(VALUE self) {
328328
* enough to fit in a socket buffer, but sometimes large UPDATE and
329329
* INSERTs will cause the process to block
330330
*/
331-
static VALUE nogvl_send_query(void *ptr) {
331+
static void *nogvl_send_query(void *ptr) {
332332
struct nogvl_send_query_args *args = ptr;
333333
int rv;
334334

335335
rv = mysql_send_query(args->mysql, args->sql_ptr, args->sql_len);
336336

337-
return rv == 0 ? Qtrue : Qfalse;
337+
return (void*)(rv == 0 ? Qtrue : Qfalse);
338338
}
339339

340340
static VALUE do_send_query(void *args) {
341341
struct nogvl_send_query_args *query_args = args;
342342
mysql_client_wrapper *wrapper = query_args->wrapper;
343-
if (rb_thread_blocking_region(nogvl_send_query, args, RUBY_UBF_IO, 0) == Qfalse) {
343+
if ((VALUE)rb_thread_call_without_gvl(nogvl_send_query, args, RUBY_UBF_IO, 0) == Qfalse) {
344344
/* an error occurred, we're not active anymore */
345345
MARK_CONN_INACTIVE(self);
346346
return rb_raise_mysql2_error(wrapper);
@@ -353,14 +353,14 @@ static VALUE do_send_query(void *args) {
353353
* response can overflow the socket buffers and cause us to eventually
354354
* block while calling mysql_read_query_result
355355
*/
356-
static VALUE nogvl_read_query_result(void *ptr) {
356+
static void *nogvl_read_query_result(void *ptr) {
357357
MYSQL * client = ptr;
358358
my_bool res = mysql_read_query_result(client);
359359

360-
return res == 0 ? Qtrue : Qfalse;
360+
return (void *)(res == 0 ? Qtrue : Qfalse);
361361
}
362362

363-
static VALUE nogvl_do_result(void *ptr, char use_result) {
363+
static void *nogvl_do_result(void *ptr, char use_result) {
364364
mysql_client_wrapper *wrapper;
365365
MYSQL_RES *result;
366366

@@ -375,15 +375,15 @@ static VALUE nogvl_do_result(void *ptr, char use_result) {
375375
ready for another command to be issued */
376376
wrapper->active_thread = Qnil;
377377

378-
return (VALUE)result;
378+
return result;
379379
}
380380

381381
/* mysql_store_result may (unlikely) read rows off the socket */
382-
static VALUE nogvl_store_result(void *ptr) {
382+
static void *nogvl_store_result(void *ptr) {
383383
return nogvl_do_result(ptr, 0);
384384
}
385385

386-
static VALUE nogvl_use_result(void *ptr) {
386+
static void *nogvl_use_result(void *ptr) {
387387
return nogvl_do_result(ptr, 1);
388388
}
389389

@@ -403,17 +403,17 @@ static VALUE rb_mysql_client_async_result(VALUE self) {
403403
return Qnil;
404404

405405
REQUIRE_CONNECTED(wrapper);
406-
if (rb_thread_blocking_region(nogvl_read_query_result, wrapper->client, RUBY_UBF_IO, 0) == Qfalse) {
406+
if ((VALUE)rb_thread_call_without_gvl(nogvl_read_query_result, wrapper->client, RUBY_UBF_IO, 0) == Qfalse) {
407407
/* an error occurred, mark this connection inactive */
408408
MARK_CONN_INACTIVE(self);
409409
return rb_raise_mysql2_error(wrapper);
410410
}
411411

412412
is_streaming = rb_hash_aref(rb_iv_get(self, "@current_query_options"), sym_stream);
413413
if(is_streaming == Qtrue) {
414-
result = (MYSQL_RES *)rb_thread_blocking_region(nogvl_use_result, wrapper, RUBY_UBF_IO, 0);
414+
result = (MYSQL_RES *)rb_thread_call_without_gvl(nogvl_use_result, wrapper, RUBY_UBF_IO, 0);
415415
} else {
416-
result = (MYSQL_RES *)rb_thread_blocking_region(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
416+
result = (MYSQL_RES *)rb_thread_call_without_gvl(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
417417
}
418418

419419
if (result == NULL) {
@@ -512,7 +512,7 @@ static VALUE finish_and_mark_inactive(void *args) {
512512
/* if we got here, the result hasn't been read off the wire yet
513513
so lets do that and then throw it away because we have no way
514514
of getting it back up to the caller from here */
515-
result = (MYSQL_RES *)rb_thread_blocking_region(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
515+
result = (MYSQL_RES *)rb_thread_call_without_gvl(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
516516
mysql_free_result(result);
517517

518518
wrapper->active_thread = Qnil;
@@ -542,7 +542,7 @@ static VALUE rb_mysql_client_abandon_results(VALUE self) {
542542
rb_raise_mysql2_error(wrapper);
543543
}
544544

545-
result = (MYSQL_RES *)rb_thread_blocking_region(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
545+
result = (MYSQL_RES *)rb_thread_call_without_gvl(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
546546

547547
if (result != NULL) {
548548
mysql_free_result(result);
@@ -866,13 +866,13 @@ static VALUE rb_mysql_client_thread_id(VALUE self) {
866866
return ULL2NUM(retVal);
867867
}
868868

869-
static VALUE nogvl_select_db(void *ptr) {
869+
static void *nogvl_select_db(void *ptr) {
870870
struct nogvl_select_db_args *args = ptr;
871871

872872
if (mysql_select_db(args->mysql, args->db) == 0)
873-
return Qtrue;
873+
return (void *)Qtrue;
874874
else
875-
return Qfalse;
875+
return (void *)Qfalse;
876876
}
877877

878878
/* call-seq:
@@ -891,16 +891,16 @@ static VALUE rb_mysql_client_select_db(VALUE self, VALUE db)
891891
args.mysql = wrapper->client;
892892
args.db = StringValuePtr(db);
893893

894-
if (rb_thread_blocking_region(nogvl_select_db, &args, RUBY_UBF_IO, 0) == Qfalse)
894+
if (rb_thread_call_without_gvl(nogvl_select_db, &args, RUBY_UBF_IO, 0) == Qfalse)
895895
rb_raise_mysql2_error(wrapper);
896896

897897
return db;
898898
}
899899

900-
static VALUE nogvl_ping(void *ptr) {
900+
static void *nogvl_ping(void *ptr) {
901901
MYSQL *client = ptr;
902902

903-
return mysql_ping(client) == 0 ? Qtrue : Qfalse;
903+
return (void *)(mysql_ping(client) == 0 ? Qtrue : Qfalse);
904904
}
905905

906906
/* call-seq:
@@ -917,7 +917,7 @@ static VALUE rb_mysql_client_ping(VALUE self) {
917917
if (!wrapper->connected) {
918918
return Qfalse;
919919
} else {
920-
return rb_thread_blocking_region(nogvl_ping, wrapper->client, RUBY_UBF_IO, 0);
920+
return (VALUE)rb_thread_call_without_gvl(nogvl_ping, wrapper->client, RUBY_UBF_IO, 0);
921921
}
922922
}
923923

@@ -969,7 +969,7 @@ static VALUE rb_mysql_client_store_result(VALUE self)
969969
VALUE current;
970970
GET_CLIENT(self);
971971

972-
result = (MYSQL_RES *)rb_thread_blocking_region(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
972+
result = (MYSQL_RES *)rb_thread_call_without_gvl(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
973973

974974
if (result == NULL) {
975975
if (mysql_errno(wrapper->client) != 0) {
@@ -1101,7 +1101,7 @@ static VALUE set_secure_auth(VALUE self, VALUE value) {
11011101
static VALUE initialize_ext(VALUE self) {
11021102
GET_CLIENT(self);
11031103

1104-
if (rb_thread_blocking_region(nogvl_init, wrapper->client, RUBY_UBF_IO, 0) == Qfalse) {
1104+
if ((VALUE)rb_thread_call_without_gvl(nogvl_init, wrapper->client, RUBY_UBF_IO, 0) == Qfalse) {
11051105
/* TODO: warning - not enough memory? */
11061106
return rb_raise_mysql2_error(wrapper);
11071107
}

ext/mysql2/client.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
#ifndef MYSQL2_CLIENT_H
22
#define MYSQL2_CLIENT_H
33

4+
#ifndef HAVE_RB_THREAD_CALL_WITHOUT_GVL
5+
#ifdef HAVE_RB_THREAD_BLOCKING_REGION
6+
7+
/* emulate rb_thread_call_without_gvl with rb_thread_blocking_region */
8+
#define rb_thread_call_without_gvl(func, data1, ubf, data2) \
9+
rb_thread_blocking_region((rb_blocking_function_t *)func, data1, ubf, data2)
10+
11+
#else /* ! HAVE_RB_THREAD_BLOCKING_REGION */
412
/*
5-
* partial emulation of the 1.9 rb_thread_blocking_region under 1.8,
13+
* partial emulation of the 2.0 rb_thread_call_without_gvl under 1.8,
614
* this is enough for dealing with blocking I/O functions in the
715
* presence of threads.
816
*/
9-
#ifndef HAVE_RB_THREAD_BLOCKING_REGION
1017

1118
#include <rubysig.h>
1219
#define RUBY_UBF_IO ((rb_unblock_function_t *)-1)
1320
typedef void rb_unblock_function_t(void *);
14-
typedef VALUE rb_blocking_function_t(void *);
15-
static VALUE
16-
rb_thread_blocking_region(
17-
rb_blocking_function_t *func, void *data1,
21+
static void *
22+
rb_thread_call_without_gvl(
23+
void *(*func), void *data1,
1824
RB_MYSQL_UNUSED rb_unblock_function_t *ubf,
1925
RB_MYSQL_UNUSED void *data2)
2026
{
21-
VALUE rv;
27+
void *rv;
2228

2329
TRAP_BEG;
2430
rv = func(data1);
@@ -28,6 +34,7 @@ rb_thread_blocking_region(
2834
}
2935

3036
#endif /* ! HAVE_RB_THREAD_BLOCKING_REGION */
37+
#endif /* ! HAVE_RB_THREAD_CALL_WITHOUT_GVL */
3138

3239
void init_mysql2_client();
3340

ext/mysql2/extconf.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ def asplode lib
55
abort "-----\n#{lib} is missing. please check your installation of mysql and try again.\n-----"
66
end
77

8+
# 2.0-only
9+
have_header('ruby/thread.h') && have_func('rb_thread_call_without_gvl', 'ruby/thread.h')
10+
811
# 1.9-only
912
have_func('rb_thread_blocking_region')
1013
have_func('rb_wait_for_single_fd')

ext/mysql2/mysql2_ext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ typedef unsigned int uint;
2929
#ifdef HAVE_RUBY_ENCODING_H
3030
#include <ruby/encoding.h>
3131
#endif
32+
#ifdef HAVE_RUBY_THREAD_H
33+
#include <ruby/thread.h>
34+
#endif
3235

3336
#if defined(__GNUC__) && (__GNUC__ >= 3)
3437
#define RB_MYSQL_UNUSED __attribute__ ((unused))

ext/mysql2/result.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ static void rb_mysql_result_free(void *ptr) {
9999
* reliable way for us to tell this so we'll always release the GVL
100100
* to be safe
101101
*/
102-
static VALUE nogvl_fetch_row(void *ptr) {
102+
static void *nogvl_fetch_row(void *ptr) {
103103
MYSQL_RES *result = ptr;
104104

105-
return (VALUE)mysql_fetch_row(result);
105+
return mysql_fetch_row(result);
106106
}
107107

108108
static VALUE rb_mysql_result_fetch_field(VALUE self, unsigned int idx, short int symbolize_keys) {
@@ -203,7 +203,7 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo
203203
#endif
204204

205205
ptr = wrapper->result;
206-
row = (MYSQL_ROW)rb_thread_blocking_region(nogvl_fetch_row, ptr, RUBY_UBF_IO, 0);
206+
row = (MYSQL_ROW)rb_thread_call_without_gvl(nogvl_fetch_row, ptr, RUBY_UBF_IO, 0);
207207
if (row == NULL) {
208208
return Qnil;
209209
}

0 commit comments

Comments
 (0)