Skip to content

Commit

Permalink
Return :timeout instead of throwing an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
kpamnany committed Jan 10, 2025
1 parent d76a21b commit 0d2c642
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
6 changes: 1 addition & 5 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export
InterruptException, InexactError, OutOfMemoryError, ReadOnlyMemoryError,
OverflowError, StackOverflowError, SegmentationFault, UndefRefError, UndefVarError,
TypeError, ArgumentError, MethodError, AssertionError, LoadError, InitError,
UndefKeywordError, ConcurrencyViolationError, FieldError, TimeoutError,
UndefKeywordError, ConcurrencyViolationError, FieldError,
# AST representation
Expr, QuoteNode, LineNumberNode, GlobalRef,
# object model functions
Expand Down Expand Up @@ -473,10 +473,6 @@ end

struct PrecompilableError <: Exception end

struct TimeoutError <: Exception
timeout::Real
end

String(s::String) = s # no constructor yet

const Cvoid = Nothing
Expand Down
8 changes: 4 additions & 4 deletions base/condition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ Wait for [`notify`](@ref) on `c` and return the `val` parameter passed to `notif
If the keyword `first` is set to `true`, the waiter will be put _first_
in line to wake up on `notify`. Otherwise, `wait` has first-in-first-out (FIFO) behavior.
If `timeout` is specified, cancel the `wait` when it expires and throw a
`TimeoutError` to the waiting task. The minimum value for `timeout`
is 0.001 seconds, i.e. 1 millisecond.
If `timeout` is specified, cancel the `wait` when it expires and return
`:timeout`. The minimum value for `timeout` is 0.001 seconds, i.e. 1
millisecond.
"""
function wait(c::GenericCondition; first::Bool=false, timeout::Real=0.0)
timeout == 0.0 || timeout 1e-3 || throw(ArgumentError("timeout must be ≥ 1 millisecond"))
Expand Down Expand Up @@ -167,7 +167,7 @@ function wait(c::GenericCondition; first::Bool=false, timeout::Real=0.0)
end
unlock(c.lock)
# send the waiting task a timeout
dosched && schedule(ct, TimeoutError(timeout); error=true)
dosched && schedule(ct, :timeout)
end
t.sticky = false
Threads._spawn_set_thrpool(t, :interactive)
Expand Down
14 changes: 7 additions & 7 deletions test/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ end
end

@testset "timed wait on Condition" begin
a = Condition()
@test_throws ArgumentError wait(a; timeout=0.0005)
@test_throws TimeoutError wait(a; timeout=0.1)
a = Threads.Condition()
@test_throws ArgumentError @lock a wait(a; timeout=0.0005)
@test @lock a wait(a; timeout=0.1)==:timeout
lock(a)
@spawn begin
sleep(0.01)
notify(a)
@lock a notify(a)
end
@test try
wait(a; timeout=2)
true
catch
false
finally
unlock(a)
end
end

Expand Down

0 comments on commit 0d2c642

Please sign in to comment.