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

@lock export from Base, closes #36441 #39588

Merged
merged 8 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,8 @@ export
istaskstarted,
istaskfailed,
lock,
@lock,
@lock_nofail,
fredrikekre marked this conversation as resolved.
Show resolved Hide resolved
notify,
ReentrantLock,
schedule,
Expand Down
15 changes: 15 additions & 0 deletions base/lock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ function trylock(f, l::AbstractLock)
return false
end

"""
@lock l expr

Macro version for `lock(f, l::AbstractLock)`. It wraps an arbitrary `expr` with a pair of
`lock(l)` and `unlock(l)` functions. It is often more performant than function form
`lock(f, l)`, because it don't capture variables into a lambda.
See [`lock`](@ref).
"""
macro lock(l, expr)
quote
temp = $(esc(l))
Expand All @@ -213,6 +221,13 @@ macro lock(l, expr)
end
end

"""
@lock_nofail l expr

Equivalent to `@lock l expr` for cases in which we can guarantee that the function
will not throw any error. In this case, avoiding try-catch can improve the performance.
See [`@lock`](@ref).
"""
macro lock_nofail(l, expr)
quote
temp = $(esc(l))
Expand Down