Note: This class is not available with the PO_NO_THREAD_CLASSES parse option.
The Gate class implements a reentrant thread lock. Once a thread grabs the lock, it can call the Gate::enter() method again without blocking. Other threads that try to enter the lock will block until the thread holding the lock calls Gate::exit() an equal number of times to Gate::enter() calls.
See the AutoGate class for a class that assists in exception-safe Gate locking.
Additionally, the on_exit statement can provide exception-safe Gate handling at the lexical block level as in the following example:
{
$g.enter();
on_exit
$g.exit();
# ... when this block exits the gate lock counter will be decremented,
# even in the case of return statements or exceptions
}Table 4.786. Gate Method Overview
Method | Except? | Description |
|---|---|---|
N | Create a new Gate object. | |
Y | Destroys the Gate object. | |
N | Creates a new Gate object, not based on the original. | |
| Y | Acquires the lock if it is unlocked or locked by the same thread, otherwise blocks until the lock counter reaches zero. For variants taking a timeout; returns 0 if no timeout occurred, non-zero if a timeout occurred. |
N | Acquires the lock if it is unlocked or locked by the same thread, otherwise returns immediately with -1. | |
Y | Decrements the lock counter; if it reaches zero then the lock is unlocked and any blocked threads are awoken; in this case 0 is returns; in all other cases, non-zero is returned. | |
N | Returns the current lock count. | |
N | Returns the number of threads blocked on the Gate. |
Creates a new Gate object.
my Gate $gate();Destroys the object. Note that it is a programming error to delete this object while other threads are blocked on it; in this case an exception is thrown in the deleting thread, and in each thread blocked on this object when it is deleted.
delete $gate;Table 4.787. Exceptions Thrown by Gate::destructor()
err | desc |
|---|---|
| Object deleted while other threads blocked on it. |
Creates a new Gate object, not based on the original.
my Gate $new_gate = $gate.copy();Acquires the lock if it is unlocked or locked by the same thread, otherwise blocks until the lock counter reaches zero. An optional timeout value may be passed to this method, giving a time in milliseconds to wait for the lock to become free. Like all Qore functions and methods taking timeout values, a relative time value may be passed instead of an integer to make the timeout units clear.
intGate::enter(timeout$timeout_ms)
$gate.enter();
Table 4.788. Arguments for Gate::enter()
Argument | Description |
|---|---|
| An optional timeout value to wait to acquire the lock; integers are interpreted as milliseconds; relative date/time values are interpreted literally. |
Table 4.790. Exceptions Thrown by Gate::enter()
err | desc |
|---|---|
| A deadlock was detected while trying to acquire the lock. |
| object deleted in another thread |
Acquires the lock if it is unlocked or locked by the same thread, otherwise returns immediately.
my int $rc = $gate.tryEnter();Table 4.791. Return Values for Gate::tryEnter()
Return Type | Description |
|---|---|
Returns 0 for success (acquired the lock) or -1 for failure (would block). |
Decrements the lock counter; if it reaches zero then the lock is unlocked and any blocked threads are awoken.
$gate.exit();
Table 4.792. Exceptions Thrown by Gate::exit()
err | desc |
|---|---|
| lock not held by this thread, object deleted in another thread |
Returns the current lock count.
my int $num = $gate.numInside();Table 4.793. Return Values for Gate::numInside()
Return Type | Description |
|---|---|
Returns the current lock count. |
There are no comments yet