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.972. 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. | |
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. | |
N | Returns the current lock count. | |
N | Returns the number of threads blocked on the Gate. |
Creates a new Gate object.
new Gate()
$gate = new Gate();
Table 4.973. Arguments for Gate::constructor()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.974. Return Values for Gate::constructor()
Return Type | Description |
---|---|
Gate Object | The new object created. |
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 lvalue
delete $gate;
Table 4.975. 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.
Gate::copy()
$new_gate = $gate.copy();
Table 4.977. Return Values for Gate::copy()
Return Type | Description |
---|---|
Gate Object | A new Gate object, not based on the original. |
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.
Gate::enter([timeout_ms]
)
$gate.enter();
Table 4.978. Arguments for Gate::enter()
Argument | Type | Description |
---|---|---|
| Integer or Relative Date/Time | An optional timeout value in milliseconds for acquiring the lock. |
Table 4.979. Return Values for Gate::enter()
Return Type | Description |
---|---|
Integer | 0 for lock acquired, -1 for timeout (only when a timeout argument is passed) |
Table 4.980. 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.
Gate::tryEnter([timeout_ms]
)
$gate.tryEnter();
Table 4.981. Arguments for Gate::tryEnter()
Argument | Type | Description |
---|---|---|
| Integer or Relative Date/Time | An optional timeout value in milliseconds for acquiring the lock. |
Table 4.982. Return Values for Gate::tryEnter()
Return Type | Description |
---|---|
Integer | 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()
$gate.exit();
Table 4.985. Exceptions Thrown by Gate::exit()
err | desc |
---|---|
| lock not held by this thread, object deleted in another thread |
Returns the current lock count.
Gate::numInside()
$num = $gate.numInside();
Table 4.986. Arguments for Gate::numInside()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.987. Return Values for Gate::numInside()
Return Type | Description |
---|---|
Integer | Returns the current lock count. |
Returns the number of threads blocked on this object.
Gate::numWaiting()
$num = $gate.numWaiting();
Table 4.988. Arguments for Gate::numWaiting()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.989. Return Values for Gate::numWaiting()
Return Type | Description |
---|---|
Integer | Returns the number of threads blocked on this object. |