<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[2039] MacRuby/branches/experimental</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.macosforge.org/projects/ruby/changeset/2039">2039</a></dd>
<dt>Author</dt> <dd>pthomson@apple.com</dd>
<dt>Date</dt> <dd>2009-07-21 11:06:06 -0700 (Tue, 21 Jul 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>Implemented Kernel#select and ensured that all the specs pass.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#MacRubybranchesexperimentalioc">MacRuby/branches/experimental/io.c</a></li>
</ul>

<h3>Removed Paths</h3>
<ul>
<li><a href="#MacRubybranchesexperimentalspecfrozentagsmacrubycoreioselect_tagstxt">MacRuby/branches/experimental/spec/frozen/tags/macruby/core/io/select_tags.txt</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="MacRubybranchesexperimentalioc"></a>
<div class="modfile"><h4>Modified: MacRuby/branches/experimental/io.c (2038 => 2039)</h4>
<pre class="diff"><span>
<span class="info">--- MacRuby/branches/experimental/io.c        2009-07-21 10:45:32 UTC (rev 2038)
+++ MacRuby/branches/experimental/io.c        2009-07-21 18:06:06 UTC (rev 2039)
</span><span class="lines">@@ -3209,18 +3209,6 @@
</span><span class="cx">     return bstr;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-// static VALUE
-// select_call(VALUE arg)
-// {
-// rb_notimplement();
-// }
-// 
-// static VALUE
-// select_end(VALUE arg)
-// {
-// rb_notimplement();
-// }
-
</del><span class="cx"> /*
</span><span class="cx">  *  call-seq:
</span><span class="cx">  *     IO.select(read_array
</span><span class="lines">@@ -3231,12 +3219,128 @@
</span><span class="cx">  *  See &lt;code&gt;Kernel#select&lt;/code&gt;.
</span><span class="cx">  */
</span><span class="cx"> 
</span><ins>+// helper method. returns the highest-valued file descriptor encountered.
+static int
+build_fd_set_from_io_array(fd_set* set, VALUE arr)
+{
+        int max_fd = 0;
+        FD_ZERO(set);
+        if (!NIL_P(arr))
+        {
+                if (TYPE(arr) != T_ARRAY)
+                {
+                        rb_raise(rb_eTypeError, &quot;Kernel#select expects arrays of IO objects&quot;);
+                }
+
+                long n = RARRAY_LEN(arr);
+                long ii;
+                for(ii=0; ii&lt;n; ii++)
+                {
+                        VALUE io = RARRAY_AT(arr, ii);
+                        io = rb_check_convert_type(io, T_FILE, &quot;IO&quot;, &quot;to_io&quot;);
+                        if (NIL_P(io))
+                        {
+                                rb_raise(rb_eTypeError, &quot;Kernel#select expects arrays of IO objects&quot;);
+                        }
+                        int fd = ExtractIOStruct(io)-&gt;fd;
+                        FD_SET(fd, set);
+                        max_fd = MAX(fd, max_fd);
+                }
+        }
+        return max_fd;
+}
+
+static void
+build_timeval_from_numeric(struct timeval *tv, VALUE num)
+{
+        tv-&gt;tv_sec = 0L;
+        tv-&gt;tv_usec = 0L;
+        if(FIXNUM_P(num))
+        {
+                if (FIX2LONG(num) &lt; 0)
+                {
+                        rb_raise(rb_eArgError, &quot;select() does not accept negative timeouts.&quot;);
+                }
+                tv-&gt;tv_sec = FIX2LONG(num);
+        }
+        else if (FIXFLOAT_P(num))
+        {
+                double quantity = RFLOAT_VALUE(num);
+                if (quantity &lt; 0.0)
+                {
+                        rb_raise(rb_eArgError, &quot;select() does not accept negative timeouts.&quot;);
+                }
+                tv-&gt;tv_sec = (long)floor(quantity);
+                tv-&gt;tv_usec = (long)(1000 * (quantity - floor(quantity)));
+        }
+        else if (!NIL_P(num))
+        {
+                rb_raise(rb_eTypeError, &quot;timeout parameter must be numeric.&quot;);
+        }
+}
+
</ins><span class="cx"> static VALUE
</span><ins>+extract_ready_descriptors(VALUE arr, fd_set* set)
+{
+        VALUE ready_ios = rb_ary_new();
+        if (NIL_P(arr))
+        {
+                return ready_ios;
+        }
+        long len = RARRAY_LEN(arr);
+        long ii;
+        for (ii=0; ii&lt;len; ii++)
+        {
+                VALUE io = RARRAY_AT(arr, ii);
+                VALUE tmp = rb_check_convert_type(io, T_FILE, &quot;IO&quot;, &quot;to_io&quot;);
+                if (FD_ISSET(ExtractIOStruct(tmp)-&gt;fd, set))
+                {
+                        rb_ary_push(ready_ios, io);
+                }
+        }
+        return ready_ios;
+}
+
+static VALUE
</ins><span class="cx"> rb_f_select(VALUE recv, SEL sel, int argc, VALUE *argv)
</span><span class="cx"> {
</span><del>-    rb_notimplement();
</del><ins>+        VALUE read_arr, write_arr, err_arr, timeout_val;
+        VALUE readable_arr, writable_arr, errored_arr;
+        rb_scan_args(argc, argv, &quot;13&quot;, &amp;read_arr, &amp;write_arr, &amp;err_arr, &amp;timeout_val);
+        fd_set read_set, write_set, err_set;
+        struct timeval timeout;
+        
+        // ndfs has to be 1 + the highest fd we're looking for.
+        int temp = 0;
+        int ndfs = build_fd_set_from_io_array(&amp;read_set, read_arr);
+        temp = build_fd_set_from_io_array(&amp;write_set, write_arr);
+        ndfs = MAX(temp, ndfs);
+        temp = build_fd_set_from_io_array(&amp;err_set, err_arr);
+        ndfs = MAX(temp, ndfs);
+        ndfs++;
+        
+        
+        // A timeval of 0 needs to be distinguished from a NULL timeval, as a 
+        // NULL timeval polls indefinitly while a 0 timeval returns immediately.
+        build_timeval_from_numeric(&amp;timeout, timeout_val);
+        struct timeval *timeval_ptr = (NIL_P(timeout_val) ? NULL : &amp;timeout);
+        
+        int ready_count = select(ndfs, &amp;read_set, &amp;write_set, &amp;err_set, timeval_ptr);
+        if(ready_count == -1) 
+        {
+                rb_sys_fail(&quot;select(2) failed&quot;);
+        }
+        else if (ready_count == 0)
+        {
+                return Qnil; // no ready file descriptors? return 0.
+        }
+        
+        readable_arr = extract_ready_descriptors(read_arr, &amp;read_set);
+        writable_arr = extract_ready_descriptors(write_arr, &amp;write_set);
+        errored_arr = extract_ready_descriptors(err_arr, &amp;err_set);
+        
+        return rb_ary_new3(3, readable_arr, writable_arr, errored_arr);
</ins><span class="cx"> }
</span><del>-
</del><span class="cx"> // Here be dragons.
</span><span class="cx"> static VALUE
</span><span class="cx"> rb_io_ctl(VALUE io, VALUE req, VALUE arg, int is_io)
</span></span></pre></div>
<a id="MacRubybranchesexperimentalspecfrozentagsmacrubycoreioselect_tagstxt"></a>
<div class="delfile"><h4>Deleted: MacRuby/branches/experimental/spec/frozen/tags/macruby/core/io/select_tags.txt (2038 => 2039)</h4>
<pre class="diff"><span>
<span class="info">--- MacRuby/branches/experimental/spec/frozen/tags/macruby/core/io/select_tags.txt        2009-07-21 10:45:32 UTC (rev 2038)
+++ MacRuby/branches/experimental/spec/frozen/tags/macruby/core/io/select_tags.txt        2009-07-21 18:06:06 UTC (rev 2039)
</span><span class="lines">@@ -1,11 +0,0 @@
</span><del>-fails:IO.select blocks for duration of timeout if there are no objects ready for I/O
-fails:IO.select returns immediately all objects that are ready for I/O when timeout is 0
-fails:IO.select returns nil after timeout if there are no objects ready for I/O
-fails:IO.select returns supplied objects when they are ready for I/O
-fails:IO.select returns supplied objects correctly even when monitoring the same object in different arrays
-fails:IO.select invokes to_io on supplied objects that are not IO
-fails:IO.select raises TypeError if supplied objects are not IO
-fails:IO.select raises TypeError if the specified timeout value is not Numeric
-fails:IO.select raises TypeError if the first three arguments are not Arrays
-fails:IO.select does not raise errors if the first three arguments are nil
-fails:IO.select does not accept negative timeouts
</del></span></pre>
</div>
</div>

</body>
</html>