From 5ab2bf530dec8d8c0a6a20ae8e5675c0f82f799d Mon Sep 17 00:00:00 2001 From: Sebastian Pfitzner Date: Mon, 15 May 2023 13:27:04 +0200 Subject: [PATCH] fix: use threadsafe conditions --- src/AtomShell/window.jl | 9 +++++++-- src/rpc/callbacks.jl | 12 +++++++++--- src/rpc/rpc.jl | 7 ++++++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/AtomShell/window.jl b/src/AtomShell/window.jl index 299198d..113a6a7 100644 --- a/src/AtomShell/window.jl +++ b/src/AtomShell/window.jl @@ -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"), diff --git a/src/rpc/callbacks.jl b/src/rpc/callbacks.jl index bd562c5..f40db18 100644 --- a/src/rpc/callbacks.jl +++ b/src/rpc/callbacks.jl @@ -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 diff --git a/src/rpc/rpc.jl b/src/rpc/rpc.jl index 5eeea96..2fc3b47 100644 --- a/src/rpc/rpc.jl +++ b/src/rpc/rpc.jl @@ -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)