eagain_readwrite.diff

Charles Nutter, 03/23/2012 03:38 AM

Download (6.74 KB)

View differences:

error.c
37 37
#define WEXITSTATUS(status) (status)
38 38
#endif
39 39

  
40
VALUE rb_eEAGAIN;
41
VALUE rb_eEWOULDBLOCK;
42

  
40 43
extern const char ruby_description[];
41 44

  
42 45
#define REPORTBUG_MSG \
......
1148 1151

  
1149 1152
    if (!st_lookup(syserr_tbl, n, &error)) {
1150 1153
	error = rb_define_class_under(rb_mErrno, name, rb_eSystemCallError);
1154
	
1155
	if (n == EAGAIN) {
1156
	    rb_eEAGAIN = error;
1157
		if (n == EWOULDBLOCK) rb_eEWOULDBLOCK = error; /* same value */
1158
	}
1159
	else if (n == EWOULDBLOCK) { /* different error */
1160
	    rb_eEWOULDBLOCK = error;
1161
	}
1162
	
1151 1163
	rb_define_const(error, "Errno", INT2NUM(n));
1152 1164
	st_add_direct(syserr_tbl, n, error);
1153 1165
    }
ext/socket/ancdata.c
1284 1284

  
1285 1285
    if (ss == -1) {
1286 1286
        if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN))
1287
            rb_mod_sys_fail(rb_mWaitWritable, "sendmsg(2) would block");
1287
            rb_readwrite_sys_fail(1, "sendmsg(2) would block");
1288 1288
	rb_sys_fail("sendmsg(2)");
1289 1289
    }
1290 1290

  
......
1593 1593

  
1594 1594
    if (ss == -1) {
1595 1595
        if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN))
1596
            rb_mod_sys_fail(rb_mWaitReadable, "recvmsg(2) would block");
1596
            rb_readwrite_sys_fail(0, "recvmsg(2) would block");
1597 1597
#if defined(HAVE_ST_MSG_CONTROL)
1598 1598
        if (!gc_done && (errno == EMFILE || errno == EMSGSIZE)) {
1599 1599
          /*
ext/socket/init.c
212 212
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
213 213
	  case EWOULDBLOCK:
214 214
#endif
215
            rb_mod_sys_fail(rb_mWaitReadable, "recvfrom(2) would block");
215
            rb_readwrite_sys_fail(0, "recvfrom(2) would block");
216 216
	}
217 217
	rb_sys_fail("recvfrom(2)");
218 218
    }
......
525 525
#if defined EPROTO
526 526
	  case EPROTO:
527 527
#endif
528
            rb_mod_sys_fail(rb_mWaitReadable, "accept(2) would block");
528
            rb_readwrite_sys_fail(0, "accept(2) would block");
529 529
	}
530 530
        rb_sys_fail("accept(2)");
531 531
    }
include/ruby/ruby.h
1189 1189
NORETURN(void rb_sys_fail_str(VALUE));
1190 1190
NORETURN(void rb_mod_sys_fail(VALUE, const char*));
1191 1191
NORETURN(void rb_mod_sys_fail_str(VALUE, VALUE));
1192
NORETURN(void rb_readwrite_sys_fail(int, const char*));
1192 1193
NORETURN(void rb_iter_break(void));
1193 1194
NORETURN(void rb_iter_break_value(VALUE));
1194 1195
NORETURN(void rb_exit(int));
io.c
120 120
VALUE rb_eIOError;
121 121
VALUE rb_mWaitReadable;
122 122
VALUE rb_mWaitWritable;
123
extern VALUE rb_eEAGAIN;
124
extern VALUE rb_eEWOULDBLOCK;
125
VALUE rb_eEAGAINReadable;
126
VALUE rb_eEAGAINWritable;
127
VALUE rb_eEWOULDBLOCKReadable;
128
VALUE rb_eEWOULDBLOCKWritable;
123 129

  
124 130
VALUE rb_stdin, rb_stdout, rb_stderr;
125 131
VALUE rb_deferr;		/* rescue VIM plugin */
......
2221 2227
            if (!nonblock && rb_io_wait_readable(fptr->fd))
2222 2228
                goto again;
2223 2229
            if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN))
2224
                rb_mod_sys_fail(rb_mWaitReadable, "read would block");
