-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
libuv.jl
148 lines (120 loc) · 4.79 KB
/
libuv.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# This file is a part of Julia. License is MIT: https://julialang.org/license
# Core definitions for interacting with the libuv library from Julia
include(string(length(Core.ARGS) >= 2 ? Core.ARGS[2] : "", "uv_constants.jl")) # include($BUILDROOT/base/uv_constants.jl)
# convert UV handle data to julia object, checking for null
function uv_sizeof_handle(handle)
if !(UV_UNKNOWN_HANDLE < handle < UV_HANDLE_TYPE_MAX)
throw(DomainError(handle))
end
return ccall(:uv_handle_size, Csize_t, (Int32,), handle)
end
function uv_sizeof_req(req)
if !(UV_UNKNOWN_REQ < req < UV_REQ_TYPE_MAX)
throw(DomainError(req))
end
return ccall(:uv_req_size, Csize_t, (Int32,), req)
end
for h in uv_handle_types
@eval const $(Symbol("_sizeof_", lowercase(string(h)))) = uv_sizeof_handle($h)
end
for r in uv_req_types
@eval const $(Symbol("_sizeof_", lowercase(string(r)))) = uv_sizeof_req($r)
end
uv_handle_data(handle) = ccall(:jl_uv_handle_data, Ptr{Cvoid}, (Ptr{Cvoid},), handle)
uv_req_data(handle) = ccall(:jl_uv_req_data, Ptr{Cvoid}, (Ptr{Cvoid},), handle)
uv_req_set_data(req, data) = ccall(:jl_uv_req_set_data, Cvoid, (Ptr{Cvoid}, Any), req, data)
uv_req_set_data(req, data::Ptr{Cvoid}) = ccall(:jl_uv_req_set_data, Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), req, data)
macro handle_as(hand, typ)
return quote
local data = uv_handle_data($(esc(hand)))
data == C_NULL && return
unsafe_pointer_to_objref(data)::($(esc(typ)))
end
end
associate_julia_struct(handle::Ptr{Cvoid}, @nospecialize(jlobj)) =
ccall(:jl_uv_associate_julia_struct, Cvoid, (Ptr{Cvoid}, Any), handle, jlobj)
disassociate_julia_struct(uv) = disassociate_julia_struct(uv.handle)
disassociate_julia_struct(handle::Ptr{Cvoid}) =
handle != C_NULL && ccall(:jl_uv_disassociate_julia_struct, Cvoid, (Ptr{Cvoid},), handle)
iolock_begin() = ccall(:jl_iolock_begin, Cvoid, ())
iolock_end() = ccall(:jl_iolock_end, Cvoid, ())
# A dict of all libuv handles that are being waited on somewhere in the system
# and should thus not be garbage collected
const uvhandles = IdDict()
const preserve_handle_lock = Threads.SpinLock()
function preserve_handle(x)
lock(preserve_handle_lock)
v = get(uvhandles, x, 0)::Int
uvhandles[x] = v + 1
unlock(preserve_handle_lock)
nothing
end
function unpreserve_handle(x)
lock(preserve_handle_lock)
v = uvhandles[x]::Int
if v == 1
pop!(uvhandles, x)
else
uvhandles[x] = v - 1
end
unlock(preserve_handle_lock)
nothing
end
## Libuv error handling ##
struct IOError <: Exception
msg::AbstractString
code::Int32
IOError(msg::AbstractString, code::Integer) = new(msg, code)
end
showerror(io::IO, e::IOError) = print(io, "IOError: ", e.msg)
function _UVError(pfx::AbstractString, code::Integer)
code = Int32(code)
IOError(string(pfx, ": ", struverror(code), " (", uverrorname(code), ")"), code)
end
function _UVError(pfx::AbstractString, code::Integer, sfxs::AbstractString...)
code = Int32(code)
IOError(string(pfx, ": ", struverror(code), " (", uverrorname(code), ")", " ", sfxs...), code)
end
struverror(err::Int32) = unsafe_string(ccall(:uv_strerror, Cstring, (Int32,), err))
uverrorname(err::Int32) = unsafe_string(ccall(:uv_err_name, Cstring, (Int32,), err))
uv_error(prefix::Symbol, c::Integer) = uv_error(string(prefix), c)
uv_error(prefix::AbstractString, c::Integer) = c < 0 ? throw(_UVError(prefix, c)) : nothing
## event loop ##
eventloop() = uv_eventloop::Ptr{Cvoid}
function process_events()
return ccall(:jl_process_events, Int32, (Ptr{Cvoid},), eventloop())
end
function uv_alloc_buf end
function uv_readcb end
function uv_writecb_task end
function uv_return_spawn end
function uv_asynccb end
function uv_timercb end
function reinit_stdio()
global uv_jl_alloc_buf = @cfunction(uv_alloc_buf, Cvoid, (Ptr{Cvoid}, Csize_t, Ptr{Cvoid}))
global uv_jl_readcb = @cfunction(uv_readcb, Cvoid, (Ptr{Cvoid}, Cssize_t, Ptr{Cvoid}))
global uv_jl_writecb_task = @cfunction(uv_writecb_task, Cvoid, (Ptr{Cvoid}, Cint))
global uv_jl_return_spawn = @cfunction(uv_return_spawn, Cvoid, (Ptr{Cvoid}, Int64, Int32))
global uv_jl_asynccb = @cfunction(uv_asynccb, Cvoid, (Ptr{Cvoid},))
global uv_jl_timercb = @cfunction(uv_timercb, Cvoid, (Ptr{Cvoid},))
global uv_eventloop = ccall(:jl_global_event_loop, Ptr{Cvoid}, ())
global stdin = init_stdio(ccall(:jl_stdin_stream, Ptr{Cvoid}, ()))
global stdout = init_stdio(ccall(:jl_stdout_stream, Ptr{Cvoid}, ()))
global stderr = init_stdio(ccall(:jl_stderr_stream, Ptr{Cvoid}, ()))
nothing
end
"""
stdin
Global variable referring to the standard input stream.
"""
:stdin
"""
stdout
Global variable referring to the standard out stream.
"""
:stdout
"""
stderr
Global variable referring to the standard error stream.
"""
:stderr