<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,4 +1 @@
 * Set timeout from Ruby
-* Make finding free connection O(1)
-* Sending using writev
-* Implement queue of Ruby strings for write buffer
\ No newline at end of file</diff>
      <filename>TODO</filename>
    </modified>
    <modified>
      <diff>@@ -134,7 +134,7 @@ VALUE backend_close(VALUE self)
   if (backend-&gt;open) {
     backend-&gt;open = 0;
     ev_io_stop(backend-&gt;loop, &amp;backend-&gt;accept_watcher);
-    close(backend-&gt;fd);    
+    close(backend-&gt;fd);
   }
   
   return Qtrue;
@@ -169,7 +169,7 @@ VALUE backend_get_maxfds(VALUE self)
 static void backend_free(backend_t *backend)
 {
   if (backend) {
-    array_destroy(backend-&gt;connections);
+    connections_free(backend);    
     free(backend);
   }
 }
@@ -179,8 +179,7 @@ VALUE backend_alloc(VALUE klass)
   backend_t *backend = ALLOC_N(backend_t, 1);
   VALUE obj = Data_Wrap_Struct(klass, NULL, backend_free, backend);
   
-  backend-&gt;connections = array_create(CONNECTIONS_SIZE, sizeof(connection_t));
-  connections_create(backend-&gt;connections, CONNECTIONS_SIZE);
+  connections_push(backend);
   
   backend-&gt;obj = obj;
   </diff>
      <filename>ext/thin_backend/backend.c</filename>
    </modified>
    <modified>
      <diff>@@ -81,31 +81,17 @@ static void connection_timeout_cb(EV_P_ struct ev_timer *watcher, int revents)
 
 void connection_start(backend_t *backend, int fd, struct sockaddr_in remote_addr)
 {
-  connection_t *c  = NULL;
-  connection_t *cs = backend-&gt;connections-&gt;items;
-  int           i  = 0;
-  
-  /* select the first closed connection */
-  for (i = 0; i &lt; backend-&gt;connections-&gt;nitems; i++) {
-    if (!cs[i].open) {
-      c = &amp;cs[i];
-      break;
-    }
-  }
+  connection_t *c = (connection_t *) queue_pop(&amp;backend-&gt;connections);
   
   /* no free connection found, add more */
   if (c == NULL) {
-    connections_create(backend-&gt;connections, CONNECTIONS_SIZE);
-    cs = backend-&gt;connections-&gt;items;
-    /* FIXME: bug here on high concurrency, causes segfault in libev code */
-    c = &amp;cs[++i];
+    connections_push(backend);
+    c = (connection_t *) queue_pop(&amp;backend-&gt;connections);
   }
   
   assert(c != NULL);
-  assert(!c-&gt;open);
   
   /* init connection */
-  c-&gt;open           = 1;
   c-&gt;finished       = 0;
   c-&gt;loop           = backend-&gt;loop;
   c-&gt;backend        = backend;
@@ -335,7 +321,8 @@ void connection_close(connection_t *c)
   
   /* TODO maybe kill the thread also: rb_thread_kill(c-&gt;thread) */
   
-  c-&gt;open = 0;
+  /* put back in the queue of unused connections */
+  queue_push(&amp;c-&gt;backend-&gt;connections, c);
 }
 
 
@@ -351,21 +338,32 @@ void connections_init()
   rb_gc_register_address(&amp;sRackInput);
 }
 
-void connections_create(array_t *connections, size_t num)
+void connections_push(backend_t *backend)
 {
+  size_t        i;
   connection_t *c;
-  int           i;
   
-  for (i = 0; i &lt;= num; ++i) {
-    c = array_push(connections);
+  for (i = 0; i &lt; CONNECTIONS_SIZE; ++i) {
+    c = (connection_t *) malloc(sizeof(connection_t));
     assert(c);
     
-    c-&gt;open = 0;
     parser_callbacks_setup(c);
     
     buffer_init(&amp;c-&gt;read_buffer);
     buffer_init(&amp;c-&gt;write_buffer);
     
     ev_timer_init(&amp;c-&gt;timeout_watcher, connection_timeout_cb, CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);
+    
+    queue_push(&amp;backend-&gt;connections, c);
+  }
+}
+
+void connections_free(backend_t *backend)
+{
+  connection_t *c = queue_pop(&amp;backend-&gt;connections);
+  
+  while (c != NULL) {
+    free(c);
+    c = queue_pop(&amp;backend-&gt;connections);
   }
 }</diff>
      <filename>ext/thin_backend/connection.c</filename>
    </modified>
    <modified>
      <diff>@@ -27,10 +27,10 @@
 #endif
 
 #include &quot;ext_help.h&quot;
-#include &quot;array.h&quot;
 #include &quot;buffer.h&quot;
 #include &quot;palloc.h&quot;
 #include &quot;parser.h&quot;
+#include &quot;queue.h&quot;
 #include &quot;status.h&quot;
 
 #ifdef __FreeBSD__
@@ -63,7 +63,6 @@ typedef struct connection_s connection_t;
 
 struct connection_s {
   /* socket */
-  unsigned            open : 1;
   int                 fd;
   char               *remote_addr;
   
@@ -92,16 +91,16 @@ struct backend_s {
   /* socket */
   char               *address;
   unsigned            port;
-  unsigned            open : 1;
   int                 fd;
+  unsigned            open;
   struct sockaddr_in  local_addr;
   
   /* ruby */
   VALUE               obj; /* Ruby Backend object */
   VALUE               app; /* Rack app */
   
-  /* pools */
-  array_t            *connections;
+  /* connections */
+  queue_t             connections;
   
   /* libev */
   struct ev_loop     *loop;
@@ -142,7 +141,8 @@ void connection_close(connection_t *connection);
 
 /* connections */
 void connections_init();
-void connections_create(array_t *connections, size_t num);
+void connections_push(backend_t *backend);
+void connections_free(backend_t *backend);
 
 /* parser */
 void parser_callbacks_init();</diff>
      <filename>ext/thin_backend/thin.h</filename>
    </modified>
    <modified>
      <diff>@@ -1,8 +1,8 @@
 CC = gcc
 CFLAGS = -fno-common -g -O2 -pipe -I../ext/thin_backend -DDEBUG
 
-OBJS = $(addprefix ../ext/thin_backend/,palloc.o array.o buffer.o queue.o)
-TARGETS = $(addsuffix _test,palloc array status buffer queue)
+OBJS = $(addprefix ../ext/thin_backend/,palloc.o buffer.o queue.o)
+TARGETS = $(addsuffix _test,palloc status buffer queue)
 
 %: %.c
 	$(CC) $(CFLAGS) -c $&lt;</diff>
      <filename>test/Makefile</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>b237e2c035baf912ad4f8c4ea835ea25a8ba3645</id>
    </parent>
  </parents>
  <author>
    <name>macournoyer</name>
    <email>macournoyer@gmail.com</email>
  </author>
  <url>http://github.com/macournoyer/thin-turbo/commit/9062ac39f5ea1833f22c42129f90e7bedcf6af00</url>
  <id>9062ac39f5ea1833f22c42129f90e7bedcf6af00</id>
  <committed-date>2008-04-29T20:07:08-07:00</committed-date>
  <authored-date>2008-04-29T20:07:08-07:00</authored-date>
  <message>Initial work on using queue for getting free connection</message>
  <tree>de3a7386e0ddda3c417e2cdf7b1251fb078f1bd2</tree>
  <committer>
    <name>macournoyer</name>
    <email>macournoyer@gmail.com</email>
  </committer>
</commit>
