Skip to content

Commit

Permalink
make Libuv types RAII with better error handling and bump libuv versi…
Browse files Browse the repository at this point in the history
…on for bugfixes
  • Loading branch information
vtjnash committed Aug 5, 2016
1 parent 043eb26 commit 2fdc182
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 141 deletions.
9 changes: 3 additions & 6 deletions base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -403,22 +403,19 @@ end

function setup_stdio(stdio::PipeEndpoint, readable::Bool)
closeafter = false
if stdio.handle == C_NULL
io = Libc.malloc(_sizeof_uv_named_pipe)
if stdio.status == StatusUninit
if readable
link_pipe(io, false, stdio, true)
else
link_pipe(stdio, true, io, false)
end
closeafter = true
else
io = stdio.handle
end
return (io, closeafter)
return (stdio.handle, closeafter)
end

function setup_stdio(stdio::Pipe, readable::Bool)
if stdio.in.handle == C_NULL && stdio.out.handle == C_NULL
if stdio.in.status == StatusUninit && stdio.out.status == StatusUninit
link_pipe(stdio)
end
io = readable ? stdio.out : stdio.in
Expand Down
115 changes: 53 additions & 62 deletions base/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,33 +255,30 @@ type TCPSocket <: LibuvStream
lock::ReentrantLock
throttle::Int

TCPSocket(handle) = new(
handle,
StatusUninit,
true,
PipeBuffer(),
false, Condition(),
false, Condition(),
false, Condition(),
nothing,
ReentrantLock(),
DEFAULT_READ_BUFFER_SZ
)
function TCPSocket(handle::Ptr{Void}, status)
tcp = new(
handle,
status,
true,
PipeBuffer(),
false, Condition(),
false, Condition(),
false, Condition(),
nothing,
ReentrantLock(),
DEFAULT_READ_BUFFER_SZ)
associate_julia_struct(tcp.handle, tcp)
finalizer(tcp, uvfinalize)
return tcp
end
end
function TCPSocket()
this = TCPSocket(Libc.malloc(_sizeof_uv_tcp))
associate_julia_struct(this.handle,this)
finalizer(this,uvfinalize)
err = ccall(:uv_tcp_init,Cint,(Ptr{Void},Ptr{Void}),
eventloop(),this.handle)
if err != 0
#TODO: this codepath is not currently tested
Libc.free(this.handle)
this.handle = C_NULL
throw(UVError("failed to create tcp socket",err))
end
this.status = StatusInit
return this
tcp = TCPSocket(Libc.malloc(_sizeof_uv_tcp), StatusUninit)
err = ccall(:uv_tcp_init, Cint, (Ptr{Void}, Ptr{Void}),
eventloop(), tcp.handle)
uv_error("failed to create tcp socket", err)
tcp.status = StatusInit
return tcp
end

type TCPServer <: LibuvServer
Expand All @@ -292,27 +289,24 @@ type TCPServer <: LibuvServer
closecb::Callback
closenotify::Condition

TCPServer(handle) = new(
handle,
StatusUninit,
false, Condition(),
false, Condition()
)
function TCPServer(handle::Ptr{Void}, status)
tcp = new(
handle,
status,
false, Condition(),
false, Condition())
associate_julia_struct(tcp.handle, tcp)
finalizer(tcp, uvfinalize)
return tcp
end
end
function TCPServer()
this = TCPServer(Libc.malloc(_sizeof_uv_tcp))
associate_julia_struct(this.handle, this)
finalizer(this,uvfinalize)
err = ccall(:uv_tcp_init,Cint,(Ptr{Void},Ptr{Void}),
eventloop(),this.handle)
if err != 0
#TODO: this codepath is not currently tested
Libc.free(this.handle)
this.handle = C_NULL
throw(UVError("failed to create tcp server",err))
end
this.status = StatusInit
return this
tcp = TCPServer(Libc.malloc(_sizeof_uv_tcp), StatusUninit)
err = ccall(:uv_tcp_init, Cint, (Ptr{Void}, Ptr{Void}),
eventloop(), tcp.handle)
uv_error("failed to create tcp server", err)
tcp.status = StatusInit
return tcp
end

isreadable(io::TCPSocket) = isopen(io) || nb_available(io) > 0
Expand Down Expand Up @@ -344,26 +338,23 @@ type UDPSocket <: LibuvStream
sendnotify::Condition
closenotify::Condition

UDPSocket(handle::Ptr) = new(
handle,
StatusUninit,
Condition(),
Condition(),
Condition()
)
function UDPSocket(handle::Ptr{Void}, status)
udp = new(
handle,
status,
Condition(),
Condition(),
Condition())
associate_julia_struct(udp.handle, udp)
finalizer(udp, uvfinalize)
return udp
end
end
function UDPSocket()
this = UDPSocket(Libc.malloc(_sizeof_uv_udp))
associate_julia_struct(this.handle, this)
err = ccall(:uv_udp_init,Cint,(Ptr{Void},Ptr{Void}),
eventloop(),this.handle)
finalizer(this, uvfinalize)
if err != 0
#TODO: this codepath is not currently tested
Libc.free(this.handle)
this.handle = C_NULL
throw(UVError("failed to create udp socket",err))
end
this = UDPSocket(Libc.malloc(_sizeof_uv_udp), StatusUninit)
err = ccall(:uv_udp_init, Cint, (Ptr{Void}, Ptr{Void}),
eventloop(), this.handle)
uv_error("failed to create udp socket", err)
this.status = StatusInit
return this
end
Expand Down
Loading

0 comments on commit 2fdc182

Please sign in to comment.