Skip to content

Commit 926d579

Browse files
committed
Merge pull request #415 from tadd/fix-warning
Fix warning
2 parents 996438e + 958e1f8 commit 926d579

File tree

5 files changed

+84
-55
lines changed

5 files changed

+84
-55
lines changed

ext/mysql2/client.c

Lines changed: 49 additions & 45 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;
@@ -124,6 +124,7 @@ static void rb_mysql_client_mark(void * wrapper) {
124124
static VALUE rb_raise_mysql2_error(mysql_client_wrapper *wrapper) {
125125
VALUE rb_error_msg = rb_str_new2(mysql_error(wrapper->client));
126126
VALUE rb_sql_state = rb_tainted_str_new2(mysql_sqlstate(wrapper->client));
127+
VALUE e;
127128
#ifdef HAVE_RUBY_ENCODING_H
128129
rb_encoding *default_internal_enc = rb_default_internal_encoding();
129130
rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding);
@@ -136,22 +137,22 @@ static VALUE rb_raise_mysql2_error(mysql_client_wrapper *wrapper) {
136137
}
137138
#endif
138139

139-
VALUE e = rb_exc_new3(cMysql2Error, rb_error_msg);
140+
e = rb_exc_new3(cMysql2Error, rb_error_msg);
140141
rb_funcall(e, intern_error_number_eql, 1, UINT2NUM(mysql_errno(wrapper->client)));
141142
rb_funcall(e, intern_sql_state_eql, 1, rb_sql_state);
142143
rb_exc_raise(e);
143144
return Qnil;
144145
}
145146

