-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
keep a single message buffer per node, not per network edge
- Loading branch information
Showing
5 changed files
with
98 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,37 @@ | ||
struct MessageBuffer{T} | ||
queue::DelayQueue{T} | ||
buffer::Vector{T} | ||
signalreception::Resource | ||
env::Simulation | ||
buffer::Vector{NamedTuple{(:src,:tag), Tuple{Int,T}}} | ||
waiters::IdDict{Resource,Resource} | ||
end | ||
|
||
@resumable function take_loop_mb(env, q, mb) | ||
@resumable function take_loop_mb(env, channel, src, mb) | ||
while true | ||
@yield lock(mb.signalreception) | ||
msg = @yield take!(q) | ||
push!(mb.buffer, msg) | ||
unlock(mb.signalreception) | ||
tag = @yield take!(channel) | ||
push!(mb.buffer, (;src,tag)) | ||
for waiter in keys(mb.waiters) | ||
unlock(waiter) | ||
end | ||
end | ||
end | ||
|
||
function MessageBuffer(q::DelayQueue{T}) where {T} | ||
mb = MessageBuffer{T}(q, T[], ConcurrentSim.Resource(q.store.env)) | ||
@process take_loop_mb(q.store.env, q, mb) | ||
function MessageBuffer(qs::Vector{NamedTuple{(:src,:channel), Tuple{Int, DelayQueue{T}}}}) where {T} | ||
env = qs[1].channel.store.env | ||
signal = IdDict{Resource,Resource}() | ||
mb = MessageBuffer{T}(env, Tuple{Int,T}[], signal) | ||
for (;src, channel) in qs | ||
@process take_loop_mb(env, channel, src, mb) | ||
end | ||
mb | ||
end | ||
|
||
@resumable function wait_process(env, mb::MessageBuffer) | ||
@yield lock(mb.signalreception) | ||
unlock(mb.signalreception) | ||
waitresource = Resource(env) | ||
lock(waitresource) | ||
mb.waiters[waitresource] = waitresource | ||
@yield lock(waitresource) | ||
pop!(mb.waiters, waitresource) | ||
end | ||
|
||
function Base.wait(mb::MessageBuffer) | ||
@process wait_process(mb.queue.store.env, mb) | ||
@process wait_process(mb.env, mb) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using QuantumSavory | ||
using QuantumSavory: tag_types | ||
using ResumableFunctions, ConcurrentSim | ||
using Test | ||
|
||
net = RegisterNet([Register(3), Register(2), Register(3)]) | ||
env = get_time_tracker(net); | ||
@resumable function receive_tags(env) | ||
while true | ||
mb = messagebuffer(net, 2) | ||
@yield wait(mb) | ||
msg = querypop!(mb, :second_tag, ❓, ❓) | ||
print("t=$(now(env)): query returns ") | ||
if isnothing(msg) | ||
#println("nothing") | ||
else | ||
#println("$(msg.tag) received from node $(msg.src)") | ||
end | ||
end | ||
end | ||
@resumable function send_tags(env) | ||
@yield timeout(env, 1.0) | ||
put!(channel(net, 1=>2), Tag(:my_tag)) | ||
@yield timeout(env, 2.0) | ||
put!(channel(net, 3=>2), Tag(:second_tag, 123, 456)) | ||
end | ||
@process send_tags(env); | ||
@process receive_tags(env); | ||
run(env, 10) | ||
|
||
@test query(messagebuffer(net, 2), :second_tag, ❓, ❓) === nothing | ||
@test query(messagebuffer(net, 2), :my_tag).tag == Tag(:my_tag) |