1 /* we do not modify RSTRING pointers here */
5 static VALUE sym_wait_writable
;
16 static void prepare_write(struct wr_args
*a
, VALUE io
, VALUE str
)
18 a
->buf
= (TYPE(str
) == T_STRING
) ? str
: rb_obj_as_string(str
);
19 a
->ptr
= RSTRING_PTR(a
->buf
);
20 a
->len
= RSTRING_LEN(a
->buf
);
22 a
->fd
= my_fileno(io
);
25 static int write_check(struct wr_args
*a
, long n
, const char *msg
, int io_wait
)
32 a
->fd
= my_fileno(a
->io
);
35 if (errno
== EAGAIN
) {
36 long written
= RSTRING_LEN(a
->buf
) - a
->len
;
39 (void)kgio_call_wait_writable(a
->io
);
41 /* buf may be modified in other thread/fiber */
42 a
->len
= RSTRING_LEN(a
->buf
) - written
;
45 a
->ptr
= RSTRING_PTR(a
->buf
) + written
;
47 } else if (written
> 0) {
48 a
->buf
= MY_STR_SUBSEQ(a
->buf
, written
, a
->len
);
50 a
->buf
= sym_wait_writable
;
54 kgio_wr_sys_fail(msg
);
56 assert(n
>= 0 && n
< a
->len
&& "write/send syscall broken?");
64 static VALUE
my_write(VALUE io
, VALUE str
, int io_wait
)
69 prepare_write(&a
, io
, str
);
70 set_nonblocking(a
.fd
);
72 n
= (long)write(a
.fd
, a
.ptr
, a
.len
);
73 if (write_check(&a
, n
, "write", io_wait
) != 0)
81 * io.kgio_write(str) -> nil
83 * Returns nil when the write completes.
85 * This may block and call any method defined to +kgio_wait_writable+
88 static VALUE
kgio_write(VALUE io
, VALUE str
)
90 return my_write(io
, str
, 1);
96 * io.kgio_trywrite(str) -> nil, String or :wait_writable
98 * Returns nil if the write was completed in full.
100 * Returns a String containing the unwritten portion if EAGAIN
101 * was encountered, but some portion was successfully written.
103 * Returns :wait_writable if EAGAIN is encountered and nothing
106 static VALUE
kgio_trywrite(VALUE io
, VALUE str
)
108 return my_write(io
, str
, 0);
111 #ifdef USE_MSG_DONTWAIT
113 * This method behaves like Kgio::PipeMethods#kgio_write, except
114 * it will use send(2) with the MSG_DONTWAIT flag on sockets to
115 * avoid unnecessary calls to fcntl(2).
117 static VALUE
my_send(VALUE io
, VALUE str
, int io_wait
)
122 prepare_write(&a
, io
, str
);
124 n
= (long)send(a
.fd
, a
.ptr
, a
.len
, MSG_DONTWAIT
);
125 if (write_check(&a
, n
, "send", io_wait
) != 0)
131 * This method may be optimized on some systems (e.g. GNU/Linux) to use
132 * MSG_DONTWAIT to avoid explicitly setting the O_NONBLOCK flag via fcntl.
133 * Otherwise this is the same as Kgio::PipeMethods#kgio_write
135 static VALUE
kgio_send(VALUE io
, VALUE str
)
137 return my_send(io
, str
, 1);
141 * This method may be optimized on some systems (e.g. GNU/Linux) to use
142 * MSG_DONTWAIT to avoid explicitly setting the O_NONBLOCK flag via fcntl.
143 * Otherwise this is the same as Kgio::PipeMethods#kgio_trywrite
145 static VALUE
kgio_trysend(VALUE io
, VALUE str
)
147 return my_send(io
, str
, 0);
149 #else /* ! USE_MSG_DONTWAIT */
150 # define kgio_send kgio_write
151 # define kgio_trysend kgio_trywrite
152 #endif /* ! USE_MSG_DONTWAIT */
154 #if defined(KGIO_WITHOUT_GVL)
155 # include "blocking_io_region.h"
156 #ifdef MSG_DONTWAIT /* Linux only */
157 # define MY_MSG_DONTWAIT (MSG_DONTWAIT)
159 # define MY_MSG_DONTWAIT (0)
162 static VALUE
nogvl_send(void *ptr
)
164 struct wr_args
*a
= ptr
;
166 return (VALUE
)send(a
->fd
, a
->ptr
, a
->len
, a
->flags
);
171 * io.kgio_syssend(str, flags) -> nil, String or :wait_writable
173 * Returns nil if the write was completed in full.
175 * Returns a String containing the unwritten portion if EAGAIN
176 * was encountered, but some portion was successfully written.
178 * Returns :wait_writable if EAGAIN is encountered and nothing
181 * This method is only available on Ruby 1.9.3 or later.
183 static VALUE
kgio_syssend(VALUE io
, VALUE str
, VALUE flags
)
188 a
.flags
= NUM2INT(flags
);
189 prepare_write(&a
, io
, str
);
190 if (a
.flags
& MY_MSG_DONTWAIT
) {
192 n
= (long)send(a
.fd
, a
.ptr
, a
.len
, a
.flags
);
193 } while (write_check(&a
, n
, "send", 0) != 0);
196 n
= (long)rb_thread_io_blocking_region(
197 nogvl_send
, &a
, a
.fd
);
198 } while (write_check(&a
, n
, "send", 0) != 0);
202 #endif /* HAVE_RB_THREAD_IO_BLOCKING_REGION */
207 * Kgio.trywrite(io, str) -> nil, String or :wait_writable
209 * Returns nil if the write was completed in full.
211 * Returns a String containing the unwritten portion if EAGAIN
212 * was encountered, but some portion was successfully written.
214 * Returns :wait_writable if EAGAIN is encountered and nothing
217 * Maybe used in place of PipeMethods#kgio_trywrite for non-Kgio objects
219 static VALUE
s_trywrite(VALUE mod
, VALUE io
, VALUE str
)
221 return my_write(io
, str
, 0);
224 void init_kgio_write(void)
226 VALUE mPipeMethods
, mSocketMethods
;
227 VALUE mKgio
= rb_define_module("Kgio");
229 sym_wait_writable
= ID2SYM(rb_intern("wait_writable"));
231 rb_define_singleton_method(mKgio
, "trywrite", s_trywrite
, 2);
234 * Document-module: Kgio::PipeMethods
236 * This module may be used used to create classes that respond to
237 * various Kgio methods for reading and writing. This is included
238 * in Kgio::Pipe by default.
240 mPipeMethods
= rb_define_module_under(mKgio
, "PipeMethods");
241 rb_define_method(mPipeMethods
, "kgio_write", kgio_write
, 1);
242 rb_define_method(mPipeMethods
, "kgio_trywrite", kgio_trywrite
, 1);
245 * Document-module: Kgio::SocketMethods
247 * This method behaves like Kgio::PipeMethods, but contains
248 * optimizations for sockets on certain operating systems
251 mSocketMethods
= rb_define_module_under(mKgio
, "SocketMethods");
252 rb_define_method(mSocketMethods
, "kgio_write", kgio_send
, 1);
253 rb_define_method(mSocketMethods
, "kgio_trywrite", kgio_trysend
, 1);
255 #if defined(KGIO_WITHOUT_GVL)
256 rb_define_method(mSocketMethods
, "kgio_syssend", kgio_syssend
, 2);