Skip to content

Commit

Permalink
Miscellaneous task/thread related tweaks (JuliaLang#36927)
Browse files Browse the repository at this point in the history
- use ThreadSynchronizer type alias in ReentrantLock
- tweak the `timedwait` documentation; in particular, rename the `testcb`
  argument to `callback`
- clarify an "Special note for Threads.Condition" to not reference a
  variable `c` that was never defined
  • Loading branch information
fingolfin committed Aug 6, 2020
1 parent 6d356db commit afe562c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions base/asyncevent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ function Timer(cb::Function, timeout::Real; interval::Real=0.0)
end

"""
timedwait(testcb::Function, timeout::Real; pollint::Real=0.1)
timedwait(callback::Function, timeout::Real; pollint::Real=0.1)
Waits until `testcb` returns `true` or for `timeout` seconds, whichever is earlier.
`testcb` is polled every `pollint` seconds. The minimum duration for `timeout` and `pollint`
is 1 millisecond or `0.001`.
Waits until `callback` returns `true` or `timeout` seconds have passed, whichever is earlier.
`callback` is polled every `pollint` seconds. The minimum value for `timeout` and `pollint`
is `0.001`, that is, 1 millisecond.
Returns :ok or :timed_out
"""
Expand Down
12 changes: 6 additions & 6 deletions base/lock.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

const ThreadSynchronizer = GenericCondition{Threads.SpinLock}

# Advisory reentrant lock
"""
ReentrantLock()
Expand All @@ -10,10 +12,10 @@ Each [`lock`](@ref) must be matched with an [`unlock`](@ref).
"""
mutable struct ReentrantLock <: AbstractLock
locked_by::Union{Task, Nothing}
cond_wait::GenericCondition{Threads.SpinLock}
cond_wait::ThreadSynchronizer
reentrancy_cnt::Int

ReentrantLock() = new(nothing, GenericCondition{Threads.SpinLock}(), 0)
ReentrantLock() = new(nothing, ThreadSynchronizer(), 0)
end

assert_havelock(l::ReentrantLock) = assert_havelock(l, l.locked_by)
Expand Down Expand Up @@ -232,17 +234,15 @@ end
"""
Special note for [`Threads.Condition`](@ref):
The caller must be holding the [`lock`](@ref) that owns `c` before calling this method.
The caller must be holding the [`lock`](@ref) that owns a `Threads.Condition` before calling this method.
The calling task will be blocked until some other task wakes it,
usually by calling [`notify`](@ref) on the same Condition object.
usually by calling [`notify`](@ref) on the same `Threads.Condition` object.
The lock will be atomically released when blocking (even if it was locked recursively),
and will be reacquired before returning.
"""
wait(c::Condition)
end

const ThreadSynchronizer = GenericCondition{Threads.SpinLock}

"""
Semaphore(sem_size)
Expand Down

0 comments on commit afe562c

Please sign in to comment.