-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
waiting on register modification events #156
base: master
Are you sure you want to change the base?
Changes from 2 commits
067051d
b48b23e
c11673f
6388b6f
66024d3
9e628a5
06b1f0c
708a12c
68af5bd
ed372e6
badb9a6
ac025b5
ecfddb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,29 @@ | ||||
using ConcurrentSim | ||||
using ResumableFunctions | ||||
import Base: unlock, lock | ||||
|
||||
"""Multiple processes can wait on this semaphore for a permission to run given by another process""" | ||||
struct AsymmetricSemaphore | ||||
nbwaiters::Ref{Int} | ||||
lock::Resource | ||||
end | ||||
AsymmetricSemaphore(sim) = AsymmetricSemaphore(Ref(0), Resource(sim,1,level=1)) # start locked | ||||
|
||||
function Base.lock(s::AsymmetricSemaphore) | ||||
return @process _lock(s.lock.env, s) | ||||
end | ||||
|
||||
@resumable function _lock(sim, s::AsymmetricSemaphore) | ||||
s.nbwaiters[] += 1 | ||||
@yield lock(s.lock) | ||||
s.nbwaiters[] -= 1 | ||||
if s.nbwaiters[] > 0 | ||||
unlock(s.lock) | ||||
end | ||||
end | ||||
|
||||
function unlock(s::AsymmetricSemaphore) | ||||
if s.nbwaiters[] > 0 | ||||
unlock(s.lock) | ||||
end | ||||
end | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rest of the library does not automatically know about the existence of this file. In needs to be included from the main file QuantumSavory.jl/src/QuantumSavory.jl Line 82 in 46cb2f3
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,58 @@ | ||
#using QuantumSavory: AsymmetricSemaphore | ||
# TODO better constructors | ||
# TODO am I overusing Ref | ||
|
||
using ConcurrentSim | ||
using ResumableFunctions | ||
import Base: unlock, lock | ||
import Base: getindex, setindex! | ||
|
||
"""Multiple processes can wait on this semaphore for a permission to run given by another process""" | ||
struct AsymmetricSemaphore | ||
nbwaiters::Ref{Int} | ||
lock::Resource | ||
end | ||
AsymmetricSemaphore(sim) = AsymmetricSemaphore(Ref(0), Resource(sim,1,level=1)) # start locked | ||
|
||
function Base.lock(s::AsymmetricSemaphore) | ||
return @process _lock(s.lock.env, s) | ||
end | ||
|
||
@resumable function _lock(sim, s::AsymmetricSemaphore) | ||
s.nbwaiters[] += 1 | ||
@yield lock(s.lock) | ||
s.nbwaiters[] -= 1 | ||
if s.nbwaiters[] > 0 | ||
unlock(s.lock) | ||
end | ||
end | ||
|
||
function unlock(s::AsymmetricSemaphore) | ||
if s.nbwaiters[] > 0 | ||
unlock(s.lock) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be repeating the content of |
||
|
||
"""Vector with a semaphore where processes can wait on until there's a change in the vector""" | ||
struct StateIndexVector | ||
data::Vector{Int} | ||
waiter::AsymmetricSemaphore | ||
end | ||
|
||
function StateIndexVector(data::Vector{Int}) | ||
env = ConcurrentSim.Simulation() | ||
return StateIndexVector(data, AsymmetricSemaphore(env)) | ||
end | ||
|
||
function getindex(vec::StateIndexVector, index::Int) | ||
return vec.data[index] | ||
end | ||
|
||
function setindex!(vec::StateIndexVector, value::Int, index::Int) | ||
vec.data[index] = value | ||
unlock(vec.waiter) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this for? Can it be removed? |
||
|
||
struct StateRef | ||
state::Base.RefValue{Any} # TODO it would be nice if this was not abstract but `uptotime!` converts between types... maybe make StateRef{T} state::RefValue{T} and a new function that swaps away the backpointers in the appropriate registers | ||
registers::Vector{Any} # TODO Should be Vector{Register}, but right now we occasionally set it to nothing to deal with padded storage | ||
|
@@ -23,15 +76,16 @@ struct Register # TODO better type description | |
tag_info::Dict{Int128, @NamedTuple{tag::Tag, slot::Int, time::Float64}} | ||
guids::Vector{Int128} | ||
netparent::Ref{Any} | ||
tag_waiter::AsymmetricSemaphore | ||
end | ||
|
||
function Register(traits, reprs, bg, sr, si, at) | ||
env = ConcurrentSim.Simulation() | ||
Register(traits, reprs, bg, sr, si, at, [ConcurrentSim.Resource(env) for _ in traits], Dict{Int128, Tuple{Tag, Int64, Float64}}(), [], nothing) | ||
Register(traits, reprs, bg, sr, si, at, [ConcurrentSim.Resource(env) for _ in traits], Dict{Int128, Tuple{Tag, Int64, Float64}}(), [], nothing, AsymmetricSemaphore(env)) | ||
end | ||
|
||
Register(traits,reprs,bg,sr,si) = Register(traits,reprs,bg,sr,si,zeros(length(traits))) | ||
Register(traits,reprs,bg) = Register(traits,reprs,bg,fill(nothing,length(traits)),zeros(Int,length(traits)),zeros(length(traits))) | ||
Register(traits,reprs,bg) = Register(traits,reprs,bg,fill(nothing,length(traits)),StateIndexVector(zeros(Int,length(traits))),zeros(length(traits))) | ||
Register(traits,bg::Base.AbstractVecOrTuple{<:Union{Nothing,<:AbstractBackground}}) = Register(traits,default_repr.(traits),bg) | ||
Register(traits,reprs::Base.AbstractVecOrTuple{<:AbstractRepresentation}) = Register(traits,reprs,fill(nothing,length(traits))) | ||
Register(traits) = Register(traits,default_repr.(traits)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure, but I think this should be
@yield lock(...) | lock(...)
so that it runs whenever there is a change to either one of them. This would need more testing for correctness though.