Skip to content

Commit fbc4270

Browse files
committed
Add wait, isready, lock and unlock to algo-interface
1 parent 70910e4 commit fbc4270

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/AbstractImageReconstruction.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using Observables
66
using Scratch
77
using LRUCache
88

9-
import Base: put!, take!, fieldtypes, fieldtype, ismissing, propertynames, parent, hash, wait, isready
9+
import Base: put!, take!, fieldtypes, fieldtype, ismissing, propertynames, parent, hash, wait, isready, lock, unlock
1010

1111
include("AlgorithmInterface.jl")
1212
include("StructTransforms.jl")

src/AlgorithmInterface.jl

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,57 @@ put!(algo::AbstractImageReconstructionAlgorithm, inputs...) = error("$(typeof(al
2727
Remove and return a stored result from the algorithm `algo`. Blocks until a result is available.
2828
"""
2929
take!(algo::AbstractImageReconstructionAlgorithm) = error("$(typeof(algo)) must implement take!")
30+
"""
31+
isready(algo::AbstractImageReconstructionAlgorithm)
32+
33+
Determine if the algorithm `algo` has a result available.
34+
"""
35+
isready(algo::AbstractImageReconstructionAlgorithm) = error("$(typeof(algo)) must implement isready")
36+
"""
37+
wait(algo::AbstractImageReconstructionAlgorithm)
38+
39+
Wait for a result to be available from the specified `algo`.
40+
"""
41+
wait(algo::AbstractImageReconstructionAlgorithm) = error("$(typeof(algo)) must implement wait")
42+
"""
43+
lock(algo::AbstractImageReconstructionAlgorithm)
44+
45+
Acquire a lock on the algorithm `algo`. If the lock is already acquired, wait until it is released.
46+
47+
Each `lock` must be matched with a `unlock`.
48+
"""
49+
lock(algo::AbstractImageReconstructionAlgorithm) = error("$(typeof(algo)) must implement lock")
50+
"""
51+
unlock(algo::AbstractImageReconstructionAlgorithm)
52+
53+
Release a lock on the algorithm `algo`.
54+
"""
55+
unlock(algo::AbstractImageReconstructionAlgorithm) = error("$(typeof(algo)) must implement unlock")
56+
"""
57+
lock(fn, algo::AbstractImageReconstructionAlgorithm)
58+
59+
Acquire the `lock` on `algo`, execute `fn` and release the `lock` afterwards.
60+
"""
61+
function lock(fn, algo::AbstractImageReconstructionAlgorithm)
62+
lock(algo)
63+
try
64+
fn()
65+
finally
66+
unlock(algo)
67+
end
68+
end
3069

3170
export reconstruct
3271
"""
3372
reconstruct(algo::T, u) where {T<:AbstractImageReconstructionAlgorithm}
3473
35-
Reconstruct an image from input `u` using algorithm `algo`.
74+
Reconstruct an image from input `u` using algorithm `algo`. The `àlgo` will be `lock`ed until the result is available or an error occurs.
3675
"""
3776
function reconstruct(algo::T, u) where {T<:AbstractImageReconstructionAlgorithm}
38-
put!(algo, u)
39-
return take!(algo)
77+
lock(algo) do
78+
put!(algo, u)
79+
return take!(algo)
80+
end
4081
end
4182

4283
export process

0 commit comments

Comments
 (0)