Skip to content

Commit 54f5fb3

Browse files
committed
introduce lock, unlock, islocked, isready
1 parent 8e72fe9 commit 54f5fb3

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

src/ConcurrentSim.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module ConcurrentSim
77
using Dates
88
using ResumableFunctions
99

10-
import Base: run, isless, show, yield, get, put!, &, |
10+
import Base: run, isless, show, yield, get, put!, isready, islocked, unlock, lock, &, |
1111
import Dates: now
1212

1313
export AbstractEvent, Environment, value, state, environment

src/resources/containers.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,36 @@ function do_get(con::Container{N}, get_ev::Get, key::ContainerKey{N}) where N<:R
5555
con.level -= key.amount
5656
true
5757
end
58+
59+
"""
60+
isready(::Container)
61+
62+
Returns `true` if the Container is not empty, similarly to the meaning of `isready` for `Base.Channel`.
63+
64+
```jldoctest
65+
julia> sim = Simulation(); res = Resource(sim); isready(res)
66+
false
67+
68+
julia> request(res); isready(res)
69+
true
70+
```
71+
"""
72+
isready(c::Container) = c.level > 0
73+
74+
"""
75+
islocked(::Container)
76+
77+
Returns `true` if the store is full, similarly to the meaning of `islocked` for `Base.ReentrantLock`.
78+
79+
```jldoctest
80+
julia> sim = Simulation(); res = Resource(sim); islocked(res)
81+
false
82+
83+
julia> request(res); islocked(res)
84+
true
85+
```
86+
"""
87+
islocked(c::Container) = c.level==c.capacity
88+
89+
unlock(c::Container) = release(c)
90+
lock(c::Container) = request(c)

src/resources/stores.jl

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ mutable struct Store{T} <: AbstractResource
1919
seid :: UInt
2020
put_queue :: DataStructures.PriorityQueue{Put, StorePutKey{T}}
2121
get_queue :: DataStructures.PriorityQueue{Get, StoreGetKey}
22-
function Store{T}(env::Environment; capacity::UInt=typemax(UInt)) where {T}
23-
new(env, capacity, zero(UInt), Dict{T, UInt}(), zero(UInt), DataStructures.PriorityQueue{Put, StorePutKey{T}}(), DataStructures.PriorityQueue{Get, StoreGetKey}())
22+
function Store{T}(env::Environment; capacity=typemax(UInt)) where {T}
23+
new(env, UInt(capacity), zero(UInt), Dict{T, UInt}(), zero(UInt), DataStructures.PriorityQueue{Put, StorePutKey{T}}(), DataStructures.PriorityQueue{Get, StoreGetKey}())
2424
end
2525
end
2626

@@ -66,3 +66,36 @@ function do_get(sto::Store{T}, get_ev::Get, key::StoreGetKey) where {T}
6666
end
6767
true
6868
end
69+
70+
"""
71+
isready(::Store)
72+
73+
Returns `true` if the store is not empty, similarly to the meaning of `isready` for `Base.Channel`.
74+
75+
```jldoctest
76+
julia> sim = Simulation(); store = Store{Symbol}(sim); isready(store)
77+
false
78+
79+
julia> put!(store, :message); isready(store)
80+
true
81+
```
82+
"""
83+
isready(sto::Store) = sto.load > 0
84+
85+
"""
86+
islocked(::Store)
87+
88+
Returns `true` if the store is full, similarly to the meaning of `islocked` for `Base.ReentrantLock`.
89+
90+
```jldoctest
91+
julia> sim = Simulation(); store = Store{Symbol}(sim; capacity=1); islocked(store)
92+
false
93+
94+
julia> put!(store, :message); islocked(store)
95+
true
96+
```
97+
"""
98+
islocked(sto::Store) = sto.load==sto.capacity
99+
100+
unlock(::Store) = error("There is no well defined way to \"unlock\" a store. Instead of attempting `unlock` consider using `pop!(::Store)` or use a `Resource` instead of a `Store`.")
101+
lock(::Store) = error("There is no well defined way to \"lock\" a store. Instead of attempting `lock` consider using `put!(::Store, ...)` or use a `Resource` instead of a `Store`.")

0 commit comments

Comments
 (0)