@include <SharedArray>

The SharedArray class provides an array implementation that shares its state across multiple instances. This class is useful for scenarios in which we’re forking processes and need a shared array between them.

Reads and writes are atomic and concurrency-safe.

The array can be encrypted for security.

Internally, it uses the file system to store the array state.

Methods

@SharedArray.size

Scope: public

Echoes the number of elements in the shared array.

Usage: @SharedArray.size

@SharedArray.push

Scope: public

Adds a value to the end of the shared array.

Usage: @SharedArray.push $value

Parameters

  • value
    • Type: primitive
    • The value to add to the shared array.

@SharedArray.pop

Scope: public

Removes and echoes the last element of the shared array.

Usage: @SharedArray.pop

@SharedArray.insert

Scope: public

Inserts a value at a specific index in the shared array.

Usage: @SharedArray.insert $index $value

Parameters

  • index
    • Type: primitive
    • The index at which to insert the value.
  • value
    • Type: primitive
    • The value to insert into the shared array.

@SharedArray.remove

Scope: public

Removes the value at a specific index from the shared array.

Usage: @SharedArray.remove $index

Parameters

  • index
    • Type: primitive
    • The index of the value to remove.

@SharedArray.at

Scope: public

Gets the value at a specific index in the shared array.

Usage: @SharedArray.at $index

Parameters

  • index
    • Type: primitive
    • The index of the value to retrieve.

@SharedArray.front

Scope: public

Echoes the first element of the shared array.

Usage: @SharedArray.front

@SharedArray.back

Scope: public

Echoes the last element of the shared array.

Usage: @SharedArray.back

@SharedArray.all

Scope: public

Echoes all elements of the shared array.

Usage: @SharedArray.all

@SharedArray.empty

Scope: public

Echoes “true” if the shared array is empty, “false” otherwise.

Usage: @SharedArray.empty

@SharedArray.slice

Scope: public

Returns a slice of the shared array from start to end indices.

Usage: @SharedArray.slice $start $end

Parameters

  • start
    • Type: primitive
    • The starting index of the slice (inclusive).
  • end
    • Type: primitive
    • The ending index of the slice (inclusive).

@SharedArray.clear

Scope: public

Removes all elements from the shared array.

Usage: @SharedArray.clear

@SharedArray.setEncrypted

Scope: public

Sets whether the object should be encrypted. Encrypting the object can mitigate unauthorized reads by other processes.

This cannot be changed after the object has been used.

Usage: @SharedArray.setEncrypted $value

Parameters

  • value
    • Type: primitive
    • ‘1’ or ‘true’ enables encryption, any other value disables it.

@SharedArray.lock

Scope: public

Locks the shared object for exclusive access.

Once locked, only the process which locked it can access the object until it is unlocked.

Attempts by other processes access the object while it is locked will block until it is unlocked.

Using this method (as opposed to the internal _lock method) sets the extended_lock flag to 1, indicating that the lock should not be dropped until explicitly unlocked. I.e., subsequent calls to _unlock (internal) will not release the lock until unlock (public) is called.

Usage: @SharedArray.lock

@SharedArray.unlock

Scope: public

Unlocks the shared object, allowing other processes to access it.

This also resets the extended_lock flag to 0.

Usage: @SharedArray.unlock