Skip to content

Commit

Permalink
Merge pull request #512 from mosullivan93/threadsafety
Browse files Browse the repository at this point in the history
Use thread-safe conditions for connection pool
  • Loading branch information
shashi committed Jun 5, 2023
2 parents 37f6f8b + 7697a72 commit 832e2b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
20 changes: 16 additions & 4 deletions src/connection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ are automatically removed from the pool.
struct ConnectionPool
outbox::Channel
connections::Set{AbstractConnection}
condition::Condition
condition::Threads.Condition
end

function ConnectionPool(
Expand All @@ -30,7 +30,7 @@ function ConnectionPool(
pool = ConnectionPool(
outbox,
connections,
Condition(),
Threads.Condition(),
)

# Catch errors here, otherwise they are lost to the void.
Expand Down Expand Up @@ -68,12 +68,24 @@ current task until that is the case. Also processes incoming connections.
"""
function ensure_connection(pool::ConnectionPool)
if isempty(pool.connections)
wait(pool.condition)
lock(pool.condition)
try
wait(pool.condition)
finally
unlock(pool.condition)
end
end
end

Base.wait(pool::ConnectionPool) = ensure_connection(pool)
Base.notify(pool::ConnectionPool) = notify(pool.condition)
function Base.notify(pool::ConnectionPool)
lock(pool.condition)
try
notify(pool.condition)
finally
unlock(pool.condition)
end
end

"""
process_messages(pool)
Expand Down
6 changes: 0 additions & 6 deletions test/blink-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ using Blink
using Observables
using Test

notinstalled = !AtomShell.isinstalled()

notinstalled && AtomShell.install()

"""
Execute function f() with a timeout of `timeout` seconds. Returns the
result of f() or `nothing` in the case of a timeout.
Expand Down Expand Up @@ -146,5 +142,3 @@ w = open_window()
body!(w, ExampleRenderableType())
@test example_renderable_was_rendered
end

notinstalled && AtomShell.uninstall()

0 comments on commit 832e2b8

Please sign in to comment.