Skip to content

Commit 32ef930

Browse files
sairus7antoine-levitt
authored andcommitted
@lock export from Base, closes JuliaLang#36441 (JuliaLang#39588)
1 parent a35535e commit 32ef930

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Standard library changes
8585
@test isequal(complex(one(T)) / complex(T(Inf), T(-Inf)), complex(zero(T), zero(T))) broken=(T == Float64)
8686
```
8787
([#39322])
88+
* `@lock` is now exported from Base ([#39588]).
8889

8990
#### Package Manager
9091

base/exports.jl

+1
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ export
675675
istaskstarted,
676676
istaskfailed,
677677
lock,
678+
@lock,
678679
notify,
679680
ReentrantLock,
680681
schedule,

base/lock.jl

+23
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,22 @@ function trylock(f, l::AbstractLock)
201201
return false
202202
end
203203

204+
"""
205+
@lock l expr
206+
207+
Macro version of `lock(f, l::AbstractLock)` but with `expr` instead of `f` function.
208+
Expands to:
209+
```julia
210+
lock(l)
211+
try
212+
expr
213+
finally
214+
unlock(l)
215+
end
216+
```
217+
This is similar to using [`lock`](@ref) with a `do` block, but avoids creating a closure
218+
and thus can improve the performance.
219+
"""
204220
macro lock(l, expr)
205221
quote
206222
temp = $(esc(l))
@@ -213,6 +229,13 @@ macro lock(l, expr)
213229
end
214230
end
215231

232+
"""
233+
@lock_nofail l expr
234+
235+
Equivalent to `@lock l expr` for cases in which we can guarantee that the function
236+
will not throw any error. In this case, avoiding try-catch can improve the performance.
237+
See [`@lock`](@ref).
238+
"""
216239
macro lock_nofail(l, expr)
217240
quote
218241
temp = $(esc(l))

0 commit comments

Comments
 (0)