Skip to content

Commit

Permalink
@lock export from Base, closes JuliaLang#36441 (JuliaLang#39588)
Browse files Browse the repository at this point in the history
  • Loading branch information
sairus7 authored and ElOceanografo committed May 4, 2021
1 parent eec9b21 commit f34b3bc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Standard library changes
@test isequal(complex(one(T)) / complex(T(Inf), T(-Inf)), complex(zero(T), zero(T))) broken=(T == Float64)
```
([#39322])
* `@lock` is now exported from Base ([#39588]).

#### Package Manager

Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ export
istaskstarted,
istaskfailed,
lock,
@lock,
notify,
ReentrantLock,
schedule,
Expand Down
23 changes: 23 additions & 0 deletions base/lock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,22 @@ function trylock(f, l::AbstractLock)
return false
end

"""
@lock l expr
Macro version of `lock(f, l::AbstractLock)` but with `expr` instead of `f` function.
Expands to:
```julia
lock(l)
try
expr
finally
unlock(l)
end
```
This is similar to using [`lock`](@ref) with a `do` block, but avoids creating a closure
and thus can improve the performance.
"""
macro lock(l, expr)
quote
temp = $(esc(l))
Expand All @@ -213,6 +229,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

0 comments on commit f34b3bc

Please sign in to comment.