When in Ruby 1.9, release the global interpreter lock while performing a query, so that other threads can still run.

This commit is contained in:
Hongli Lai (Phusion) 2008-11-18 19:18:51 +01:00
parent 1af96063be
commit 8a758f77e3

View File

@ -719,18 +719,43 @@ static VALUE use_result(VALUE obj)
} }
static VALUE res_free(VALUE); static VALUE res_free(VALUE);
typedef struct {
MYSQL *m;
const char *data;
unsigned long len;
} QueryArgs;
static VALUE blocking_query(void *data)
{
QueryArgs *args = (QueryArgs *) data;
return (VALUE) mysql_real_query(args->m, args->data, args->len);
}
/* query(sql) */ /* query(sql) */
static VALUE query(VALUE obj, VALUE sql) static VALUE query(VALUE obj, VALUE sql)
{ {
int loop = 0; int loop = 0;
MYSQL* m = GetHandler(obj); MYSQL* m = GetHandler(obj);
QueryArgs args;
int result;
Check_Type(sql, T_STRING); Check_Type(sql, T_STRING);
if (GetMysqlStruct(obj)->connection == Qfalse) { if (GetMysqlStruct(obj)->connection == Qfalse) {
rb_raise(eMysql, "query: not connected"); rb_raise(eMysql, "query: not connected");
} }
if (rb_block_given_p()) { if (rb_block_given_p()) {
if (mysql_real_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0) #ifdef RUBY_VM
args.m = m;
args.data = RSTRING_PTR(sql);
args.len = RSTRING_LEN(sql);
result = (int) rb_thread_blocking_region(blocking_query, &args, RUBY_UBF_PROCESS, 0);
#else
result = mysql_real_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql));
#endif
if (result != 0)
mysql_raise(m); mysql_raise(m);
do { do {
MYSQL_RES* res = mysql_store_result(m); MYSQL_RES* res = mysql_store_result(m);
if (res == NULL) { if (res == NULL) {
@ -749,7 +774,16 @@ static VALUE query(VALUE obj, VALUE sql)
#endif #endif
return obj; return obj;
} }
if (mysql_real_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0)
#ifdef RUBY_VM
args.m = m;
args.data = RSTRING_PTR(sql);
args.len = RSTRING_LEN(sql);
result = (int) rb_thread_blocking_region(blocking_query, &args, RUBY_UBF_PROCESS, 0);
#else
result = mysql_real_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql));
#endif
if (result != 0)
mysql_raise(m); mysql_raise(m);
if (GetMysqlStruct(obj)->query_with_result == Qfalse) if (GetMysqlStruct(obj)->query_with_result == Qfalse)
return obj; return obj;