Skip to content

Commit

Permalink
Do not migrate tasks to a different thread pool (#32)
Browse files Browse the repository at this point in the history
Change the try_with_timeout method so that it does not migrate
a task to a different thread pool.
  • Loading branch information
samtkaplan authored Mar 12, 2024
1 parent 89c13cc commit f6bdb95
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ConcurrentUtilities"
uuid = "f0e56b4a-5159-44fe-b623-3e5288b988bb"
authors = ["Jacob Quinn <quinn.jacobd@gmail.com>"]
version = "2.3.1"
version = "2.4.0"

[deps]
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Expand Down
8 changes: 8 additions & 0 deletions src/ConcurrentUtilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ export Lockable, OrderedSynchronizer, reset!, ReadWriteLock, readlock, readunloc
Workers, remote_eval, remote_fetch, Worker, terminate!, WorkerTerminatedException,
Pool, acquire, release, drain!, try_with_timeout, TimeoutException

macro samethreadpool_spawn(expr)
if isdefined(Base.Threads, :threadpool)
esc(:(Threads.@spawn Threads.threadpool() $expr))
else
esc(:(Threads.@spawn $expr))
end
end

include("try_with_timeout.jl")
include("workers.jl")
using .Workers
Expand Down
2 changes: 1 addition & 1 deletion src/try_with_timeout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function try_with_timeout(f, timeout, ::Type{T}=Any) where {T}
ch = Channel{T}(0)
x = TimedOut(ch)
timer = Timer(tm -> !isready(ch) && close(ch, TimeoutException(timeout)), timeout)
Threads.@spawn begin
@samethreadpool_spawn begin
try
put!(ch, $f(x)::T)
catch e
Expand Down
7 changes: 7 additions & 0 deletions test/try_with_timeout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ using ConcurrentUtilities, Test
@test e isa CapturedException
rethrow(e.ex)
end

# try_with_timeout should not migrate the task to a different thread pool
if isdefined(Base.Threads, :threadpool)
@test try_with_timeout(_ -> Threads.threadpool(), 1) == Threads.threadpool()
@test read(`julia -t 1,1 -E 'using ConcurrentUtilities; try_with_timeout(_ -> Threads.threadpool(), 1)'`, String) == ":interactive\n"
@test read(`julia -t 1,1 -E 'using ConcurrentUtilities; fetch(Threads.@spawn begin try_with_timeout(_ -> Threads.threadpool(), 1) end)'`, String) == ":default\n"
end
end

2 comments on commit f6bdb95

@quinnj
Copy link
Member

@quinnj quinnj commented on f6bdb95 Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/102713

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.4.0 -m "<description of version>" f6bdb9566baa067043cf5c8313da08bf2c47081d
git push origin v2.4.0

Please sign in to comment.