Ruby  2.1.10p492(2016-04-01revision54464)
wait.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  io/wait.c -
4 
5  $Author: nagachika $
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 INT2FIX(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  * io.wait_readable -> IO, true, false or nil
100  * io.wait_readable(timeout) -> IO, true, false or nil
101  *
102  * Waits until input is available or times out and returns self or nil when
103  * EOF is reached.
104  */
105 
106 static VALUE
108 {
109  rb_io_t *fptr;
110  int i;
111  ioctl_arg n;
112  VALUE timeout;
113  struct timeval timerec;
114  struct timeval *tv;
115 
116  GetOpenFile(io, fptr);
117  rb_io_check_readable(fptr);
118  rb_scan_args(argc, argv, "01", &timeout);
119  if (NIL_P(timeout)) {
120  tv = NULL;
121  }
122  else {
123  timerec = rb_time_interval(timeout);
124  tv = &timerec;
125  }
126 
127  if (rb_io_read_pending(fptr)) return Qtrue;
128  if (!FIONREAD_POSSIBLE_P(fptr->fd)) return Qfalse;
129  i = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_IN, tv);
130  if (i < 0)
131  rb_sys_fail(0);
132  rb_io_check_closed(fptr);
133  if (ioctl(fptr->fd, FIONREAD, &n)) rb_sys_fail(0);
134  if (n > 0) return io;
135  return Qnil;
136 }
137 
138 /*
139  * call-seq:
140  * io.wait_writable -> IO
141  * io.wait_writable(timeout) -> IO or nil
142  *
143  * Waits until IO writable is available or times out and returns self or
144  * nil when EOF is reached.
145  */
146 static VALUE
148 {
149  rb_io_t *fptr;
150  int i;
151  VALUE timeout;
152  struct timeval timerec;
153  struct timeval *tv;
154 
155  GetOpenFile(io, fptr);
156  rb_io_check_writable(fptr);
157  rb_scan_args(argc, argv, "01", &timeout);
158  if (NIL_P(timeout)) {
159  tv = NULL;
160  }
161  else {
162  timerec = rb_time_interval(timeout);
163  tv = &timerec;
164  }
165 
166  i = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_OUT, tv);
167  if (i < 0)
168  rb_sys_fail(0);
169  rb_io_check_closed(fptr);
170  if (i & RB_WAITFD_OUT)
171  return io;
172  return Qnil;
173 }
174 
175 /*
176  * IO wait methods
177  */
178 
179 void
181 {
182  rb_define_method(rb_cIO, "nread", io_nread, 0);
183  rb_define_method(rb_cIO, "ready?", io_ready_p, 0);
185  rb_define_method(rb_cIO, "wait_readable", io_wait_readable, -1);
186  rb_define_method(rb_cIO, "wait_writable", io_wait_writable, -1);
187 }
struct timeval rb_time_interval(VALUE num)
Definition: time.c:2411
Definition: io.c:8714
int ioctl(int, int,...)
Definition: win32.c:2544
void rb_io_check_readable(rb_io_t *)
Definition: io.c:794
#define Qtrue
Definition: ruby.h:426
static VALUE io_ready_p _((VALUE io))
Definition: io.h:61
#define FIONREAD_POSSIBLE_P(fd)
Definition: wait.c:39
#define GetOpenFile(obj, fp)
Definition: io.h:118
#define RB_WAITFD_OUT
Definition: io.h:49
void Init_wait()
Definition: wait.c:180
#define NIL_P(v)
Definition: ruby.h:438
int fd
Definition: io.h:62
int argc
Definition: ruby.c:131
#define Qfalse
Definition: ruby.h:425
RUBY_EXTERN VALUE rb_cIO
Definition: ruby.h:1577
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1719
#define Qnil
Definition: ruby.h:427
static VALUE io_wait_readable(int argc, VALUE *argv, VALUE io)
Definition: wait.c:107
unsigned long VALUE
Definition: ruby.h:88
#define ioctl_arg2num(i)
Definition: wait.c:33
int rb_wait_for_single_fd(int fd, int events, struct timeval *tv)
Definition: thread.c:3779
void rb_sys_fail(const char *mesg)
Definition: error.c:1976
int rb_io_read_pending(rb_io_t *)
Definition: io.c:830
#define INT2FIX(i)
Definition: ruby.h:231
static VALUE io_wait_writable(int argc, VALUE *argv, VALUE io)
Definition: wait.c:147
void rb_io_check_writable(rb_io_t *)
Definition: io.c:818
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:1479
void rb_io_check_closed(rb_io_t *)
Definition: io.c:617
char ** argv
Definition: ruby.c:132