Ruby 2.1.5p273(2014-11-13revision48405)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

nonblock.c
Go to the documentation of this file.
1 /**********************************************************************
2
3 io/wait.c -
4
5 $Author: zzak $
6 created at: Tue Jul 14 21:53:18 2009
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 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <fcntl.h>
19
20 #ifdef F_GETFL
21 static int
22 io_nonblock_mode(int fd)
23 {
24 int f = fcntl(fd, F_GETFL);
25 if (f == -1) rb_sys_fail(0);
26 return f;
27 }
28 #else
29 #define io_nonblock_mode(fd) ((void)(fd), 0)
30 #endif
31
32 #ifdef F_GETFL
33 /*
34 * call-seq:
35 * io.nonblock? -> boolean
36 *
37 * Returns +true+ if an IO object is in non-blocking mode.
38 */
39 static VALUE
40 rb_io_nonblock_p(VALUE io)
41 {
42 rb_io_t *fptr;
43 GetOpenFile(io, fptr);
44 if (io_nonblock_mode(fptr->fd) & O_NONBLOCK)
45 return Qtrue;
46 return Qfalse;
47 }
48 #else
49 #define rb_io_nonblock_p rb_f_notimplement
50 #endif
51
52 #ifdef F_SETFL
53 static void
54 io_nonblock_set(int fd, int f, int nb)
55 {
56 if (nb) {
57 if ((f & O_NONBLOCK) != 0)
58 return;
59 f |= O_NONBLOCK;
60 }
61 else {
62 if ((f & O_NONBLOCK) == 0)
63 return;
64 f &= ~O_NONBLOCK;
65 }
66 if (fcntl(fd, F_SETFL, f) == -1)
67 rb_sys_fail(0);
68 }
69
70 /*
71 * call-seq:
72 * io.nonblock = boolean -> boolean
73 *
74 * Enables non-blocking mode on a stream when set to
75 * +true+, and blocking mode when set to +false+.
76 */
77 static VALUE
78 rb_io_nonblock_set(VALUE io, VALUE nb)
79 {
80 rb_io_t *fptr;
81 GetOpenFile(io, fptr);
82 io_nonblock_set(fptr->fd, io_nonblock_mode(fptr->fd), RTEST(nb));
83 return io;
84 }
85
86 static VALUE
87 io_nonblock_restore(VALUE arg)
88 {
89 int *restore = (int *)arg;
90 if (fcntl(restore[0], F_SETFL, restore[1]) == -1)
91 rb_sys_fail(0);
92 return Qnil;
93 }
94
95 /*
96 * call-seq:
97 * io.nonblock {|io| } -> io
98 * io.nonblock(boolean) {|io| } -> io
99 *
100 * Yields +self+ in non-blocking mode.
101 *
102 * When +false+ is given as an argument, +self+ is yielded in blocking mode.
103 * The original mode is restored after the block is executed.
104 */
105 static VALUE
106 rb_io_nonblock_block(int argc, VALUE *argv, VALUE io)
107 {
108 int nb = 1;
109 rb_io_t *fptr;
110 int f, restore[2];
111
112 GetOpenFile(io, fptr);
113 if (argc > 0) {
114 VALUE v;
115 rb_scan_args(argc, argv, "01", &v);
116 nb = RTEST(v);
117 }
118 f = io_nonblock_mode(fptr->fd);
119 restore[0] = fptr->fd;
120 restore[1] = f;
121 io_nonblock_set(fptr->fd, f, nb);
122 return rb_ensure(rb_yield, io, io_nonblock_restore, (VALUE)restore);
123 }
124 #else
125 #define rb_io_nonblock_set rb_f_notimplement
126 #define rb_io_nonblock_block rb_f_notimplement
127 #endif
128
129 void
130 Init_nonblock(void)
131 {
132 rb_define_method(rb_cIO, "nonblock?", rb_io_nonblock_p, 0);
133 rb_define_method(rb_cIO, "nonblock=", rb_io_nonblock_set, 1);
134 rb_define_method(rb_cIO, "nonblock", rb_io_nonblock_block, -1);
135 }
Qtrue
#define Qtrue
Definition: ruby.h:426
rb_io_t
Definition: io.h:61
io_nonblock_mode
#define io_nonblock_mode(fd)
Definition: nonblock.c:29
ruby.h
Init_nonblock
void Init_nonblock(void)
Definition: nonblock.c:130
fcntl
int fcntl(int, int,...)
Definition: win32.c:4085
GetOpenFile
#define GetOpenFile(obj, fp)
Definition: io.h:118
io.h
F_SETFL
#define F_SETFL
Definition: win32.h:622
rb_io_t::fd
int fd
Definition: io.h:62
unistd.h
argc
int argc
Definition: ruby.c:131
Qfalse
#define Qfalse
Definition: ruby.h:425
rb_cIO
RUBY_EXTERN VALUE rb_cIO
Definition: ruby.h:1569
rb_yield
VALUE rb_yield(VALUE)
Definition: vm_eval.c:942
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1728
Qnil
#define Qnil
Definition: ruby.h:427
VALUE
unsigned long VALUE
Definition: ruby.h:88
rb_ensure
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
Definition: eval.c:839
rb_sys_fail
void rb_sys_fail(const char *mesg)
Definition: error.c:1976
rb_io_nonblock_block
#define rb_io_nonblock_block
Definition: nonblock.c:126
f
#define f
O_NONBLOCK
#define O_NONBLOCK
Definition: win32.h:626
RTEST
#define RTEST(v)
Definition: ruby.h:437
rb_io_nonblock_p
#define rb_io_nonblock_p
Definition: nonblock.c:49
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1488
argv
char ** argv
Definition: ruby.c:132
rb_io_nonblock_set
#define rb_io_nonblock_set
Definition: nonblock.c:125