GPhpThreadCriticalSection
in package
Critical section for sharing data between multiple processes.
It provides a slower data-safe manipulating methods and unsafe faster methods.
Tags
Table of Contents
Methods
- __construct() : mixed
- Constructor.
- __destruct() : mixed
- Destructor.
- addOrUpdateResource() : bool
- Adds or updates shared resource in a reliable, slower way. A lock of the critical section is required.
- addOrUpdateUnrelResource() : bool
- Adds or updates shared resource in an unreliable, faster way. A lock of the critical section is NOT required.
- cleanPipeGarbage() : int
- Cleans pipe files garbage left from any ungracefully terminated instances.
- dispatch() : void
- Dispatcher responsible for the thread intercommunication and communication with their parent process.
- finalize() : void
- Finalization of a thread instance that ended and soon will be destroyed.
- getResourceNames() : array<string|int, mixed>
- Returns the names of all reliable shared resources.
- getResourceValue() : mixed
- Returns an reliable resource value by asking the dispatcher for it. An ownership of the critical section is required.
- getResourceValueFast() : mixed
- Returns an reliable resource without trying to ask for it the dispatcher.
- getUnrelResourceNames() : array<string|int, mixed>
- Returns the names of all unreliable shared resources.
- getUnrelResourceValue() : mixed
- Returns an unreliable resource value by asking the dispatcher for it. An ownership of the critical section is required.
- getUnrelResourceValueFast() : mixed
- Returns an unreliable resource without trying to ask for it the dispatcher.
- initialize() : bool
- Initializes the critical section.
- lock() : bool
- Tries to lock the associated with the instance critical section.
- removeResource() : bool
- Removes shared resource in a reliable, slower way. A lock of the critical section is required.
- removeUnrelResource() : bool
- Removes shared resource in an unreliable, faster way. A lock of the critical section is NOT required.
- unlock() : bool
- Tries to unlock the associated with the instance critical section.
- dataDispatch() : void
- Operations on the transferred data dispatch helper.
- decodeMessage() : void
- Decodes encoded from GPhpThread's instance message.
- doIOwnIt() : bool
- Confirms if the current thread has the ownership of the critical section associated with it.
- encodeMessage() : string
- Encodes data in a message that will be sent to the thread process dispatcher.
- isIntercomBroken() : bool
- Checks if the internal intercom is broken.
- isPidAlive() : bool
- Checks if specific process id is still alive.
- receive() : bool
- Receives data operation from the main process dispatcher.
- requestLock() : bool
- Tries to lock the critical section.
- requestUnlock() : bool
- Tries to unlock the critical section.
- send() : bool
- Sends data operation to the main process dispatcher.
- sortByLockAndDispatchPriority() : int
- Sort by occurred lock and dispatch priority. This is a workaround method required for PHP 5.3 and relies on an initialized $bindVariable inside this class.
- sortByLockDispatchPriorityAndMostThreadsInside() : int
- Sort by occurred lock, dispatch priority and most threads using the critical section. This is workaround method required for PHP 5.3 and relies on an initialized $bindVariable inside this class.
- updateDataContainer() : bool
- Executes data operation on the internal shared data container.
Methods
__construct()
Constructor.
public
__construct([string $pipeDirectory = '/dev/shm' ]) : mixed
Parameters
- $pipeDirectory : string = '/dev/shm'
-
The directory where the pipe files for the inter-process communication will be stored.
__destruct()
Destructor.
public
__destruct() : mixed
addOrUpdateResource()
Adds or updates shared resource in a reliable, slower way. A lock of the critical section is required.
public
addOrUpdateResource(string $name, mixed $value) : bool
Parameters
- $name : string
-
The name of the resource.
- $value : mixed
-
The value of the resource.
Return values
bool —Returns true on success otherwise returns false.
addOrUpdateUnrelResource()
Adds or updates shared resource in an unreliable, faster way. A lock of the critical section is NOT required.
public
addOrUpdateUnrelResource(string $name, mixed $value) : bool
Parameters
- $name : string
-
The name of the resource.
- $value : mixed
-
The value of the resource.
Return values
bool —Returns true on success otherwise returns false.
cleanPipeGarbage()
Cleans pipe files garbage left from any ungracefully terminated instances.
public
cleanPipeGarbage() : int
Return values
int —The total number of unused, cleaned pipe garbage files.
dispatch()
Dispatcher responsible for the thread intercommunication and communication with their parent process.
public
static dispatch([bool $useBlocking = false ]) : void
Parameters
- $useBlocking : bool = false
-
On true blocks the internal execution until communication data is available for the current dispatched thread otherwise it skips it.
finalize()
Finalization of a thread instance that ended and soon will be destroyed.
public
finalize(int $threadId) : void
Parameters
- $threadId : int
-
The internal thread identifier.
getResourceNames()
Returns the names of all reliable shared resources.
public
getResourceNames() : array<string|int, mixed>
Return values
array<string|int, mixed> —An array of (0 => 'resource name1', 1 => 'resource name2', ...)
getResourceValue()
Returns an reliable resource value by asking the dispatcher for it. An ownership of the critical section is required.
public
getResourceValue(string $name) : mixed
Parameters
- $name : string
-
The name of the desired resource.
Tags
Return values
mixed —The resource value or null if the resource was not found.
getResourceValueFast()
Returns an reliable resource without trying to ask for it the dispatcher.
public
getResourceValueFast(string $name) : mixed
Parameters
- $name : string
-
The name of the resource.
Return values
mixed —Returns the resource value or null on failure or if the resource name was not found.
getUnrelResourceNames()
Returns the names of all unreliable shared resources.
public
getUnrelResourceNames() : array<string|int, mixed>
Return values
array<string|int, mixed> —An array of (0 => 'resource name1', 1 => 'resource name2', ...)
getUnrelResourceValue()
Returns an unreliable resource value by asking the dispatcher for it. An ownership of the critical section is required.
public
getUnrelResourceValue(string $name) : mixed
Parameters
- $name : string
-
The name of the desired resource.
Tags
Return values
mixed —The resource value or null if the resource was not found.
getUnrelResourceValueFast()
Returns an unreliable resource without trying to ask for it the dispatcher.
public
getUnrelResourceValueFast(string $name) : mixed
Parameters
- $name : string
-
The name of the resource.
Return values
mixed —Returns the resource value or null on failure or if the resource name was not found.
initialize()
Initializes the critical section.
public
initialize(int $afterForkPid, int $threadId) : bool
Parameters
- $afterForkPid : int
-
The process identifier obtained after the execution of a fork() used to differentiate between different processes - pseudo threads.
- $threadId : int
-
The internal to the GPhpThread identifier of the current thread instance which is unique identifier in the context of the current process.
Return values
bool —On success returns true otherwise false.
lock()
Tries to lock the associated with the instance critical section.
public
lock([bool $useBlocking = true ]) : bool
Parameters
- $useBlocking : bool = true
-
If it's set to true the method will block until a lock is successfully established.
Return values
bool —Returns true on success otherwise returns false.
removeResource()
Removes shared resource in a reliable, slower way. A lock of the critical section is required.
public
removeResource(string $name) : bool
Parameters
- $name : string
-
The name of the resource.
Return values
bool —Returns true on success otherwise returns false.
removeUnrelResource()
Removes shared resource in an unreliable, faster way. A lock of the critical section is NOT required.
public
removeUnrelResource(string $name) : bool
Parameters
- $name : string
-
The name of the resource.
Return values
bool —Returns true on success otherwise returns false.
unlock()
Tries to unlock the associated with the instance critical section.
public
unlock([bool $useBlocking = false ]) : bool
Parameters
- $useBlocking : bool = false
-
If it's set to true the method will block until an unlock is successfully established.
Return values
bool —Returns true on success otherwise returns false.
dataDispatch()
Operations on the transferred data dispatch helper.
private
static dataDispatch(GPhpThreadCriticalSection &$inst, int $threadId) : void
Parameters
- $inst : GPhpThreadCriticalSection
-
The instance of the critical section to work with
- $threadId : int
-
The identifier of the thread whose critical section is currently dispatched.
decodeMessage()
Decodes encoded from GPhpThread's instance message.
private
decodeMessage(string $encodedMsg, string &$msg, int &$pid, string &$name, mixed &$value) : void
Parameters
- $encodedMsg : string
-
The encoded message
- $msg : string
-
The encoded message type identifier. REFERENCE type.
- $pid : int
-
The process id of the sender. REFERENCE type.
- $name : string
-
The variable name whose data was sent. REFERENCE type.
- $value : mixed
-
The variable data contained in the encoded message. REFERENCE type.
doIOwnIt()
Confirms if the current thread has the ownership of the critical section associated with it.
private
doIOwnIt() : bool
Return values
boolencodeMessage()
Encodes data in a message that will be sent to the thread process dispatcher.
private
encodeMessage(string $msg, string $name, mixed $value) : string
Parameters
- $msg : string
-
The message type identifier.
- $name : string
-
The variable name whose data is going to be sent.
- $value : mixed
-
The current value of the desired for sending variable.
Return values
string —The composed message string
isIntercomBroken()
Checks if the internal intercom is broken.
private
isIntercomBroken() : bool
Return values
bool —Returns true if the intercom is broken otherwise returns false.
isPidAlive()
Checks if specific process id is still alive.
private
isPidAlive(int $pid) : bool
Parameters
- $pid : int
-
The process identifier.
Return values
bool —Returns true if the pid is alive otherwise returns false.
receive()
Receives data operation from the main process dispatcher.
private
receive(string &$message, int &$pid, string &$resourceName, mixed &$resourceValue[, int $timeInterval = 700 ]) : bool
Parameters
- $message : string
-
The operation type code. REFERENCE type.
- $pid : int
-
The process id of the sender "thread". REFERENCE type.
- $resourceName : string
-
The name of resource that is holding a particular data that will be "shared". REFERENCE type.
- $resourceValue : mixed
-
The value of the resource. REFERENCE type.
- $timeInterval : int = 700
-
Internal wait time interval in milliseconds between each data check.
Return values
bool —Returns true on success otherwise false.
requestLock()
Tries to lock the critical section.
private
requestLock() : bool
Return values
bool —Returns true on success otherwise false.
requestUnlock()
Tries to unlock the critical section.
private
requestUnlock() : bool
Return values
bool —Returns true on success otherwise false.
send()
Sends data operation to the main process dispatcher.
private
send(string $operation, string $resourceName, mixed $resourceValue) : bool
Parameters
- $operation : string
-
The operation type code.
- $resourceName : string
-
The name of resource that is holding a particular data that will be "shared".
- $resourceValue : mixed
-
The value of the resource.
Return values
bool —Returns true on success otherwise false.
sortByLockAndDispatchPriority()
Sort by occurred lock and dispatch priority. This is a workaround method required for PHP 5.3 and relies on an initialized $bindVariable inside this class.
private
sortByLockAndDispatchPriority(mixed $a, mixed $b) : int
Parameters
- $a : mixed
-
The first key to be taken into account.
- $b : mixed
-
The second key to be taken into account.
Return values
int —-1, 0 or 1 depending on which key is more preferable.
sortByLockDispatchPriorityAndMostThreadsInside()
Sort by occurred lock, dispatch priority and most threads using the critical section. This is workaround method required for PHP 5.3 and relies on an initialized $bindVariable inside this class.
private
static sortByLockDispatchPriorityAndMostThreadsInside(mixed $a, mixed $b) : int
Parameters
- $a : mixed
-
The first key to be taken into account.
- $b : mixed
-
The second key to be taken into account.
Return values
int —-1, 0 or 1 depending on which key is more preferable.
updateDataContainer()
Executes data operation on the internal shared data container.
private
updateDataContainer(string $actionType, string $name, mixed $value) : bool
Parameters
- $actionType : string
-
The performed operation code.
- $name : string
-
The name of the resource.
- $value : mixed
-
The value of the resource.
Return values
bool —Returns true on success otherwise false.