1- struct ContainerKey{N<: Real } <: ResourceKey
2- priority :: Int
1+ struct ContainerKey{N<: Real , T<: Number } <: ResourceKey
32 id :: UInt
43 amount :: N
4+ priority :: T
55end
66
77"""
8- Container{N}(env::Environment, capacity::N=one(N); level::N=zero(N))
8+ Container{N<:Real, T<:Number }(env::Environment, capacity::N=one(N); level::N=zero(N))
99
1010A "Container" resource object, storing up to `capacity` units of a resource (of type `N`).
1111
12- There is a `Resource` alias for `Container{Int}`.
12+ There is a `Resource` alias for `Container{Int, Int }`.
1313
1414`Resource()` with default capacity of `1` is very similar to a typical lock.
1515The [`request`](@ref) and [`unlock`](@ref) functions are a convenient way to interact with such a "lock",
@@ -19,27 +19,31 @@ See [`Store`](@ref) for a more channel-like resource.
1919
2020Think of `Resource` and `Container` as locks and of `Store` as channels. They block only if empty (on taking) or full (on storing).
2121"""
22- mutable struct Container{N<: Real } <: AbstractResource
22+ mutable struct Container{N<: Real , T <: Number } <: AbstractResource
2323 env :: Environment
2424 capacity :: N
2525 level :: N
2626 seid :: UInt
27- put_queue :: DataStructures.PriorityQueue{Put, ContainerKey{N}}
28- get_queue :: DataStructures.PriorityQueue{Get, ContainerKey{N}}
29- function Container {N} (env:: Environment , capacity:: N = one (N); level:: N = zero (N)) where {N<: Real }
30- new (env, capacity, level, zero (UInt), DataStructures. PriorityQueue {Put, ContainerKey{N}} (), DataStructures. PriorityQueue {Get, ContainerKey{N}} ())
27+ put_queue :: DataStructures.PriorityQueue{Put, ContainerKey{N, T }}
28+ get_queue :: DataStructures.PriorityQueue{Get, ContainerKey{N, T }}
29+ function Container {N, T } (env:: Environment , capacity:: N = one (N); level= zero (N)) where {N<: Real , T <: Number }
30+ new (env, capacity, N ( level) , zero (UInt), DataStructures. PriorityQueue {Put, ContainerKey{N, T }} (), DataStructures. PriorityQueue {Get, ContainerKey{N, T }} ())
3131 end
3232end
3333
34- function Container (env:: Environment , capacity:: N = one (N); level:: N = zero (N)) where {N<: Real }
35- Container {N} (env, capacity, level= level)
34+ function Container (env:: Environment , capacity:: N = one (N); level= zero (N)) where {N<: Real }
35+ Container {N, Int } (env, capacity; level= N ( level) )
3636end
3737
38- const Resource = Container{Int}
38+ function Container {T} (env:: Environment , capacity:: N = one (N); level= zero (N)) where {N<: Real , T<: Number }
39+ Container {N, T} (env, capacity; level= N (level))
40+ end
41+
42+ const Resource = Container{Int, Int}
3943
40- function put! (con:: Container{N} , amount:: N ; priority:: Int = 0 ) where N<: Real
44+ function put! (con:: Container{N, T } , amount:: N ; priority= zero (T)) where { N<: Real , T <: Number }
4145 put_ev = Put (con. env)
42- con. put_queue[put_ev] = ContainerKey (priority, con. seid+= one (UInt), amount)
46+ con. put_queue[put_ev] = ContainerKey {N,T} ( con. seid+= one (UInt), amount, T (priority) )
4347 @callback trigger_get (put_ev, con)
4448 trigger_put (put_ev, con)
4549 put_ev
@@ -52,7 +56,7 @@ Locks the Container (or Resources) and return the lock event.
5256If the capacity of the Container is greater than 1,
5357multiple requests can be made before blocking occurs.
5458"""
55- request (res:: Container ; priority:: Int = 0 ) = put! (res, 1 ; priority = priority)
59+ request (res:: Resource ; priority= 0 ) = put! (res, 1 ; priority)
5660
5761"""
5862 tryrequest(res::Container)
@@ -76,14 +80,14 @@ julia> tryrequest(res)
7680false
7781```
7882"""
79- function tryrequest (res:: Container ; priority:: Int = 0 )
83+ function tryrequest (res:: Container ; priority= 0 )
8084 islocked (res) && return false # TODO check priority
8185 request (res; priority)
8286end
8387
84- function get (con:: Container{N} , amount:: N ; priority:: Int = 0 ) where N<: Real
88+ function get (con:: Container{N, T } , amount:: N ; priority= zero (T)) where { N<: Real , T <: Number }
8589 get_ev = Get (con. env)
86- con. get_queue[get_ev] = ContainerKey (priority, con. seid+= one (UInt), amount)
90+ con. get_queue[get_ev] = ContainerKey (con. seid+= one (UInt), amount, T (priority) )
8791 @callback trigger_put (get_ev, con)
8892 trigger_get (get_ev, con)
8993 get_ev
9498
9599Unlocks the Container and return the unlock event.
96100"""
97- unlock (res:: Container ; priority:: Int = 0 ) = get (res, 1 ; priority= priority)
101+ unlock (res:: Resource ; priority:: Number = 0 ) = get (res, 1 ; priority= priority)
98102
99- function do_put (con:: Container{N} , put_ev:: Put , key:: ContainerKey{N} ) where N<: Real
103+ function do_put (con:: Container{N, T } , put_ev:: Put , key:: ContainerKey{N, T } ) where { N<: Real , T <: Number }
100104 con. level + key. amount > con. capacity && return false
101105 schedule (put_ev)
102106 con. level += key. amount
103107 true
104108end
105109
106- function do_get (con:: Container{N} , get_ev:: Get , key:: ContainerKey{N} ) where N<: Real
110+ function do_get (con:: Container{N, T } , get_ev:: Get , key:: ContainerKey{N, T } ) where { N<: Real , T <: Number }
107111 con. level - key. amount < zero (N) && return false
108112 schedule (get_ev)
109113 con. level -= key. amount
0 commit comments