Resources

Shared resources with limited capacity are often needed in simulations.

  1. One approach to model them, is to use Julia Channels with their API. This is thread-safe and thus should be preferred for multithreading applications.
  2. Using Resource is a second possibility to model shared resources. Its interface gives more flexibility and is faster in single threaded applications, but in multithreading the user must avoid race conditions by explicitly wrapping access with lock -… access …- unlock – if the resources are shared by multiple tasks.
DiscreteEvents.ResourceType
Resource{T}(capacity)

A Resource implements a Deque with a limited capacity. If used in multithreading applications, the user must avoid race conditions by explicitly wrapping modifying calls with lock-unlock.

Fields

  • items::Deque{T}: resource buffer
  • capacity::Number=Inf: the capacity is limited to the given integer,
  • lock::ReentrantLock: a lock for coordinating resource access by tasks.

Example

julia> 
Note

In order to use the full interface to Resource you have to load DataStructures.

DataStructures.capacityFunction
capacity(r::Resource)

Get the capacity of a resource

capacity(ch::Channel)

Get the capacity of a channel.

DataStructures.isfullFunction
isfull(r::Resource)

Test whether the resource is full

isfull(ch::Channel)

Test whether a channel is full.

Base.isreadyFunction
isready(r::Resource)

Test whether an item is available.

Base.isemptyFunction
isempty(r::Resource)

Test whether the resource is empty.

Base.empty!Function
empty!(r::Resource)

Reset the resource buffer (deque).

empty!(ch::Channel)

Reset a channel, throw away the elements stored to it.

Base.lengthFunction
length(r::Resource)

Get the number of elements available.

length(ch::Channel)

Get the number of items in a channel.

Base.push!Function
push!(r::Resource, x)

Add an element to the back of a resource deque.

Base.pop!Function
pop!(r::Resource)

Remove an element from the back of a resource deque.

Base.pushfirst!Function
pushfirst!(r::Resource, x)

Add an element to the front of a resource deque.

Base.popfirst!Function
popfirst!(r::Resource)

Remove an element from the front of a resource deque.

Base.firstFunction
first(r::Resource)

Get the element at the front of a resource deque.

Base.lastFunction
last(r::Resource)

Get the element at the back of a resource deque.

Resource provides a lock-unlock API for multithreading applications.

Base.lockFunction
lock(r::Resource)

Acquire the resource lock when it becomes available. If the lock is already locked by a different task/thread, wait for it to become available.

Each lock must be matched by an unlock.

Base.unlockFunction
unlock(r::Resource)

Releases ownership of the resource lock.

If this is a recursive lock which has been acquired before, decrement an internal counter and return immediately.

Base.islockedFunction
islocked(r::Resource)

Check whether the lock is held by any task/thread. This should not be used for synchronization (see instead trylock).

Base.trylockFunction
trylock(r::Resource)

Acquire the resource lock if it is available, and return true if successful. If the lock is already locked by a different task/thread, return false.

Each successful trylock must be matched by an unlock.