Skip to content

Commit

Permalink
Merge pull request #307 from JuliaGizmos/sp/threadsafety
Browse files Browse the repository at this point in the history
fix: use threadsafe conditions
  • Loading branch information
pfitzseb authored May 15, 2023
2 parents fd1ed94 + 5ab2bf5 commit 52cfbcf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/AtomShell/window.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ function Window(a::Shell, content::Page, opts::AbstractDict = Dict(); async=fals
return w
end

function initwindow!(w::Window, callback_cond::Condition)
initresult = wait(callback_cond)
function initwindow!(w::Window, callback_cond::Threads.Condition)
lock(callback_cond)
initresult = try
wait(callback_cond)
finally
unlock(callback_cond)
end
if isa(initresult, AbstractDict) && get(initresult, "type", "") == "error"
throw(JSError(
get(initresult, "name", "unknown"),
Expand Down
12 changes: 9 additions & 3 deletions src/rpc/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@ handle(f, o, t) = (handlers(o)[t] = f)

# Callback Tasks

const callbacks = Dict{Int,Condition}()
const callbacks = Dict{Int,Threads.Condition}()

const counter = Ref(0)

function callback!()
id = (counter[] += 1)
cb = Condition()
cb = Threads.Condition()
callbacks[id] = cb
return id, cb
end

function callback!(id, value = nothing)
haskey(callbacks, id) || return
notify(callbacks[id], value)
c = callbacks[id]
lock(c)
try
notify(callbacks[id], value)
finally
unlock(c)
end
delete!(callbacks, id)
return
end
Expand Down
7 changes: 6 additions & 1 deletion src/rpc/rpc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ function js(o, js::JSString; callback = true)
msg(o, cmd)

if callback
val = wait(cond)
lock(cond)
val = try
wait(cond)
finally
unlock(cond)
end
if isa(val, AbstractDict) && get(val, "type", "") == "error"
err = JSError(get(val, "name", "unknown"), get(val, "message", "blank"))
throw(err)
Expand Down

0 comments on commit 52cfbcf

Please sign in to comment.