You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/guides/blockingandyielding.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ In particular, the `ConcurrentSim.jl` package uses the async "coroutines" model
21
21
Without further ado, here is the typical API used with:
22
22
23
23
-`ConcurrentSim.Resource` which is used to represent scarce resource that can be used by only up to a fixed number of tasks. If the limit is just one task (the default), this is very similar to `Base.ReentrantLock`. `Resource` is a special case of `Container` with an integer "resource counter".
24
-
-`ConcurrentSim.Store` which is used to represent a FILO stack.
24
+
-`ConcurrentSim.Store` which is used to represent an unordered heap. For the ordered versions, consider `QueueStore` or `StackStore`.
A store in which items are stored in a FILO order.
5
+
6
+
```jldoctest
7
+
julia> sim = Simulation()
8
+
store = Store{Symbol}(sim)
9
+
stack = StackStore{Symbol}(sim)
10
+
items = [:a,:b,:a,:c];
11
+
12
+
julia> [put!(store, item) for item in items];
13
+
14
+
julia> [value(take!(store)) for _ in 1:length(items)]
15
+
4-element Vector{Symbol}:
16
+
:a
17
+
:a
18
+
:b
19
+
:c
20
+
21
+
julia> [put!(stack, item) for item in items];
22
+
23
+
julia> [value(take!(stack)) for _ in 1:length(items)]
24
+
4-element Vector{Symbol}:
25
+
:c
26
+
:a
27
+
:b
28
+
:a
29
+
```
30
+
31
+
See also: [`QueueStore`](@ref), [`Store`](@ref)
32
+
"""
33
+
const StackStore = Store{N, T, DataStructures.Stack{N}} where {N, T<:Number}
34
+
StackStore{N}(env::Environment; capacity=typemax(UInt)) where {N} =StackStore{N, Int}(env; capacity)
35
+
36
+
"""
37
+
QueueStore{N, T<:Number}
38
+
39
+
A store in which items are stored in a FIFO order.
40
+
41
+
```jldoctest
42
+
julia> sim = Simulation()
43
+
store = Store{Symbol}(sim)
44
+
queue = QueueStore{Symbol}(sim)
45
+
items = [:a,:b,:a,:c];
46
+
47
+
julia> [put!(store, item) for item in items];
48
+
49
+
julia> [value(take!(store)) for _ in 1:length(items)]
50
+
4-element Vector{Symbol}:
51
+
:a
52
+
:a
53
+
:b
54
+
:c
55
+
56
+
julia> [put!(queue, item) for item in items];
57
+
58
+
julia> [value(take!(queue)) for _ in 1:length(items)]
59
+
4-element Vector{Symbol}:
60
+
:a
61
+
:b
62
+
:a
63
+
:c
64
+
```
65
+
66
+
See also: [`StackStore`](@ref), [`Store`](@ref)
67
+
"""
68
+
const QueueStore = Store{N, T, DataStructures.Queue{N}} where {N, T<:Number}
69
+
QueueStore{N}(env::Environment; capacity=typemax(UInt)) where {N} =QueueStore{N, Int}(env; capacity)
70
+
71
+
functiondo_put(sto::StackStore{N, T}, put_ev::Put, key::StorePutKey{N, T}) where {N, T<:Number}
72
+
if sto.load < sto.capacity
73
+
sto.load +=one(UInt)
74
+
push!(sto.items, key.item)
75
+
schedule(put_ev)
76
+
end
77
+
false
78
+
end
79
+
80
+
functiondo_get(sto::StackStore{N, T}, get_ev::Get, key::StoreGetKey{T}) where {N, T<:Number}
81
+
key.filter !== get_any_item &&error("Filtering not supported for `StackStore`. Use an unordered store instead, or submit a feature request for implementing filtering to our issue tracker.")
82
+
item =pop!(sto.items)
83
+
sto.load -=one(UInt)
84
+
schedule(get_ev; value=item)
85
+
true
86
+
end
87
+
88
+
functiondo_put(sto::QueueStore{N, T}, put_ev::Put, key::StorePutKey{N, T}) where {N, T<:Number}
89
+
if sto.load < sto.capacity
90
+
sto.load +=one(UInt)
91
+
enqueue!(sto.items, key.item)
92
+
schedule(put_ev)
93
+
end
94
+
false
95
+
end
96
+
97
+
functiondo_get(sto::QueueStore{N, T}, get_ev::Get, key::StoreGetKey{T}) where {N, T<:Number}
98
+
key.filter !== get_any_item &&error("Filtering not supported for `QueueStore`. Use an unordered store instead, or submit a feature request for implementing filtering to our issue tracker.")
A store is a resource that can hold a number of items of type `N` in a FILO stack. It is similar to a `Base.Channel` with a finite capacity ([`put!`](@ref) blocks after reaching capacity).
16
+
A store is a resource that can hold a number of items of type `N`. It is similar to a `Base.Channel` with a finite capacity ([`put!`](@ref) blocks after reaching capacity).
17
17
The [`put!`](@ref) and [`take!`](@ref) functions are a convenient way to interact with such a "channel" in a way mostly compatible with other discrete event and concurrency frameworks.
18
18
19
19
See [`Container`](@ref) for a more lock-like resource.
20
20
21
21
Think of `Resource` and `Container` as locks and of `Store` as channels/stacks. They block only if empty (on taking) or full (on storing).
22
22
23
+
`Store` does not guarantee any order of items. See [`StackStore`](@ref) and [`QueueStore`](@ref) for ordered variants.
24
+
23
25
```jldoctest
24
26
julia> sim = Simulation(); store = Store{Int}(sim);
0 commit comments