Ruby  2.0.0p648(2015-12-16revision53162)
wait.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  io/wait.c -
4 
5  $Author: nobu $
6  created at: Tue Aug 28 09:08:06 JST 2001
7 
8  All the files in this distribution are covered under the Ruby's
9  license (see the file COPYING).
10 
11 **********************************************************************/
12 
13 #include "ruby.h"
14 #include "ruby/io.h"
15 
16 #include <sys/types.h>
17 #if defined(HAVE_UNISTD_H) && (defined(__sun))
18 #include <unistd.h>
19 #endif
20 #if defined(HAVE_SYS_IOCTL_H)
21 #include <sys/ioctl.h>
22 #endif
23 #if defined(FIONREAD_HEADER)
24 #include FIONREAD_HEADER
25 #endif
26 
27 #ifdef HAVE_RB_W32_IOCTLSOCKET
28 #define ioctl ioctlsocket
29 #define ioctl_arg u_long
30 #define ioctl_arg2num(i) ULONG2NUM(i)
31 #else
32 #define ioctl_arg int
33 #define ioctl_arg2num(i) INT2NUM(i)
34 #endif
35 
36 #ifdef HAVE_RB_W32_IS_SOCKET
37 #define FIONREAD_POSSIBLE_P(fd) rb_w32_is_socket(fd)
38 #else
39 #define FIONREAD_POSSIBLE_P(fd) ((void)(fd),Qtrue)
40 #endif
41 
42 static VALUE io_ready_p _((VALUE io));
43 static VALUE io_wait_readable _((int argc, VALUE *argv, VALUE io));
44 static VALUE io_wait_writable _((int argc, VALUE *argv, VALUE io));
45 void Init_wait _((void));
46 
47 /*
48  * call-seq:
49  * io.nread -> int
50  *
51  * Returns number of bytes that can be read without blocking.
52  * Returns zero if no information available.
53  */
54 
55 static VALUE
57 {
58  rb_io_t *fptr;
59  int len;
60  ioctl_arg n;
61 
62  GetOpenFile(io, fptr);
64  len = rb_io_read_pending(fptr);
65  if (len > 0) return len;
66  if (!FIONREAD_POSSIBLE_P(fptr->fd)) return INT2FIX(0);
67  if (ioctl(fptr->fd, FIONREAD, &n)) return INT2FIX(0);
68  if (n > 0) return ioctl_arg2num(n);
69  return INT2FIX(0);
70 }
71 
72 /*
73  * call-seq:
74  * io.ready? -> true, false or nil
75  *
76  * Returns true if input available without blocking, or false.
77  * Returns nil if no information available.
78  */
79 
80 static VALUE
82 {
83  rb_io_t *fptr;
84  ioctl_arg n;
85 
86  GetOpenFile(io, fptr);
88  if (rb_io_read_pending(fptr)) return Qtrue;
89  if (!FIONREAD_POSSIBLE_P(fptr->fd)) return Qnil;
90  if (ioctl(fptr->fd, FIONREAD, &n)) return Qnil;
91  if (n > 0) return Qtrue;
92  return Qfalse;
93 }
94 
95 /*
96  * call-seq:
97  * io.wait -> IO, true, false or nil
98  * io.wait(timeout) -> IO, true, false or nil
99  *
100  * Waits until input is available or times out and returns self or nil when
101  * EOF is reached.
102  */
103 
104 static VALUE
106 {
107  rb_io_t *fptr;
108  int i;
109  ioctl_arg n;
110  VALUE timeout;
111  struct timeval timerec;
112  struct timeval *tv;
113 
114  GetOpenFile(io, fptr);
115  rb_io_check_readable(fptr);
116  rb_scan_args(argc, argv, "01", &timeout);
117  if (NIL_P(timeout)) {
118  tv = NULL;
119  }
120  else {
121  timerec = rb_time_interval(timeout);
122  tv = &timerec;
123  }
124 
125  if (rb_io_read_pending(fptr)) return Qtrue;
126  if (!FIONREAD_POSSIBLE_P(fptr->fd)) return Qfalse;
127  i = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_IN, tv);
128  if (i < 0)
129  rb_sys_fail(0);
130  rb_io_check_closed(fptr);
131  if (ioctl(fptr->fd, FIONREAD, &n)) rb_sys_fail(0);
132  if (n > 0) return io;
133  return Qnil;
134 }
135 
136 /*
137  * call-seq:
138  * io.wait_writable -> IO
139  * io.wait_writable(timeout) -> IO or nil
140  *
141  * Waits until IO writable is available or times out and returns self or
142  * nil when EOF is reached.
143  */
144 static VALUE
146 {
147  rb_io_t *fptr;
148  int i;
149  VALUE timeout;
150  struct timeval timerec;
151  struct timeval *tv;
152 
153  GetOpenFile(io, fptr);
154  rb_io_check_writable(fptr);
155  rb_scan_args(argc, argv, "01", &timeout);
156  if (NIL_P(timeout)) {
157  tv = NULL;
158  }
159  else {
160  timerec = rb_time_interval(timeout);
161  tv = &timerec;
162  }
163 
164  i = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_OUT, tv);
165  if (i < 0)
166  rb_sys_fail(0);
167  rb_io_check_closed(fptr);
168  if (i & RB_WAITFD_OUT)
169  return io;
170  return Qnil;
171 }
172 
173 /*
174  * IO wait methods
175  */
176 
177 void
179 {
180  rb_define_method(rb_cIO, "nread", io_nread, 0);
181  rb_define_method(rb_cIO, "ready?", io_ready_p, 0);
183  rb_define_method(rb_cIO, "wait_readable", io_wait_readable, -1);
184  rb_define_method(rb_cIO, "wait_writable", io_wait_writable, -1);
185 }
struct timeval rb_time_interval(VALUE num)
Definition: time.c:2496
Definition: io.c:8533
int ioctl(int, int,...)
Definition: win32.c:2444
void rb_io_check_readable(rb_io_t *)
Definition: io.c:789
int i
Definition: win32ole.c:784
#define Qtrue
Definition: ruby.h:434
static VALUE io_ready_p _((VALUE io))
Definition: io.h:63
#define FIONREAD_POSSIBLE_P(fd)
Definition: wait.c:39
#define GetOpenFile(obj, fp)
Definition: io.h:120
#define RB_WAITFD_OUT
Definition: io.h:49
void Init_wait()
Definition: wait.c:178
#define NIL_P(v)
Definition: ruby.h:446
int fd
Definition: io.h:64
int argc
Definition: ruby.c:130
#define Qfalse
Definition: ruby.h:433
RUBY_EXTERN VALUE rb_cIO
Definition: ruby.h:1442
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1570
#define Qnil
Definition: ruby.h:435
static VALUE io_wait_readable(int argc, VALUE *argv, VALUE io)
Definition: wait.c:105
unsigned long VALUE
Definition: ruby.h:104
#define ioctl_arg2num(i)
Definition: wait.c:33
int rb_wait_for_single_fd(int fd, int events, struct timeval *tv)
Definition: thread.c:3695
void rb_sys_fail(const char *mesg)
Definition: error.c:1907
int rb_io_read_pending(rb_io_t *)
Definition: io.c:825
#define INT2FIX(i)
Definition: ruby.h:241
static VALUE io_wait_writable(int argc, VALUE *argv, VALUE io)
Definition: wait.c:145
void rb_io_check_writable(rb_io_t *)
Definition: io.c:813
static VALUE io_nread(VALUE io)
Definition: wait.c:56
static VALUE io_ready_p(VALUE io)
Definition: wait.c:81
#define RB_WAITFD_IN
Definition: io.h:47
#define NULL
Definition: _sdbm.c:102
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344
void rb_io_check_closed(rb_io_t *)
Definition: io.c:612
char ** argv
Definition: ruby.c:131