diff --git a/src/ProtocolZoo/cutoff.jl b/src/ProtocolZoo/cutoff.jl index 4d399475..f975f05a 100644 --- a/src/ProtocolZoo/cutoff.jl +++ b/src/ProtocolZoo/cutoff.jl @@ -69,7 +69,9 @@ end unlock(slot) end - if !isnothing(prot.period) + if isnothing(prot.period) + @yield onchange_tag(reg) + else @yield timeout(prot.sim, prot.period) end end diff --git a/src/semaphore.jl b/src/semaphore.jl index 31effde8..5f6df27f 100644 --- a/src/semaphore.jl +++ b/src/semaphore.jl @@ -1,6 +1,6 @@ using ConcurrentSim using ResumableFunctions -import Base: unlock, lock +import Base: unlock, lock, islocked """Multiple processes can wait on this semaphore for a permission to run given by another process""" struct AsymmetricSemaphore @@ -27,3 +27,7 @@ function unlock(s::AsymmetricSemaphore) unlock(s.lock) end end + +function islocked(s::AsymmetricSemaphore) + return islocked(s.lock) +end diff --git a/test/test_protocolzoo_cutoffprot.jl b/test/test_protocolzoo_cutoffprot.jl index 5dc00dc6..0d4a965c 100644 --- a/test/test_protocolzoo_cutoffprot.jl +++ b/test/test_protocolzoo_cutoffprot.jl @@ -27,4 +27,36 @@ run(sim, 2.0) run(sim, 6.0) @test !isassigned(net[1][1]) @test isassigned(net[2][1]) + +# test for period=nothing +net = RegisterNet([Register(2), Register(1)]) +sim = get_time_tracker(net) +initialize!((net[1][1], net[2][1]), (Z1⊗Z1+Z2⊗Z2)/(sqrt(2.0))) +tag!(net[1][1], EntanglementCounterpart, 2, 1) +tag!(net[2][1], EntanglementCounterpart, 1, 1) + +cprot1 = CutoffProt(sim, net, 1; period=nothing, retention_time=3.0) +cprot2 = CutoffProt(sim, net, 2; period=0.1, retention_time=3.0) +@process cprot1() +@process cprot2() + +@resumable function wait_and_tag(sim) + @yield timeout(sim, 5) + tag!(net[1][2], Int) end + +@process wait_and_tag(sim) + +run(sim, 2.0) +@test isassigned(net[1][1]) +@test isassigned(net[2][1]) + +run(sim, 4.0) +@test isassigned(net[1][1]) +@test !isassigned(net[2][1]) + +run(sim, 6.0) +@test !isassigned(net[1][1]) +@test !isassigned(net[1][1]) + +end \ No newline at end of file