Skip to content
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

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

Krastanov
Copy link
Member

No description provided.

Copy link
Member Author

@Krastanov Krastanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, seems like a good start! Let's discuss the points raised above.

if s.nbwaiters[] > 0
unlock(s.lock)
end
end
Copy link
Member Author

Choose a reason for hiding this comment

The 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 src/QuantumSavory.jl. Given that it is a dependency for the content of states_registers.jl it should probably be included before it. E.g. here:

include("states_registers.jl")

Comment on lines 36 to 54
"""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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for? Can it be removed?

Comment on lines 5 to 34
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be repeating the content of semaphore.jl. I think my other comment about include-ing semaphore.jl will help clean this up.

Comment on lines 213 to 214
@yield lock(prot.nodeA.stateindices.waiter)
@yield lock(prot.nodeB.stateindices.waiter)
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants