File tree 4 files changed +51
-0
lines changed
4 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ Julia v1.13 Release Notes
4
4
New language features
5
5
---------------------
6
6
7
+ - New ` Base.@acquire ` macro for a non-closure version of ` Base.acquire(f, s::Base.Semaphore) ` , like ` @lock ` . ([ #56845 ] )
8
+
7
9
Language changes
8
10
----------------
9
11
Original file line number Diff line number Diff line change @@ -561,6 +561,36 @@ function acquire(f, s::Semaphore)
561
561
end
562
562
end
563
563
564
+ """
565
+ Base.@acquire s::Semaphore expr
566
+
567
+ Macro version of `Base.acquire(f, s::Semaphore)` but with `expr` instead of `f` function.
568
+ Expands to:
569
+ ```julia
570
+ Base.acquire(s)
571
+ try
572
+ expr
573
+ finally
574
+ Base.release(s)
575
+ end
576
+ ```
577
+ This is similar to using [`acquire`](@ref) with a `do` block, but avoids creating a closure.
578
+
579
+ !!! compat "Julia 1.13"
580
+ `Base.@acquire` was added in Julia 1.13
581
+ """
582
+ macro acquire (s, expr)
583
+ quote
584
+ local temp = $ (esc (s))
585
+ Base. acquire (temp)
586
+ try
587
+ $ (esc (expr))
588
+ finally
589
+ Base. release (temp)
590
+ end
591
+ end
592
+ end
593
+
564
594
"""
565
595
release(s::Semaphore)
566
596
Original file line number Diff line number Diff line change 28
28
# Semaphores
29
29
Semaphore,
30
30
acquire,
31
+ @acquire ,
31
32
release,
32
33
33
34
# arrays
Original file line number Diff line number Diff line change 237
237
@test all (<= (sem_size), history)
238
238
@test all (>= (0 ), history)
239
239
@test history[end ] == 0
240
+
241
+ # macro form
242
+ clock = Threads. Atomic {Int} (1 )
243
+ occupied = Threads. Atomic {Int} (0 )
244
+ history = fill! (Vector {Int} (undef, 2 n), - 1 )
245
+ @sync for _ in 1 : n
246
+ @async begin
247
+ @test Base. @acquire s begin
248
+ history[Threads. atomic_add! (clock, 1 )] = Threads. atomic_add! (occupied, 1 ) + 1
249
+ sleep (rand (0 : 0.01 : 0.1 ))
250
+ history[Threads. atomic_add! (clock, 1 )] = Threads. atomic_sub! (occupied, 1 ) - 1
251
+ return :resultvalue
252
+ end === :resultvalue
253
+ end
254
+ end
255
+ @test all (<= (sem_size), history)
256
+ @test all (>= (0 ), history)
257
+ @test history[end ] == 0
240
258
end
241
259
242
260
# task switching
You can’t perform that action at this time.
0 commit comments