ext/kgio/read_write.c in kgio-1.3.1 vs ext/kgio/read_write.c in kgio-2.0.0pre1
- old
+ new
@@ -1,7 +1,7 @@
#include "kgio.h"
-static VALUE mKgio_WaitReadable, mKgio_WaitWritable;
+static VALUE sym_wait_readable, sym_wait_writable;
static VALUE eErrno_EPIPE, eErrno_ECONNRESET;
/*
* we know MSG_DONTWAIT works properly on all stream sockets under Linux
* we can define this macro for other platforms as people care and
@@ -24,11 +24,11 @@
rb_exc_raise(exc);
}
static void my_eof_error(void)
{
- raise_empty_bt(rb_eEOFError, "");
+ raise_empty_bt(rb_eEOFError, "end of file reached");
}
static void wr_sys_fail(const char *msg)
{
switch (errno) {
@@ -65,18 +65,18 @@
if (errno == EINTR)
return -1;
rb_str_set_len(a->buf, 0);
if (errno == EAGAIN) {
if (io_wait) {
- kgio_wait_readable(a->io, a->fd);
+ (void)kgio_call_wait_readable(a->io);
/* buf may be modified in other thread/fiber */
rb_str_resize(a->buf, a->len);
a->ptr = RSTRING_PTR(a->buf);
return -1;
} else {
- a->buf = mKgio_WaitReadable;
+ a->buf = sym_wait_readable;
return 0;
}
}
rb_sys_fail(msg);
}
@@ -110,12 +110,12 @@
* io.kgio_read(maxlen, buffer) -> buffer
*
* Reads at most maxlen bytes from the stream socket. Returns with a
* newly allocated buffer, or may reuse an existing buffer if supplied.
*
- * Calls the method assigned to Kgio.wait_readable, or blocks in a
- * thread-safe manner for writability.
+ * Calls whatever is is defined to be the kgio_wait_readable method
+ * for the class.
*
* Returns nil on EOF.
*
* This behaves like read(2) and IO#readpartial, NOT fread(3) or
* IO#read which possess read-in-full behavior.
@@ -125,11 +125,13 @@
return my_read(1, argc, argv, io);
}
/*
* Same as Kgio::PipeMethods#kgio_read, except EOFError is raised
- * on EOF without a backtrace
+ * on EOF without a backtrace. This method is intended as a
+ * drop-in replacement for places where IO#readpartial is used, and
+ * may be aliased as such.
*/
static VALUE kgio_read_bang(int argc, VALUE *argv, VALUE io)
{
VALUE rv = my_read(1, argc, argv, io);
@@ -146,11 +148,11 @@
* Reads at most maxlen bytes from the stream socket. Returns with a
* newly allocated buffer, or may reuse an existing buffer if supplied.
*
* Returns nil on EOF.
*
- * Returns Kgio::WaitReadable if EAGAIN is encountered.
+ * Returns :wait_readable if EAGAIN is encountered.
*/
static VALUE kgio_tryread(int argc, VALUE *argv, VALUE io)
{
return my_read(0, argc, argv, io);
}
@@ -228,22 +230,22 @@
return -1;
if (errno == EAGAIN) {
long written = RSTRING_LEN(a->buf) - a->len;
if (io_wait) {
- kgio_wait_writable(a->io, a->fd);
+ (void)kgio_call_wait_writable(a->io);
/* buf may be modified in other thread/fiber */
a->len = RSTRING_LEN(a->buf) - written;
if (a->len <= 0)
goto done;
a->ptr = RSTRING_PTR(a->buf) + written;
return -1;
} else if (written > 0) {
a->buf = rb_str_new(a->ptr, a->len);
} else {
- a->buf = mKgio_WaitWritable;
+ a->buf = sym_wait_writable;
}
return 0;
}
wr_sys_fail(msg);
} else {
@@ -274,30 +276,29 @@
*
* io.kgio_write(str) -> nil
*
* Returns nil when the write completes.
*
- * Calls the method Kgio.wait_writable if it is set. Otherwise this
- * blocks in a thread-safe manner until all data is written or a
- * fatal error occurs.
+ * Calls whatever is is defined to be the kgio_wait_writable method
+ * for the class.
*/
static VALUE kgio_write(VALUE io, VALUE str)
{
return my_write(io, str, 1);
}
/*
* call-seq:
*
- * io.kgio_trywrite(str) -> nil or Kgio::WaitWritable
+ * io.kgio_trywrite(str) -> nil or :wait_writable
*
* Returns nil if the write was completed in full.
*
* Returns a String containing the unwritten portion if EAGAIN
* was encountered, but some portion was successfully written.
*
- * Returns Kgio::WaitWritable if EAGAIN is encountered and nothing
+ * Returns :wait_writable if EAGAIN is encountered and nothing
* was written.
*/
static VALUE kgio_trywrite(VALUE io, VALUE str)
{
return my_write(io, str, 0);
@@ -348,13 +349,14 @@
void init_kgio_read_write(void)
{
VALUE mPipeMethods, mSocketMethods;
VALUE mKgio = rb_define_module("Kgio");
+ VALUE mWaiters = rb_const_get(mKgio, rb_intern("DefaultWaiters"));
- mKgio_WaitReadable = rb_const_get(mKgio, rb_intern("WaitReadable"));
- mKgio_WaitWritable = rb_const_get(mKgio, rb_intern("WaitWritable"));
+ sym_wait_readable = ID2SYM(rb_intern("wait_readable"));
+ sym_wait_writable = ID2SYM(rb_intern("wait_writable"));
/*
* Document-module: Kgio::PipeMethods
*
* This module may be used used to create classes that respond to
@@ -389,6 +391,8 @@
*/
rb_define_attr(mSocketMethods, "kgio_addr", 1, 1);
eErrno_EPIPE = rb_const_get(rb_mErrno, rb_intern("EPIPE"));
eErrno_ECONNRESET = rb_const_get(rb_mErrno, rb_intern("ECONNRESET"));
+ rb_include_module(mPipeMethods, mWaiters);
+ rb_include_module(mSocketMethods, mWaiters);
}