Skip to content

Commit

Permalink
Allow priorities to be Floats
Browse files Browse the repository at this point in the history
Extended the store and container objects so that priorities are not restricted to being integers.
This is a more general expression of priority which may make it easier at some point to use rank functions (JuliaDynamics#75).
This also avoids having to normalize (and sometimes scaling) priorities so that they are integer in queueing systems.
  • Loading branch information
hdavid16 committed Oct 27, 2021
1 parent 1dbbdae commit c431283
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 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
priority :: Real
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::Real=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::Real=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::Real=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::Real=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
8 changes: 4 additions & 4 deletions src/resources/stores.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
struct StorePutKey{T} <: ResourceKey
priority :: Int
priority :: Real
id :: UInt
item :: T
StorePutKey{T}(priority, id, item) where T = new(priority, id, item)
end

struct StoreGetKey <: ResourceKey
priority :: Int
priority :: Real
id :: UInt
filter :: Function
end
Expand All @@ -24,7 +24,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::Real=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 +34,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::Real=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

0 comments on commit c431283

Please sign in to comment.