2230
                rb_readwrite_sys_fail(0, "read would block");
2225 2231
            rb_sys_fail_path(fptr->pathv);
2226 2232
        }
2227 2233
    }
......
2438 2444

  
2439 2445
    if (n == -1) {
2440 2446
        if (errno == EWOULDBLOCK || errno == EAGAIN)
2441
            rb_mod_sys_fail(rb_mWaitWritable, "write would block");
2447
            rb_readwrite_sys_fail(1, "write would block");
2442 2448
        rb_sys_fail_path(fptr->pathv);
2443 2449
    }
2444 2450

  
......
11071 11077
    return rb_io_write(argf_write_io(argf), str);
11072 11078
}
11073 11079

  
11080
void
11081
rb_readwrite_sys_fail(int writable, const char *mesg)
11082
{
11083
    VALUE arg;
11084
    int n = errno;
11085
    arg = mesg ? rb_str_new2(mesg) : Qnil;
11086
    if (writable) {
11087
		if (n == EAGAIN) {
11088
		    rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEAGAINWritable));
11089
		}
11090
		else if (n == EWOULDBLOCK) {
11091
			rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEWOULDBLOCKWritable));
11092
		}
11093
		else {
11094
			rb_mod_sys_fail_str(rb_mWaitWritable, arg);
11095
		}
11096
    }
11097
    else {
11098
		if (n == EAGAIN) {
11099
		    rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEAGAINReadable));
11100
		}
11101
		else if (n == EWOULDBLOCK) {
11102
			rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEWOULDBLOCKReadable));
11103
		}
11104
		else {
11105
			rb_mod_sys_fail_str(rb_mWaitReadable, arg);
11106
		}
11107
    }
11108
}
11109

  
11074 11110
/*
11075 11111
 * Document-class: IOError
11076 11112
 *
......
11311 11347

  
11312 11348
    rb_mWaitReadable = rb_define_module_under(rb_cIO, "WaitReadable");
11313 11349
    rb_mWaitWritable = rb_define_module_under(rb_cIO, "WaitWritable");
11350
    rb_eEAGAINReadable = rb_define_class_under(rb_cIO, "EAGAINReadable", rb_eEAGAIN);
11351
    rb_include_module(rb_eEAGAINReadable, rb_mWaitReadable);
11352
    rb_eEAGAINWritable = rb_define_class_under(rb_cIO, "EAGAINWritable", rb_eEAGAIN);
11353
    rb_include_module(rb_eEAGAINWritable, rb_mWaitWritable);
11354
    if (EAGAIN == EWOULDBLOCK) {
11355
		rb_eEWOULDBLOCKReadable = rb_eEAGAINReadable;
11356
		rb_define_const(rb_cIO, "EWOULDBLOCKReadable", rb_eEAGAINReadable);
11357
		rb_eEWOULDBLOCKWritable = rb_eEAGAINWritable;
11358
		rb_define_const(rb_cIO, "EWOULDBLOCKWritable", rb_eEAGAINWritable);
11359
	}
11360
	else {
11361
	    rb_eEWOULDBLOCKReadable = rb_define_class_under(rb_cIO, "EWOULDBLOCKReadable", rb_eEWOULDBLOCK);
11362
	    rb_include_module(rb_eEWOULDBLOCKReadable, rb_mWaitReadable);
11363
	    rb_eEWOULDBLOCKWritable = rb_define_class_under(rb_cIO, "EWOULDBLOCKWritable", rb_eEWOULDBLOCK);
11364
	    rb_include_module(rb_eEWOULDBLOCKWritable, rb_mWaitWritable);
11365
	}
11314 11366

  
11315 11367
#if 0
11316 11368
    /* This is necessary only for forcing rdoc handle File::open */
test/socket/test_unix.rb
345 345

  
346 346
  def test_dgram_pair
347 347
    s1, s2 = UNIXSocket.pair(Socket::SOCK_DGRAM)
348
    assert_raise(Errno::EAGAIN) { s1.recv_nonblock(10) }
348
    begin
349
      s1.recv_nonblock(10)
350
      fail
351
    rescue => e
352
      assert(Errno::EAGAIN === e)
353
      assert(IO::WaitReadable === e)
354
    end
349 355
    s2.send("", 0)
350 356
    s2.send("haha", 0)
351 357
    s2.send("", 0)