Skip to content

Commit bc80081

Browse files
committed
implement trylock
1 parent 8e8aa93 commit bc80081

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## v1.0.1 - dev
44

5-
- Start using `Base`'s API: `lock`, `unlock`, `islocked`, `isready`, `get!` and deprecate `get`, `request`, `release`.
5+
- Start using `Base`'s API: `lock`, `trylock`, `unlock`, `islocked`, `isready`, `get!` and deprecate `get`, `request`, `release`.
66
## v1.0.0 - 2023-05-03
77

88
- Rename from SimJulia.jl to ConcurrentSim.jl

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!, isready, islocked, unlock, lock, &, |
10+
import Base: run, isless, show, yield, get, put!, isready, islocked, unlock, lock, trylock, &, |
1111
import Dates: now
1212

1313
export AbstractEvent, Environment, value, state, environment

src/resources/containers.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@ end
3232

3333
lock(res::Resource; priority::Int=0) = put!(res, 1; priority=priority)
3434

35+
"""
36+
trylock(res::Resource)
37+
38+
If the Resource is not locked, locks it and return the lock event.
39+
Returns `false` if the Resource is locked, similarly to the meaning of `trylock` for `Base.ReentrantLock`.
40+
41+
```jldoctest
42+
julia> sim = Simulation(); res = Resource(sim);
43+
44+
julia> ev = trylock(res)
45+
ConcurrentSim.Put 1
46+
47+
julia> typeof(ev)
48+
ConcurrentSim.Put
49+
50+
julia> trylock(res)
51+
false
52+
```
53+
"""
54+
function trylock(res::Resource; priority::Int=0)
55+
islocked(res) && return false # TODO check priority
56+
lock(res; priority)
57+
end
58+
3559
function get(con::Container{N}, amount::N; priority::Int=0) where N<:Real
3660
get_ev = Get(con.env)
3761
con.get_queue[get_ev] = ContainerKey(priority, con.seid+=one(UInt), amount)

src/resources/stores.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,4 @@ islocked(sto::Store) = sto.load==sto.capacity
102102

103103
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`.")
104104
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`.")
105+
trylock(::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`.")

test/test_resources_stores.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ run(sim)
3333

3434
@test_throws ErrorException unlock(sto)
3535
@test_throws ErrorException lock(sto)
36+
@test_throws ErrorException trylock(sto)

0 commit comments

Comments
 (0)