Ruby 2.0.0p353(2013-11-22revision43784)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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
56 io_nread(VALUE io)
57 {
58 rb_io_t *fptr;
59 int len;
60 ioctl_arg n;
61
62 GetOpenFile(io, fptr);
63 rb_io_check_readable(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
81 io_ready_p(VALUE io)
82 {
83 rb_io_t *fptr;
84 ioctl_arg n;
85
86 GetOpenFile(io, fptr);
87 rb_io_check_readable(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
105 io_wait_readable(int argc, VALUE *argv, VALUE io)
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
145 io_wait_writable(int argc, VALUE *argv, VALUE io)
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
178 Init_wait()
179 {
180 rb_define_method(rb_cIO, "nread", io_nread, 0);
181 rb_define_method(rb_cIO, "ready?", io_ready_p, 0);
182 rb_define_method(rb_cIO, "wait", io_wait_readable, -1);
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 }
n
ssize_t n
Definition: bigdecimal.c:5655
ioctl_arg
Definition: io.c:8498
ioctl
int ioctl(int, int,...)
Definition: win32.c:2438
rb_io_check_readable
void rb_io_check_readable(rb_io_t *)
Definition: io.c:781
i
ssize_t i
Definition: bigdecimal.c:5655
rb_io_t
Definition: io.h:63
ruby.h
NIL_P
NIL_P(eventloop_thread)
Definition: tcltklib.c:4068
FIONREAD_POSSIBLE_P
#define FIONREAD_POSSIBLE_P(fd)
Definition: wait.c:39
Qtrue
return Qtrue
Definition: tcltklib.c:9610
GetOpenFile
#define GetOpenFile(obj, fp)
Definition: io.h:120
io.h
RB_WAITFD_OUT
#define RB_WAITFD_OUT
Definition: io.h:49
Qfalse
return Qfalse
Definition: tcltklib.c:6779
Qnil
#define Qnil
Definition: tcltklib.c:1896
Init_wait
void Init_wait()
Definition: wait.c:178
INT2FIX
#define INT2FIX(i)
rb_io_t::fd
int fd
Definition: io.h:64
unistd.h
argv
VALUE * argv
Definition: tcltklib.c:1971
timeval
Definition: ossl_asn1.c:16
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1570
io_wait_readable
static VALUE io_wait_readable(int argc, VALUE *argv, VALUE io)
Definition: wait.c:105
argc
int argc
Definition: tcltklib.c:1970
ioctl_arg2num
#define ioctl_arg2num(i)
Definition: wait.c:33
rb_wait_for_single_fd
int rb_wait_for_single_fd(int fd, int events, struct timeval *tv)
Definition: thread.c:3687
rb_sys_fail
void rb_sys_fail(const char *mesg)
Definition: error.c:1899
rb_cIO
RUBY_EXTERN VALUE rb_cIO
Definition: ripper.y:1442
_
#define _(args)
Definition: dln.h:28
rb_io_read_pending
int rb_io_read_pending(rb_io_t *)
Definition: io.c:817
rb_time_interval
struct timeval rb_time_interval(VALUE num)
Definition: time.c:2492
io_wait_writable
static VALUE io_wait_writable(int argc, VALUE *argv, VALUE io)
Definition: wait.c:145
io
gz io
Definition: zlib.c:2263
rb_io_check_writable
void rb_io_check_writable(rb_io_t *)
Definition: io.c:805
io_nread
static VALUE io_nread(VALUE io)
Definition: wait.c:56
VALUE
unsigned long VALUE
Definition: ripper.y:104
io_ready_p
static VALUE io_ready_p(VALUE io)
Definition: wait.c:81
RB_WAITFD_IN
#define RB_WAITFD_IN
Definition: io.h:47
NULL
#define NULL
Definition: _sdbm.c:103
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344
rb_io_check_closed
void rb_io_check_closed(rb_io_t *)
Definition: io.c:604
len
size_t len
Definition: tcltklib.c:3568