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

Priority as number #90

Merged
merged 31 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6d46c77
Update base.jl
hdavid16 Jul 9, 2023
59d237d
Update events.jl
hdavid16 Jul 9, 2023
f0303d9
Update containers.jl
hdavid16 Jul 9, 2023
d8e1320
Update stores.jl
hdavid16 Jul 9, 2023
ba6f5ea
Update simulations.jl
hdavid16 Jul 9, 2023
2efeae2
Update time.jl
hdavid16 Jul 9, 2023
7913399
Update test_resources_containers.jl
hdavid16 Jul 9, 2023
485d520
update max priority to `Inf`
hdavid16 Jul 9, 2023
a4df1e6
Update max priority to `Inf`
hdavid16 Jul 9, 2023
c19c4f6
Update max priority to `Inf`
hdavid16 Jul 9, 2023
fae28d5
Update base.jl
hdavid16 Jul 9, 2023
deb26f2
Update base.jl
hdavid16 Jul 9, 2023
b60be22
Update base.jl
hdavid16 Jul 9, 2023
4f98ac3
Update base.jl
hdavid16 Jul 9, 2023
61f0693
Update base.jl
hdavid16 Jul 9, 2023
e02cda2
user parametric type
hdavid16 Jul 9, 2023
c3ba26d
Update stores.jl
hdavid16 Jul 9, 2023
1123ccc
use parametric type
hdavid16 Jul 9, 2023
d9e16cc
use parametric type
hdavid16 Jul 9, 2023
aa10136
fix struct internal function
hdavid16 Jul 9, 2023
a292a9c
remove `new` call in StorePutKey
hdavid16 Jul 9, 2023
baa80b4
allow priorities to be Number in Containers and Stores.
hdavid16 Jul 29, 2023
b21674b
make parameter type order consistent btw resources
hdavid16 Jul 29, 2023
3b46b15
Merge branch 'master' into priority_as_number
Krastanov Aug 2, 2023
cac0fc1
fixup merge
Krastanov Aug 2, 2023
32fccd3
fixup merge
Krastanov Aug 2, 2023
cedae0b
add tests for resources with different priorities
hdavid16 Aug 3, 2023
54238ec
allow put! and get to pass objects of different priority type
hdavid16 Aug 3, 2023
136aecf
remove type signatures from kwargs in resources
hdavid16 Aug 3, 2023
944135a
fixup
Krastanov Aug 3, 2023
fbbc732
fixup
Krastanov Aug 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function remove_callback(cb::Function, ev::AbstractEvent)
i != 0 && deleteat!(ev.bev.callbacks, i)
end

function schedule(ev::AbstractEvent, delay::Number=zero(Float64); priority::Int=0, value::Any=nothing)
function schedule(ev::AbstractEvent, delay::Number=0; priority::Number=0, value::Any=nothing)
Copy link
Member

Choose a reason for hiding this comment

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

