Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use thread-safe conditions for connection pool #512

Merged
merged 2 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()