Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
staticfloat committed Feb 7, 2024
1 parent 11cda8c commit 676a035
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/independentlylinearizedutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ us = CachePool(Vector{S}, () -> Vector{S}(undef, num_us); thread_safe=false)
end
end
```
!!! warning "Escaping values"
You must not use an acquired value after you have released it;
the memory may be immediately re-used by some other consumer of
your cache pool. Do not allow the acquired value to escape
outside of the `@with_cache` block, or past a `release!()`.
"""
mutable struct CachePool{T, THREAD_SAFE}
pool::Vector{T}
alloc::Function
const pool::Vector{T}
const alloc::Function
lock::ReentrantLock
num_alloced::Int

Check warning on line 39 in src/independentlylinearizedutils.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"alloced" should be "allocated".
num_acquired::Int

function CachePool(T, alloc::F; thread_safe::Bool = true) where {F}
return new{T,Val{thread_safe}}(T[], alloc, ReentrantLock(), 0)
Expand All @@ -46,6 +53,7 @@ Returns a cached element of the cache pool, calling `cache.alloc()` if none
are available.
"""
Base.@inline function acquire!(cache::CachePool{T}, _dummy = nothing) where {T}
cache.num_acquired += 1
if isempty(cache.pool)
cache.num_alloced += 1

Check warning on line 58 in src/independentlylinearizedutils.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"alloced" should be "allocated".
return cache.alloc()::T
Expand All @@ -60,11 +68,17 @@ Returns the value `val` to the cache pool.
"""
Base.@inline function release!(cache::CachePool, val, _dummy = nothing)
push!(cache.pool, val)
cache.num_acquired -= 1
end

function is_fully_released(cache::CachePool, _dummy = nothing)
return cache.num_acquired == 0
end

# Thread-safe versions just sub out to the other methods, using `_dummy` to force correct dispatch
acquire!(cache::ThreadSafeCachePool) = @lock cache.lock acquire!(cache, nothing)
release!(cache::ThreadSafeCachePool, val) = @lock cache.lock release!(cache, val, nothing)
is_fully_released(cache::ThreadSafeCachePool) = @lock cache.lock is_fully_released(cache, nothing)

macro with_cache(cache, name, body)
return quote
Expand Down Expand Up @@ -270,8 +284,8 @@ mutable struct IndependentlyLinearizedSolution{T, S, N}
time_mask::BitMatrix

# Temporary object used during construction, will be set to `nothing` at the end.
ilsc::Union{Nothing,IndependentlyLinearizedSolutionChunks{T,S}}
ilsc_cache_pool::Union{Nothing,ThreadSafeCachePool{IndependentlyLinearizedSolutionChunksCache{T,S}}}
ilsc::Union{Nothing,IndependentlyLinearizedSolutionChunks{T,S,N}}
ilsc_cache_pool::Union{Nothing,ThreadSafeCachePool{IndependentlyLinearizedSolutionChunksCache{T,S,N}}}
end
# Helper function to create an ILS wrapped around an in-progress ILSC
function IndependentlyLinearizedSolution(ilsc::IndependentlyLinearizedSolutionChunks{T,S,N}, cache_pool = nothing) where {T,S,N}
Expand Down Expand Up @@ -392,14 +406,17 @@ function finish!(ils::IndependentlyLinearizedSolution{T,S}) where {T,S}
# Update our struct, release the `ilsc` and its caches
for t_chunk in ilsc.t_chunks
release!(ilsc.cache.t_chunks, t_chunk)
@assert is_fully_released(ilsc.cache.t_chunks)
end
for u_idx in 1:length(ilsc.u_chunks)
for u_chunk in ilsc.u_chunks[u_idx]
release!(ilsc.cache.u_chunks, u_chunk)
end
@assert is_fully_released(ilsc.cache.u_chunks)
end
for time_mask in ilsc.time_masks
release!(ilsc.cache.time_masks, time_mask)
@assert is_fully_released(ilsc.cache.time_masks)
end
if ils.ilsc_cache_pool !== nothing
release!(ils.ilsc_cache_pool, ilsc.cache)
Expand Down

0 comments on commit 676a035

Please sign in to comment.