Ruby
2.1.3p242(2014-09-19revision47630)
Main Page
Modules
Data Structures
Files
File List
Globals
ext
io
nonblock
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
}
136
Generated on Mon Sep 22 2014 11:05:22 for Ruby by
1.8.2