146-
static VALUE nogvl_init(void *ptr) {
147+
static void *nogvl_init(void *ptr) {
147148
MYSQL *client;
148149

149150
/* may initialize embedded server and read /etc/services off disk */
150151
client = mysql_init((MYSQL *)ptr);
151-
return client ? Qtrue : Qfalse;
152+
return (void*)(client ? Qtrue : Qfalse);
152153
}
153154

154-
static VALUE nogvl_connect(void *ptr) {
155+
static void *nogvl_connect(void *ptr) {
155156
struct nogvl_connect_args *args = ptr;
156157
MYSQL *client;
157158

@@ -160,10 +161,10 @@ static VALUE nogvl_connect(void *ptr) {
160161
args->db, args->port, args->unix_socket,
161162
args->client_flag);
162163

163-
return client ? Qtrue : Qfalse;
164+
return (void *)(client ? Qtrue : Qfalse);
164165
}
165166

166-
static VALUE nogvl_close(void *ptr) {
167+
static void *nogvl_close(void *ptr) {
167168
mysql_client_wrapper *wrapper;
168169
#ifndef _WIN32
169170
int flags;
@@ -190,7 +191,7 @@ static VALUE nogvl_close(void *ptr) {
190191
mysql_close(wrapper->client);
191192
}
192193

193-
return Qnil;
194+
return NULL;
194195
}
195196

196197
static void rb_mysql_client_free(void *ptr) {
@@ -292,11 +293,11 @@ static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE po
292293
args.mysql = wrapper->client;
293294
args.client_flag = NUM2ULONG(flags);
294295

295-
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);
296297
if (rv == Qfalse) {
297298
while (rv == Qfalse && errno == EINTR && !mysql_errno(wrapper->client)) {
298299
errno = 0;
299-
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);
300301
}
301302
if (rv == Qfalse)
302303
return rb_raise_mysql2_error(wrapper);
@@ -316,7 +317,7 @@ static VALUE rb_mysql_client_close(VALUE self) {
316317
GET_CLIENT(self);
317318

318319
if (wrapper->connected) {
319-
rb_thread_blocking_region(nogvl_close, wrapper, RUBY_UBF_IO, 0);
320+
rb_thread_call_without_gvl(nogvl_close, wrapper, RUBY_UBF_IO, 0);
320321
}
321322

322323
return Qnil;
@@ -327,19 +328,19 @@ static VALUE rb_mysql_client_close(VALUE self) {
327328
* enough to fit in a socket buffer, but sometimes large UPDATE and
328329
* INSERTs will cause the process to block
329330
*/
330-
static VALUE nogvl_send_query(void *ptr) {
331+
static void *nogvl_send_query(void *ptr) {
331332
struct nogvl_send_query_args *args = ptr;
332333
int rv;
333334

334335
rv = mysql_send_query(args->mysql, args->sql_ptr, args->sql_len);
335336

336-
return rv == 0 ? Qtrue : Qfalse;
337+
return (void*)(rv == 0 ? Qtrue : Qfalse);
337338
}
338339

339340
static VALUE do_send_query(void *args) {
340341
struct nogvl_send_query_args *query_args = args;
341342
mysql_client_wrapper *wrapper = query_args->wrapper;
342-
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) {
343344
/* an error occurred, we're not active anymore */
344345
MARK_CONN_INACTIVE(self);
345346
return rb_raise_mysql2_error(wrapper);
@@ -352,14 +353,14 @@ static VALUE do_send_query(void *args) {
352353
* response can overflow the socket buffers and cause us to eventually
353354
* block while calling mysql_read_query_result
354355
*/
355-
static VALUE nogvl_read_query_result(void *ptr) {
356+
static void *nogvl_read_query_result(void *ptr) {
356357
MYSQL * client = ptr;
357358
my_bool res = mysql_read_query_result(client);
358359

359-
return res == 0 ? Qtrue : Qfalse;
360+
return (void *)(res == 0 ? Qtrue : Qfalse);
360361
}
361362

362-
static VALUE nogvl_do_result(void *ptr, char use_result) {
363+
static void *nogvl_do_result(void *ptr, char use_result) {
363364
mysql_client_wrapper *wrapper;
364365
MYSQL_RES *result;
365366

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

377-
return (VALUE)result;
378+
return result;
378379
}
379380

380381
/* mysql_store_result may (unlikely) read rows off the socket */
381-
static VALUE nogvl_store_result(void *ptr) {
382+
static void *nogvl_store_result(void *ptr) {
382383
return nogvl_do_result(ptr, 0);
383384
}
384385

385-
static VALUE nogvl_use_result(void *ptr) {
386+
static void *nogvl_use_result(void *ptr) {
386387
return nogvl_do_result(ptr, 1);
387388
}
388389

@@ -402,17 +403,17 @@ static VALUE rb_mysql_client_async_result(VALUE self) {
402403
return Qnil;
403404

404405
REQUIRE_CONNECTED(wrapper);
405-
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) {
406407
/* an error occurred, mark this connection inactive */
407408
MARK_CONN_INACTIVE(self);
408409
return rb_raise_mysql2_error(wrapper);
409410
}
410411

411412
is_streaming = rb_hash_aref(rb_iv_get(self, "@current_query_options"), sym_stream);
412413
if(is_streaming == Qtrue) {
413-
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);
414415
} else {
415-
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);
416417
}
417418

418419
if (result == NULL) {
@@ -511,7 +512,7 @@ static VALUE finish_and_mark_inactive(void *args) {
511512
/* if we got here, the result hasn't been read off the wire yet
512513
so lets do that and then throw it away because we have no way
513514
of getting it back up to the caller from here */
514-
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);
515516
mysql_free_result(result);
516517

517518
wrapper->active_thread = Qnil;
@@ -530,18 +531,18 @@ static VALUE finish_and_mark_inactive(void *args) {
530531
* again.
531532
*/
532533
static VALUE rb_mysql_client_abandon_results(VALUE self) {
533-
GET_CLIENT(self);
534-
535534
MYSQL_RES *result;
536535
int ret;
537536

537+
GET_CLIENT(self);
538+
538539
while (mysql_more_results(wrapper->client) == 1) {
539540
ret = mysql_next_result(wrapper->client);
540541
if (ret > 0) {
541542
rb_raise_mysql2_error(wrapper);
542543
}
543544

544-
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);
545546

546547
if (result != NULL) {
547548
mysql_free_result(result);
@@ -749,8 +750,8 @@ static VALUE rb_mysql_client_info(VALUE self) {
749750
#ifdef HAVE_RUBY_ENCODING_H
750751
rb_encoding *default_internal_enc;
751752
rb_encoding *conn_enc;
752-
#endif
753753
GET_CLIENT(self);
754+
#endif
754755
version = rb_hash_new();
755756

756757
#ifdef HAVE_RUBY_ENCODING_H
@@ -810,9 +811,12 @@ static VALUE rb_mysql_client_server_info(VALUE self) {
810811
static VALUE rb_mysql_client_socket(VALUE self) {
811812
GET_CLIENT(self);
812813
#ifndef _WIN32
813-
REQUIRE_CONNECTED(wrapper);
814-
int fd_set_fd = wrapper->client->net.fd;
815-
return INT2NUM(fd_set_fd);
814+
{
815+
int fd_set_fd;
816+
REQUIRE_CONNECTED(wrapper);
817+
fd_set_fd = wrapper->client->net.fd;
818+
return INT2NUM(fd_set_fd);
819+
}
816820
#else
817821
rb_raise(cMysql2Error, "Raw access to the mysql file descriptor isn't supported on Windows");
818822
#endif
@@ -862,13 +866,13 @@ static VALUE rb_mysql_client_thread_id(VALUE self) {
862866
return ULL2NUM(retVal);
863867
}
864868

865-
static VALUE nogvl_select_db(void *ptr) {
869+
static void *nogvl_select_db(void *ptr) {
866870
struct nogvl_select_db_args *args = ptr;
867871

868872
if (mysql_select_db(args->mysql, args->db) == 0)
869-
return Qtrue;
873+
return (void *)Qtrue;
870874
else
871-
return Qfalse;
875+
return (void *)Qfalse;
872876
}
873877

874878
/* call-seq:
@@ -887,16 +891,16 @@ static VALUE rb_mysql_client_select_db(VALUE self, VALUE db)
887891
args.mysql = wrapper->client;
888892
args.db = StringValuePtr(db);
889893

890-
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)
891895
rb_raise_mysql2_error(wrapper);
892896

893897
return db;
894898
}
895899

896-
static VALUE nogvl_ping(void *ptr) {
900+
static void *nogvl_ping(void *ptr) {
897901
MYSQL *client = ptr;
898902

899-
return mysql_ping(client) == 0 ? Qtrue : Qfalse;
903+
return (void *)(mysql_ping(client) == 0 ? Qtrue : Qfalse);
900904
}
901905

902906
/* call-seq:
@@ -913,7 +917,7 @@ static VALUE rb_mysql_client_ping(VALUE self) {
913917
if (!wrapper->connected) {
914918
return Qfalse;
915919
} else {
916-
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);
917921
}
918922
}
919923

@@ -939,8 +943,8 @@ static VALUE rb_mysql_client_more_results(VALUE self)
939943
*/
940944
static VALUE rb_mysql_client_next_result(VALUE self)
941945
{
942-
GET_CLIENT(self);
943946
int ret;
947+
GET_CLIENT(self);
944948
ret = mysql_next_result(wrapper->client);
945949
if (ret > 0) {
946950
rb_raise_mysql2_error(wrapper);
@@ -965,7 +969,7 @@ static VALUE rb_mysql_client_store_result(VALUE self)
965969
VALUE current;
966970
GET_CLIENT(self);
967971

968-
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);
969973

970974
if (result == NULL) {
971975
if (mysql_errno(wrapper->client) != 0) {
@@ -1046,18 +1050,18 @@ static VALUE set_write_timeout(VALUE self, VALUE value) {
10461050

10471051
static VALUE set_charset_name(VALUE self, VALUE value) {
10481052
char *charset_name;
1053+
#ifdef HAVE_RUBY_ENCODING_H
10491054
size_t charset_name_len;
10501055
const struct mysql2_mysql_enc_name_to_rb_map *mysql2rb;
1051-
#ifdef HAVE_RUBY_ENCODING_H
10521056
rb_encoding *enc;
10531057
VALUE rb_enc;
10541058
#endif
10551059
GET_CLIENT(self);
10561060

10571061
charset_name = RSTRING_PTR(value);
1058-
charset_name_len = RSTRING_LEN(value);
10591062

10601063
#ifdef HAVE_RUBY_ENCODING_H
1064+
charset_name_len = RSTRING_LEN(value);
10611065
mysql2rb = mysql2_mysql_enc_name_to_rb(charset_name, charset_name_len);
10621066
if (mysql2rb == NULL || mysql2rb->rb_name == NULL) {
10631067
VALUE inspect = rb_inspect(value);
@@ -1097,7 +1101,7 @@ static VALUE set_secure_auth(VALUE self, VALUE value) {
10971101
static VALUE initialize_ext(VALUE self) {
10981102
GET_CLIENT(self);
10991103

1100-
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) {
11011105
/* TODO: warning - not enough memory? */
11021106
return rb_raise_mysql2_error(wrapper);
11031107
}

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 *), 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')

0 commit comments

Comments
 (0)