Skip to content

Commit

Permalink
Restructure MiscAlgorithms, "remove" multi threading for now
Browse files Browse the repository at this point in the history
  • Loading branch information
nHackel committed Dec 12, 2024
1 parent dae2eeb commit 80f56e2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/MiscAlgorithms/MiscAlgorithms.jl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include("RuntimeAlgorithms.jl")
include("ThreadPinnedAlgorithm.jl")
Original file line number Diff line number Diff line change
@@ -1,47 +1,3 @@
export ThreadPinnedAlgorithm, ThreadPinnedAlgorithmParameter
Base.@kwdef struct ThreadPinnedAlgorithmParameter <: AbstractImageReconstructionParameters
threadID::Int64
algo::AbstractImageReconstructionAlgorithm
end

mutable struct ThreadPinnedAlgorithm <: AbstractImageReconstructionAlgorithm
params::ThreadPinnedAlgorithmParameter
recoTask::Union{Nothing,Task}
taskLock::ReentrantLock
inputChannel::Channel{Any}
outputChannel::Channel{Any}
end

ThreadPinnedAlgorithm(params::ThreadPinnedAlgorithmParameter) = ThreadPinnedAlgorithm(params, nothing, ReentrantLock(), Channel{Any}(Inf), Channel{Any}(Inf))

take!(algo::ThreadPinnedAlgorithm) = take!(algo.outputChannel)
function put!(algo::ThreadPinnedAlgorithm, u)
put!(algo.inputChannel, u)
lock(algo.taskLock)
try
if isnothing(algo.recoTask) || istaskdone(algo.recoTask)
algo.recoTask = @tspawnat algo.params.threadID pinnedRecoTask(algo)
end
finally
unlock(algo.taskLock)
end
end
function pinnedRecoTask(algo::ThreadPinnedAlgorithm)
while isready(algo.inputChannel)
result = nothing
try
put!(algo.params.algo, take!(algo.inputChannel))
result = take!(algo.params.algo)
catch e
result = e
end
put!(algo.outputChannel, result)
end
end
# TODO general async task, has to preserve order (cant just spawn task for each put)
# TODO Timeout task with timeout options for put and take
# TODO maybe can be cancelled?

export AbstractMultiThreadedProcessing
abstract type AbstractMultiThreadedProcessing <: AbstractImageReconstructionAlgorithm end

Expand Down
43 changes: 43 additions & 0 deletions src/MiscAlgorithms/ThreadPinnedAlgorithm.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export ThreadPinnedAlgorithm, ThreadPinnedAlgorithmParameter
Base.@kwdef struct ThreadPinnedAlgorithmParameter <: AbstractImageReconstructionParameters
threadID::Int64
algo::AbstractImageReconstructionAlgorithm
end

mutable struct ThreadPinnedAlgorithm <: AbstractImageReconstructionAlgorithm
params::ThreadPinnedAlgorithmParameter
recoTask::Union{Nothing,Task}
taskLock::ReentrantLock
inputChannel::Channel{Any}
outputChannel::Channel{Any}
end

ThreadPinnedAlgorithm(params::ThreadPinnedAlgorithmParameter) = ThreadPinnedAlgorithm(params, nothing, ReentrantLock(), Channel{Any}(Inf), Channel{Any}(Inf))

take!(algo::ThreadPinnedAlgorithm) = take!(algo.outputChannel)
function put!(algo::ThreadPinnedAlgorithm, u)
put!(algo.inputChannel, u)
lock(algo.taskLock)
try
if isnothing(algo.recoTask) || istaskdone(algo.recoTask)
algo.recoTask = @tspawnat algo.params.threadID pinnedRecoTask(algo)
end
finally
unlock(algo.taskLock)
end
end
function pinnedRecoTask(algo::ThreadPinnedAlgorithm)
while isready(algo.inputChannel)
result = nothing
try
put!(algo.params.algo, take!(algo.inputChannel))
result = take!(algo.params.algo)
catch e
result = e
end
put!(algo.outputChannel, result)
end
end
# TODO general async task, has to preserve order (cant just spawn task for each put)
# TODO Timeout task with timeout options for put and take
# TODO maybe can be cancelled?

0 comments on commit 80f56e2

Please sign in to comment.