It seems a lot of these type restriction can simply be dropped. Types in function signatures are used only for dispatch, they do not provide performance improvements. And it seems types in kwargs do not even participate in dispatch (see https://discourse.julialang.org/t/force-specialization-on-kwargs/34789/14 ). So this makes types for kwargs (and in particular abstract types) not particularly valuable. In most of these situations (function signatures) you can just delete the ::Int, no need to add ::Number.

state(ev) === processed && throw(EventProcessed(ev))
env = environment(ev)
bev = ev.bev
Expand Down
8 changes: 4 additions & 4 deletions src/events.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ struct Event <: AbstractEvent
end
end

function succeed(ev::Event; priority::Int=0, value::Any=nothing) :: Event
function succeed(ev::Event; priority::Number=0, value::Any=nothing) :: Event
state(ev) !== idle && throw(EventNotIdle(ev))
schedule(ev; priority=priority, value=value)
end

function fail(ev::Event, exc::Exception; priority::Int=0) :: Event
function fail(ev::Event, exc::Exception; priority::Number=0) :: Event
succeed(ev; priority=priority, value=exc)
end

Expand All @@ -21,10 +21,10 @@ struct Timeout <: AbstractEvent
end
end

function timeout(env::Environment, delay::Number=0; priority::Int=0, value::Any=nothing)
function timeout(env::Environment, delay::Number=0; priority::Number=0, value::Any=nothing)
schedule(Timeout(env), delay; priority=priority, value=value)
end

function run(env::Environment, until::Number=typemax(Float64))
function run(env::Environment, until::Number=Inf)
run(env, timeout(env, until-now(env)))
end
2 changes: 1 addition & 1 deletion src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function check(ev::AbstractEvent, op::Operator, event_state_values::Dict{Abstrac
end
elseif state(op) === scheduled
if isa(val, Exception)
schedule(op; priority=typemax(Int), value=val)
schedule(op; priority=Inf, value=val)
else
event_state_values[ev] = StateValue(state(ev), val)
end
Expand Down
6 changes: 3 additions & 3 deletions src/processes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ end
function interrupt(proc::Process, cause::Any=nothing)
env = environment(proc)
if proc.fsmi._state !== 0xff
proc.target isa Initialize && schedule(proc.target; priority=typemax(Int))
target = schedule(Interrupt(env); priority=typemax(Int), value=InterruptException(active_process(env), cause))
proc.target isa Initialize && schedule(proc.target; priority=Inf)
target = schedule(Interrupt(env); priority=Inf, value=InterruptException(active_process(env), cause))
@callback execute_interrupt(target, proc)
end
timeout(env; priority=typemax(Int))
timeout(env; priority=Inf)
end
12 changes: 6 additions & 6 deletions src/resources/containers.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct ContainerKey{N<:Real} <: ResourceKey
priority :: Int
struct ContainerKey{T<:Number, N<:Real} <: ResourceKey
priority :: T
id :: UInt
amount :: N
end
Expand All @@ -22,25 +22,25 @@ end

const Resource = Container{Int}

function put(con::Container{N}, amount::N; priority::Int=0) where N<:Real
function put(con::Container{N}, amount::N; priority::Number=0) where N<:Real
put_ev = Put(con.env)
con.put_queue[put_ev] = ContainerKey(priority, con.seid+=one(UInt), amount)
@callback trigger_get(put_ev, con)
trigger_put(put_ev, con)
put_ev
end

request(res::Resource; priority::Int=0) = put(res, 1; priority=priority)
request(res::Resource; priority::Number=0) = put(res, 1; priority=priority)

function get(con::Container{N}, amount::N; priority::Int=0) where N<:Real
function get(con::Container{N}, amount::N; priority::Number=0) where N<:Real
get_ev = Get(con.env)
con.get_queue[get_ev] = ContainerKey(priority, con.seid+=one(UInt), amount)
@callback trigger_put(get_ev, con)
trigger_get(get_ev, con)
get_ev
end

release(res::Resource; priority::Int=0) = get(res, 1; priority=priority)
release(res::Resource; priority::Number=0) = get(res, 1; priority=priority)

function do_put(con::Container{N}, put_ev::Put, key::ContainerKey{N}) where N<:Real
con.level + key.amount > con.capacity && return false
Expand Down
13 changes: 6 additions & 7 deletions src/resources/stores.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
struct StorePutKey{T} <: ResourceKey
priority :: Int
struct StorePutKey{N<:Number, T} <: ResourceKey
priority :: N
id :: UInt
item :: T
StorePutKey{T}(priority, id, item) where T = new(priority, id, item)
end

struct StoreGetKey <: ResourceKey
priority :: Int
struct StoreGetKey{N<:Number} <: ResourceKey
priority :: N
id :: UInt
filter :: Function
end
Expand All @@ -24,7 +23,7 @@ mutable struct Store{T} <: AbstractResource
end
end

function put(sto::Store{T}, item::T; priority::Int=0) where T
function put(sto::Store{T}, item::T; priority::Number=0) where T
put_ev = Put(sto.env)
sto.put_queue[put_ev] = StorePutKey{T}(priority, sto.seid+=one(UInt), item)
@callback trigger_get(put_ev, sto)
Expand All @@ -34,7 +33,7 @@ end

get_any_item(::T) where T = true

function get(sto::Store{T}, filter::Function=get_any_item; priority::Int=0) where T
function get(sto::Store{T}, filter::Function=get_any_item; priority::Number=0) where T
get_ev = Get(sto.env)
sto.get_queue[get_ev] = StoreGetKey(priority, sto.seid+=one(UInt), filter)
@callback trigger_put(get_ev, sto)
Expand Down
4 changes: 2 additions & 2 deletions src/simulations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ end

struct EmptySchedule <: Exception end

struct EventKey
struct EventKey{N<:Number}
time :: Float64
priority :: Int
priority :: N
id :: UInt
end

Expand Down
2 changes: 1 addition & 1 deletion src/utils/time.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function run(env::Environment, until::DateTime)
run(env, Dates.datetime2epochms(until))
end

function timeout(env::Environment, delay::Period; priority::Int=0, value::Any=nothing)
function timeout(env::Environment, delay::Period; priority::Number=0, value::Any=nothing)
time = now(env)
del = Dates.datetime2epochms(Dates.epochms2datetime(time)+delay)-time
timeout(env, del; priority=priority, value=value)
Expand Down
2 changes: 1 addition & 1 deletion test/test_resources_containers.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ConcurrentSim
using ResumableFunctions

@resumable function client(sim::Simulation, res::Resource, i::Int, priority::Int)
@resumable function client(sim::Simulation, res::Resource, i::Int, priority::Number)
println("$(now(sim)), client $i is waiting")
@yield request(res, priority=priority)
println("$(now(sim)), client $i is being served")
Expand Down