Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,16 @@ uv_write(s::LibuvStream, p::Vector{UInt8}) = GC.@preserve p uv_write(s, pointer(

# caller must have acquired the iolock
function uv_write(s::LibuvStream, p::Ptr{UInt8}, n::UInt)
nwritten = uv_try_write(s, p, n)
if nwritten == n
iolock_end()
return Int(n)
elseif nwritten > 0
p += nwritten
n -= UInt(nwritten)
elseif nwritten < 0 && nwritten != UV_EAGAIN && nwritten != UV_EINTR
uv_error("write", nwritten)
end
uvw = uv_write_async(s, p, n)
ct = current_task()
preserve_handle(ct)
Expand Down Expand Up @@ -1098,6 +1108,16 @@ function uv_write(s::LibuvStream, p::Ptr{UInt8}, n::UInt)
return Int(n)
end

function uv_try_write(s::LibuvStream, p::Ptr{UInt8}, n::UInt)
check_open(s)
nwrite = min(n, MAX_OS_WRITE) # split up the write into chunks the OS can handle.
nwritten = ccall(:jl_uv_try_write,
Int32,
(Ptr{Cvoid}, Ptr{Cvoid}, UInt),
s, p, nwrite)
return nwritten
end

# helper function for uv_write that returns the uv_write_t struct for the write
# rather than waiting on it, caller must hold the iolock
function uv_write_async(s::LibuvStream, p::Ptr{UInt8}, n::UInt)
Expand Down
13 changes: 13 additions & 0 deletions src/jl_uv.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,19 @@ JL_DLLEXPORT int jl_fs_close(uv_os_fd_t handle)
return ret;
}

JL_DLLEXPORT int jl_uv_try_write(uv_stream_t *stream, const char *data, size_t n)
{
uv_buf_t buf[1];
buf[0].base = (char*)data;
buf[0].len = n;
JL_UV_LOCK();
JL_SIGATOMIC_BEGIN();
int err = uv_try_write(stream, buf, 1);
JL_UV_UNLOCK();
JL_SIGATOMIC_END();
return err;
}

JL_DLLEXPORT int jl_uv_write(uv_stream_t *stream, const char *data, size_t n,
uv_write_t *uvw, uv_write_cb writecb)
{
Expand Down