<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -63,6 +63,7 @@ struct mysql {
     char gc_disabled;
     char blocking;
     int async_in_progress;
+    char busy;
 };
 
 struct mysql_res {
@@ -821,7 +822,33 @@ static VALUE socket(VALUE obj)
 
 /* blocking */
 static VALUE blocking(VALUE obj){
-  return ( GetMysqlStruct(obj)-&gt;blocking ? Qtrue : Qfalse );
+    return ( GetMysqlStruct(obj)-&gt;blocking ? Qtrue : Qfalse );
+}
+
+/* is_busy */
+static VALUE is_busy(VALUE obj){
+    return ( GetMysqlStruct(obj)-&gt;busy ? Qtrue : Qfalse );
+}
+
+static VALUE is_idle(VALUE obj){
+    return ( is_busy(obj) == Qtrue ) ? Qfalse : Qtrue;
+}
+
+/* busy(true|false) */
+static VALUE busy_set(VALUE obj, VALUE flag)
+{
+    if (TYPE(flag) != T_TRUE &amp;&amp; TYPE(flag) != T_FALSE)
+        rb_raise(rb_eTypeError, &quot;invalid type, required true or false.&quot;);
+    GetMysqlStruct(obj)-&gt;busy = flag;
+    return flag;
+}
+
+static void busy( VALUE obj ){
+    busy_set( obj, Qtrue );
+}
+
+static void idle( VALUE obj ){
+    busy_set( obj, Qfalse );
 }
 
 /* readable(timeout=nil) */
@@ -924,12 +951,14 @@ static VALUE send_query(VALUE obj, VALUE sql)
     Check_Type(sql, T_STRING);
 
     if (GetMysqlStruct(obj)-&gt;connection == Qfalse &amp;&amp; async_in_progress(obj) == Qtrue ) {
+      idle( obj );
       rb_raise(eMysql, &quot;query: not connected&quot;);
     }
 
     validate_async_query(obj);
 
     if (mysql_send_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0){
+      idle( obj );
       mysql_raise(m);
 	}
 	
@@ -960,10 +989,13 @@ static VALUE get_result(VALUE obj)
     async_in_progress_set( obj, Qfalse );
 
     if (GetMysqlStruct(obj)-&gt;connection == Qfalse) {
-        rb_raise(eMysql, &quot;query: not connected&quot;);
+       idle( obj );    
+       rb_raise(eMysql, &quot;query: not connected&quot;);
     }
-	if (mysql_read_query_result(m) != 0)
-	    mysql_raise(m);
+	if (mysql_read_query_result(m) != 0){
+	   idle( obj );    
+ 	   mysql_raise(m);
+	}
     
     if (GetMysqlStruct(obj)-&gt;query_with_result == Qfalse)
       	return obj;
@@ -988,6 +1020,7 @@ static void schedule_query(VALUE obj, VALUE timeout)
  
       ret = rb_thread_select(m-&gt;net.fd + 1, &amp;read, NULL, NULL, &amp;tv);
       if (ret &lt; 0) {
+        idle( obj );    
         rb_raise(eMysql, &quot;query: timeout&quot;);
       }
 	  
@@ -1011,14 +1044,18 @@ static VALUE async_query(int argc, VALUE* argv, VALUE obj)
 
     async_in_progress_set( obj, Qfalse );
 
+    busy(obj); 
+
     send_query(obj,sql);
 
     schedule_query(obj, timeout);
     
     if (rb_block_given_p()) {
       rb_yield( get_result(obj) );
+      idle( obj );
       return obj; 
     }else{
+      idle( obj );
       return get_result(obj); 
     }  
 }
@@ -2323,6 +2360,9 @@ void Init_mysql(void)
     rb_define_method(cMysql, &quot;blocking?&quot;, blocking, 0);
     rb_define_method(cMysql, &quot;gc_disabled?&quot;, gc_disabled, 0);
     rb_define_method(cMysql, &quot;disable_gc=&quot;, disable_gc_set, 1);
+    rb_define_method(cMysql, &quot;busy?&quot;, is_busy, 0);
+    rb_define_method(cMysql, &quot;idle?&quot;, is_idle, 0);
+    rb_define_method(cMysql, &quot;busy=&quot;, busy_set, 1);
     rb_define_method(cMysql, &quot;socket&quot;, socket, 0);
     rb_define_method(cMysql, &quot;refresh&quot;, refresh, 1);
     rb_define_method(cMysql, &quot;reload&quot;, reload, 0);</diff>
      <filename>ext/mysql.c</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>1af85383ebc32f7f189f37b91e5932a7c398a034</id>
    </parent>
  </parents>
  <author>
    <name>Lourens Naude</name>
    <email>lourens@methodmissing.com</email>
  </author>
  <url>http://github.com/oldmoe/mysqlplus/commit/48a61dd627a6498e9c612a8e6d21d98c44d3a96d</url>
  <id>48a61dd627a6498e9c612a8e6d21d98c44d3a96d</id>
  <committed-date>2008-10-19T17:46:03-07:00</committed-date>
  <authored-date>2008-10-19T17:46:03-07:00</authored-date>
  <message>Introduce Mysql#idle? &amp;&amp; Mysql#busy? as Threaded connection pool helpers for Mysql#c_async_query</message>
  <tree>a77d030bc88aa82bb1bd2c86e19613e5b90d1d14</tree>
  <committer>
    <name>Lourens Naude</name>
    <email>lourens@methodmissing.com</email>
  </committer>
</commit>
