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.

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

Reset the resource buffer (deque).

source
empty!(ch::Channel)

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

source
Base.lengthFunction
length(r::Resource)

Get the number of elements available.

source

length(ch::Channel)

Get the number of items in a channel.

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

Add an element to the back of a resource deque.

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

Remove an element from the back of a resource deque.

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

Add an element to the front of a resource deque.

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

Remove an element from the front of a resource deque.

source
Base.firstFunction
first(r::Resource)

Get the element at the front of a resource deque.

source
Base.lastFunction
last(r::Resource)

Get the element at the back of a resource deque.

source

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.

source
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.

source
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).

source
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.

source