threaded access suppor (working this time)

This commit is contained in:
oldmoe 2008-08-27 15:09:39 +03:00
parent fa9c977d54
commit da40f34362
5 changed files with 18 additions and 40 deletions

View File

@ -5,6 +5,7 @@
*/ */
#include <ruby.h> #include <ruby.h>
#include <errno.h>
#ifndef RSTRING_PTR #ifndef RSTRING_PTR
#define RSTRING_PTR(str) RSTRING(str)->ptr #define RSTRING_PTR(str) RSTRING(str)->ptr
#endif #endif
@ -755,24 +756,6 @@ static VALUE socket(VALUE obj)
return INT2NUM(vio_fd(m->net.vio)); 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 */ /* send_query */
static VALUE send_query(VALUE obj, VALUE sql) static VALUE send_query(VALUE obj, VALUE sql)
{ {
@ -803,12 +786,15 @@ static VALUE get_result(VALUE obj)
} }
/* async_query */ /* async_query */
/*
comment it out until I figure out how it works
static VALUE async_query(VALUE obj, VALUE sql) static VALUE async_query(VALUE obj, VALUE sql)
{ {
send_query(obj,sql); send_query(obj,sql);
rb_io_wait_readable(socket(obj)); rb_io_wait_readable(socket(obj));
return get_result(obj); return get_result(obj);
} }
*/
#if MYSQL_VERSION_ID >= 40100 #if MYSQL_VERSION_ID >= 40100
@ -2014,7 +2000,7 @@ void Init_mysql(void)
#endif #endif
rb_define_method(cMysql, "query", query, 1); rb_define_method(cMysql, "query", query, 1);
rb_define_method(cMysql, "real_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, "send_query", send_query, 1);
rb_define_method(cMysql, "get_result", get_result, 0); rb_define_method(cMysql, "get_result", get_result, 0);
rb_define_method(cMysql, "socket", socket, 0); rb_define_method(cMysql, "socket", socket, 0);

9
lib/mysqlplus.rb Normal file
View File

@ -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

View File

@ -12,6 +12,7 @@ Gem::Specification.new do |s|
s.files = [ s.files = [
"mysqlplus.gemspec", "mysqlplus.gemspec",
"README", "README",
"lib/mysqlplus.rb",
"test/test_threaded.rb", "test/test_threaded.rb",
"test/test_evented.rb", "test/test_evented.rb",
"ext/extconf.rb", "ext/extconf.rb",

View File

@ -1,22 +1,4 @@
require 'mysql' require 'mysqlplus'
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
@count = 10 @count = 10
@connections = {} @connections = {}

View File

@ -1,4 +1,4 @@
require 'mysql' require 'mysqlplus'
$t = Time.now $t = Time.now
$connections = [] $connections = []
@ -11,7 +11,7 @@ $done = 0
$t = Time.now $t = Time.now
$count.times do |i| $count.times do |i|
Thread.new do 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 $done = $done + 1
puts Time.now - $t if $done == $count puts Time.now - $t if $done == $count
end end