Skip to content

Commit

Permalink
add Event synchronization primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Oct 13, 2018
1 parent e11b0b0 commit ee0c194
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions base/locks.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

import .Base: _uv_hook_close, unsafe_convert,
lock, trylock, unlock, islocked
lock, trylock, unlock, islocked, wait, notify

export SpinLock, RecursiveSpinLock, Mutex
export SpinLock, RecursiveSpinLock, Mutex, Event


##########################################
Expand Down Expand Up @@ -238,3 +238,48 @@ end
function islocked(m::Mutex)
return m.ownertid != 0
end

mutable struct Event
lock::Mutex
q::Vector{Task}
set::Bool
# TODO: use a Condition with its paired lock
Event() = new(Mutex(), Task[], false)
end

function wait(e::Event)
e.set && return
lock(e.lock)
while !e.set
ct = current_task()
push!(e.q, ct)
unlock(e.lock)
try
wait()
catch
filter!(x->x!==ct, e.q)
rethrow()
end
lock(e.lock)
end
unlock(e.lock)
return nothing
end

function notify(e::Event)
lock(e.lock)
if !e.set
e.set = true
for t in e.q
schedule(t)
end
empty!(e.q)
end
unlock(e.lock)
return nothing
end

function reset(e::Event)
e.set = false
return nothing
end

0 comments on commit ee0c194

Please sign in to comment.