/usr/share/julia/base/poll.jl is in julia-common 0.4.7-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | # This file is a part of Julia. License is MIT: http://julialang.org/license
# libuv file watching event flags
const UV_RENAME = 1
const UV_CHANGE = 2
immutable FileEvent
renamed::Bool
changed::Bool
timedout::Bool
end
FileEvent() = FileEvent(false, false, false)
FileEvent(flags::Integer) = FileEvent((flags & UV_RENAME) != 0,
(flags & UV_CHANGE) != 0,
(flags & FD_TIMEDOUT) != 0)
fetimeout() = FileEvent(false, false, true)
immutable FDEvent
readable::Bool
writable::Bool
timedout::Bool
end
# libuv file descriptor event flags
const UV_READABLE = 1
const UV_WRITABLE = 2
const FD_TIMEDOUT = 4
isreadable(f::FDEvent) = f.readable
iswritable(f::FDEvent) = f.writable
FDEvent() = FDEvent(false, false, false)
FDEvent(flags::Integer) = FDEvent((flags & UV_READABLE) != 0,
(flags & UV_WRITABLE) != 0,
(flags & FD_TIMEDOUT) != 0)
fdtimeout() = FDEvent(false, false, true)
type FileMonitor
handle::Ptr{Void}
file::ByteString
notify::Condition
active::Bool
function FileMonitor(file::AbstractString)
handle = Libc.malloc(_sizeof_uv_fs_event)
this = new(handle, file, Condition(), false)
associate_julia_struct(handle, this)
err = ccall(:uv_fs_event_init, Cint, (Ptr{Void}, Ptr{Void}), eventloop(), handle)
if err != 0
Libc.free(handle)
throw(UVError("PollingFileWatcher", err))
end
finalizer(this, uvfinalize)
this
end
end
abstract UVPollingWatcher
type PollingFileWatcher <: UVPollingWatcher
handle::Ptr{Void}
file::ByteString
interval::UInt32
notify::Condition
active::Bool
function PollingFileWatcher(file::AbstractString, interval::Float64=5.007) # same default as nodejs
handle = Libc.malloc(_sizeof_uv_fs_poll)
this = new(handle, file, round(UInt32, interval*1000), Condition(), false)
associate_julia_struct(handle, this)
err = ccall(:uv_fs_poll_init, Int32, (Ptr{Void}, Ptr{Void}), eventloop(), handle)
if err != 0
Libc.free(handle)
throw(UVError("PollingFileWatcher", err))
end
finalizer(this, uvfinalize)
return this
end
end
type _FDWatcher
handle::Ptr{Void}
refcount::Tuple{Int, Int}
notify::Condition
active::Tuple{Bool, Bool}
let FDWatchers = Vector{Any}()
global _FDWatcher
function _FDWatcher(fd::RawFD, readable::Bool, writable::Bool)
if !readable && !writable
throw(ArgumentError("must specify at least one of readable or writable to create a FDWatcher"))
end
if fd.fd+1 > length(FDWatchers)
old_len = length(FDWatchers)
resize!(FDWatchers, fd.fd+1)
fill!(sub(FDWatchers, old_len+1:fd.fd+1), nothing)
elseif FDWatchers[fd.fd + 1] !== nothing
this = FDWatchers[fd.fd + 1]::_FDWatcher
this.refcount = (this.refcount[1] + Int(readable), this.refcount[2] + Int(writable))
return this
end
@unix_only if ccall(:jl_uv_unix_fd_is_watched, Int32, (Int32, Ptr{Void}, Ptr{Void}), fd.fd, C_NULL, eventloop()) == 1
throw(ArgumentError("file descriptor $(fd.fd) is already being watched by libuv"))
end
handle = Libc.malloc(_sizeof_uv_poll)
this = new(handle, (Int(readable), Int(writable)), Condition(), (false, false))
associate_julia_struct(handle, this)
err = ccall(:uv_poll_init, Int32, (Ptr{Void}, Ptr{Void}, Int32), eventloop(), handle, fd.fd)
if err != 0
Libc.free(handle)
throw(UVError("FDWatcher",err))
end
FDWatchers[fd.fd] = this
return this
end
@windows_only function _FDWatcher(fd::WindowsRawSocket, readable::Bool, writable::Bool)
if !readable && !writable
throw(ArgumentError("must specify at least one of readable or writable to create a FDWatcher"))
end
handle = Libc.malloc(_sizeof_uv_poll)
this = new(handle, (Int(readable), Int(writable)), Condition(), (false, false))
associate_julia_struct(handle, this)
err = ccall(:uv_poll_init_socket,Int32,(Ptr{Void}, Ptr{Void}, Ptr{Void}),
eventloop(), handle, fd.handle)
if err != 0
Libc.free(handle)
throw(UVError("FDWatcher",err))
end
return this
end
global close
function close(t::_FDWatcher, readable::Bool, writable::Bool)
if t.handle != C_NULL
t.refcount = (t.refcount[1] - Int(readable), t.refcount[2] - Int(writable))
if t.refcount == (0, 0)
t.active = (false, false)
disassociate_julia_struct(t)
ccall(:jl_close_uv, Void, (Ptr{Void},), t.handle)
t.handle = C_NULL
fdnum = findfirst(FDWatchers, t)
if fdnum > 0
FDWatchers[fdnum] = nothing
end
notify(t.notify, FDEvent(true, true, false))
nothing
end
end
nothing
end
global _uv_hook_close
function _uv_hook_close(t::_FDWatcher)
# jl_atexit_hook can cause this to get called
t.refcount = (0, 0)
close(t, false, false)
end
end
end
type FDWatcher <: UVPollingWatcher
watcher::_FDWatcher
readable::Bool
writable::Bool
function FDWatcher(fd::RawFD, readable::Bool, writable::Bool)
this = new(_FDWatcher(fd, readable, writable), readable, writable)
finalizer(this, close)
return this
end
@windows_only function FDWatcher(fd::WindowsRawSocket, readable::Bool, writable::Bool)
this = new(_FDWatcher(fd, readable, writable), readable, writable)
finalizer(this, close)
return this
end
end
function close(t::FDWatcher)
r, w = t.readable, t.writable
t.readable = t.writable = false
close(t.watcher, r, w)
end
function uvfinalize(uv::Union{FileMonitor, PollingFileWatcher})
disassociate_julia_struct(uv)
close(uv)
end
function close(t::Union{FileMonitor, PollingFileWatcher})
if t.handle != C_NULL
ccall(:jl_close_uv, Void, (Ptr{Void},), t.handle)
end
end
function _uv_hook_close(uv::PollingFileWatcher)
uv.handle = C_NULL
uv.active = false
notify(uv.notify, (StatStruct(), StatStruct()))
nothing
end
function _uv_hook_close(uv::FileMonitor)
uv.handle = C_NULL
uv.active = false
notify(uv.notify, ("", FileEvent()))
nothing
end
function uv_fseventscb(handle::Ptr{Void}, filename::Ptr, events::Int32, status::Int32)
t = @handle_as handle FileMonitor
fname = filename == C_NULL ? "" : bytestring(convert(Ptr{UInt8}, filename))
if status != 0
notify_error(t.notify, UVError("FileMonitor", status))
else
notify(t.notify, (fname, FileEvent(events)))
end
nothing
end
function uv_pollcb(handle::Ptr{Void}, status::Int32, events::Int32)
t = @handle_as handle _FDWatcher
if status != 0
notify_error(t.notify, UVError("FDWatcher", status))
else
notify(t.notify, FDEvent(events))
end
nothing
end
function uv_fspollcb(handle::Ptr{Void}, status::Int32, prev::Ptr, curr::Ptr)
t = @handle_as handle PollingFileWatcher
if status != 0
notify_error(t.notify, UVError("PollingFileWatcher", status))
else
prev_stat = StatStruct(convert(Ptr{UInt8}, prev))
curr_stat = StatStruct(convert(Ptr{UInt8}, curr))
notify(t.notify, (prev_stat, curr_stat))
end
nothing
end
function start_watching(t::_FDWatcher)
read, write = t.refcount[1] > 0, t.refcount[2] > 0
if t.active[1] != read || t.active[2] != write
uv_error("start_watching (FD)",
ccall(:uv_poll_start, Int32, (Ptr{Void}, Int32, Ptr{Void}),
t.handle,
(read ? UV_READABLE : 0) | (write ? UV_WRITABLE : 0),
uv_jl_pollcb::Ptr{Void}))
t.active = (read, write)
end
end
function stop_watching(t::_FDWatcher)
if t.active[1] || t.active[2]
if isempty(t.notify.waitq)
t.active = (false, false)
uv_error("start_watching (FD)",
ccall(:uv_poll_stop, Int32, (Ptr{Void},), t.handle))
else
start_watching(t) # make sure the READABLE / WRITEABLE state is updated
end
end
end
function start_watching(t::PollingFileWatcher)
if !t.active
uv_error("start_watching (File)",
ccall(:uv_fs_poll_start, Int32, (Ptr{Void}, Ptr{Void}, Cstring, UInt32),
t.handle, uv_jl_fspollcb::Ptr{Void}, t.file, t.interval))
t.active = true
end
end
function stop_watching(t::PollingFileWatcher)
if t.active && isempty(t.notify.waitq)
t.active = false
uv_error("stop_watching (File)",
ccall(:uv_fs_poll_stop, Int32, (Ptr{Void},), t.handle))
end
end
function start_watching(t::FileMonitor)
if !t.active
uv_error("start_watching (FileMonitor)",
ccall(:uv_fs_event_start, Int32, (Ptr{Void}, Ptr{Void}, Cstring, Int32),
t.handle, uv_jl_fseventscb::Ptr{Void}, t.file, 0))
t.active = true
end
end
function stop_watching(t::FileMonitor)
if t.active && isempty(t.notify.waitq)
t.active = false
uv_error("stop_watching (FileMonitor)",
ccall(:uv_fs_event_stop, Int32, (Ptr{Void},), t.handle))
end
end
function wait(fdw::FDWatcher)
wait(fdw.watcher, readable = fdw.readable, writable = fdw.writable)
end
function wait(fdw::_FDWatcher; readable=true, writable=true)
local events
while fdw.refcount != (0, 0)
start_watching(fdw)
events = wait(fdw.notify)
if isa(events, FDEvent) &&
((readable && isreadable(events)) || (writable && iswritable(events)))
break
end
end
stop_watching(fdw)
events
end
function wait(fd::RawFD; readable=false, writable=false)
fdw = _FDWatcher(fd, readable, writable)
try
return wait(fdw, readable=readable, writable=writable)
finally
close(fdw, readable, writable)
end
end
@windows_only begin
function wait(socket::WindowsRawSocket; readable=false, writable=false)
fdw = _FDWatcher(socket, readable, writable)
try
return wait(fdw, readable=readable, writable=writable)
finally
close(fdw, readable, writable)
end
end
end
function wait(pfw::PollingFileWatcher)
start_watching(pfw)
prevstat, currstat = stream_wait(pfw, pfw.notify)
stop_watching(pfw)
return prevstat, currstat
end
function wait(m::FileMonitor)
start_watching(m)
filename, events = stream_wait(m, m.notify)
stop_watching(m)
return filename, events
end
function poll_fd(s::Union{RawFD, @windows ? WindowsRawSocket : Union{}}, timeout_s::Real=-1; readable=false, writable=false)
wt = Condition()
fdw = _FDWatcher(s, readable, writable)
try
if timeout_s >= 0
result::FDEvent = fdtimeout()
@schedule (result = wait(fdw, readable=readable, writable=writable); notify(wt))
@schedule (sleep(timeout_s); notify(wt))
wait(wt)
return result
else
return wait(fdw, readable=readable, writable=writable)
end
finally
close(fdw, readable, writable)
end
end
function watch_file(s::AbstractString, timeout_s::Real=-1)
wt = Condition()
fm = FileMonitor(s)
try
if timeout_s >= 0
result = fetimeout()
@schedule (try result = wait(fm); catch e; notify_error(wt, e); end; notify(wt))
@schedule (sleep(timeout_s); notify(wt))
wait(wt)
return result
else
return wait(fm)
end
finally
close(fm)
end
end
function poll_file(s::AbstractString, interval_seconds::Real=5.007, timeout_s::Real=-1)
wt = Condition()
pfw = PollingFileWatcher(s, Float64(interval_seconds))
try
if timeout_s >= 0
result = :timeout
@schedule (try result = wait(pfw); catch e; notify_error(wt, e); end; notify(wt))
@schedule (sleep(timeout_s); notify(wt))
wait(wt)
if result === :timeout
return (StatStruct(), StatStruct())
end
return result
else
return wait(pfw)
end
finally
close(pfw)
end
end
|