From c1d4e378458f44490fb72ecef1b262d309dddcda Mon Sep 17 00:00:00 2001 From: Tommy-Xavier Robillard <42289375+TommyXR@users.noreply.github.com> Date: Tue, 6 Apr 2021 17:39:38 -0400 Subject: [PATCH] Add `lock` wrapper for Channels (#39312) Added lock(f, c::Channel) utility function --- NEWS.md | 2 ++ base/channels.jl | 1 + base/condition.jl | 1 - base/weakkeydict.jl | 2 ++ 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 4c2f4beae153e0..140da486c30bf8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -36,6 +36,8 @@ New library functions * Two argument methods `findmax(f, domain)`, `argmax(f, domain)` and the corresponding `min` versions ([#27613]). * `isunordered(x)` returns true if `x` is value that is normally unordered, such as `NaN` or `missing`. * New macro `Base.@invokelatest f(args...; kwargs...)` provides a convenient way to call `Base.invokelatest(f, args...; kwargs...)` ([#37971]) +* New macro `Base.@invoke f(arg1::T1, arg2::T2; kwargs...)` provides an easier syntax to call `invoke(f, Tuple{T1,T2}; kwargs...)` ([#38438]) +* Two arguments method `lock(f, lck)` now accepts a `Channel` as the second argument. ([#39312]) * New functor `Returns(value)`, which returns `value` for any arguments ([#39794]) * New macro `Base.@invoke f(arg1::T1, arg2::T2; kwargs...)` provides an easier syntax to call `invoke(f, Tuple{T1,T2}, arg1, arg2; kwargs...)` ([#38438]) diff --git a/base/channels.jl b/base/channels.jl index fc6e1381b64b51..3b171ede0699e6 100644 --- a/base/channels.jl +++ b/base/channels.jl @@ -422,6 +422,7 @@ n_avail(c::Channel) = isbuffered(c) ? length(c.data) : length(c.cond_put.waitq) isempty(c::Channel) = isbuffered(c) ? isempty(c.data) : isempty(c.cond_put.waitq) lock(c::Channel) = lock(c.cond_take) +lock(f, c::Channel) = lock(f, c.cond_take) unlock(c::Channel) = unlock(c.cond_take) trylock(c::Channel) = trylock(c.cond_take) diff --git a/base/condition.jl b/base/condition.jl index 4b9f57e47ab290..cf0bca8d9dc463 100644 --- a/base/condition.jl +++ b/base/condition.jl @@ -76,7 +76,6 @@ trylock(c::GenericCondition) = trylock(c.lock) islocked(c::GenericCondition) = islocked(c.lock) lock(f, c::GenericCondition) = lock(f, c.lock) -unlock(f, c::GenericCondition) = unlock(f, c.lock) # have waiter wait for c function _wait2(c::GenericCondition, waiter::Task) diff --git a/base/weakkeydict.jl b/base/weakkeydict.jl index c7ec172af46aa8..8e1a3d480b9951 100644 --- a/base/weakkeydict.jl +++ b/base/weakkeydict.jl @@ -84,6 +84,8 @@ empty(d::WeakKeyDict, ::Type{K}, ::Type{V}) where {K, V} = WeakKeyDict{K, V}() IteratorSize(::Type{<:WeakKeyDict}) = SizeUnknown() islocked(wkh::WeakKeyDict) = islocked(wkh.lock) +lock(wkh::WeakKeyDict) = lock(wkh.lock) +unlock(wkh::WeakKeyDict) = unlock(wkh.lock) lock(f, wkh::WeakKeyDict) = lock(f, wkh.lock) trylock(f, wkh::WeakKeyDict) = trylock(f, wkh.lock)