From da40f3436209ea99dce580081658f1dee62212de Mon Sep 17 00:00:00 2001 From: oldmoe Date: Wed, 27 Aug 2008 15:09:39 +0300 Subject: [PATCH] threaded access suppor (working this time) --- ext/mysql.c | 24 +++++------------------- lib/mysqlplus.rb | 9 +++++++++ mysqlplus.gemspec | 1 + test/test_evented.rb | 20 +------------------- test/test_threaded.rb | 4 ++-- 5 files changed, 18 insertions(+), 40 deletions(-) create mode 100644 lib/mysqlplus.rb diff --git a/ext/mysql.c b/ext/mysql.c index 799e3e4..765cfde 100644 --- a/ext/mysql.c +++ b/ext/mysql.c @@ -5,6 +5,7 @@ */ #include +#include #ifndef RSTRING_PTR #define RSTRING_PTR(str) RSTRING(str)->ptr #endif @@ -755,24 +756,6 @@ static VALUE socket(VALUE obj) return INT2NUM(vio_fd(m->net.vio)); } -/* setnonblocking */ -/* -static VALUE setnonblocking(VALUE obj, VALUE new_state) -{ - MYSQL* m = GetHandler(obj); - my_bool current; - return m->net.vio->vioblocking(m->net.vio, new_state, *current); -} -*/ -/* isblocking */ -/* -static VALUE isblocking(VALUE obj) -{ - MYSQL* m = GetHandler(obj); - return m->net.vio->is_blocking(m->net.vio); -} -*/ - /* send_query */ static VALUE send_query(VALUE obj, VALUE sql) { @@ -803,12 +786,15 @@ static VALUE get_result(VALUE obj) } /* async_query */ +/* +comment it out until I figure out how it works static VALUE async_query(VALUE obj, VALUE sql) { send_query(obj,sql); rb_io_wait_readable(socket(obj)); return get_result(obj); } +*/ #if MYSQL_VERSION_ID >= 40100 @@ -2014,7 +2000,7 @@ void Init_mysql(void) #endif rb_define_method(cMysql, "query", query, 1); rb_define_method(cMysql, "real_query", query, 1); - rb_define_method(cMysql, "async_query", async_query, 1); + /*rb_define_method(cMysql, "async_query", async_query, 1);*/ rb_define_method(cMysql, "send_query", send_query, 1); rb_define_method(cMysql, "get_result", get_result, 0); rb_define_method(cMysql, "socket", socket, 0); diff --git a/lib/mysqlplus.rb b/lib/mysqlplus.rb new file mode 100644 index 0000000..88b0e06 --- /dev/null +++ b/lib/mysqlplus.rb @@ -0,0 +1,9 @@ +require 'mysql' + +class Mysql + def async_query(sql) + send_query(sql) + select([IO.new(socket)],nil,nil,nil) + get_result + end +end diff --git a/mysqlplus.gemspec b/mysqlplus.gemspec index b42d9ef..fc69d5c 100755 --- a/mysqlplus.gemspec +++ b/mysqlplus.gemspec @@ -12,6 +12,7 @@ Gem::Specification.new do |s| s.files = [ "mysqlplus.gemspec", "README", + "lib/mysqlplus.rb", "test/test_threaded.rb", "test/test_evented.rb", "ext/extconf.rb", diff --git a/test/test_evented.rb b/test/test_evented.rb index 767e872..3b4feea 100644 --- a/test/test_evented.rb +++ b/test/test_evented.rb @@ -1,22 +1,4 @@ -require 'mysql' - -class Mysql - attr_accessor :fiber - alias :old_query :query - def query(sql) - if Fiber.current[:neverblock] - send_query(sql) - @fiber = Fiber.current - Fiber.yield - else - old_query(sql) - end - end - - def process_command - @fiber.resume get_result - end -end +require 'mysqlplus' @count = 10 @connections = {} diff --git a/test/test_threaded.rb b/test/test_threaded.rb index b5d2c4e..7035eb6 100644 --- a/test/test_threaded.rb +++ b/test/test_threaded.rb @@ -1,4 +1,4 @@ -require 'mysql' +require 'mysqlplus' $t = Time.now $connections = [] @@ -11,7 +11,7 @@ $done = 0 $t = Time.now $count.times do |i| Thread.new do - $connections[i].async_query('select sleep(0.1)').each{|r|puts "#{i}:#{r}"} + $connections[i].async_query('select sleep(1)').each{|r|puts "#{i}:#{r}"} $done = $done + 1 puts Time.now - $t if $done == $count end