4.30. Thread::Gate Class

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

Gate::constructor()

N

Create a new Gate object.

Gate::destructor()

Y

Destroys the Gate object.

Gate::copy()

N

Creates a new Gate object, not based on the original.

nothingGate::enter()

intGate::enter(timeout$timeout_ms)

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.

intGate::tryEnter()

N

Acquires the lock if it is unlocked or locked by the same thread, otherwise returns immediately with -1.

intGate::exit()

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.

intGate::numInside()

N

Returns the current lock count.

intGate::numWaiting()

N

Returns the number of threads blocked on the Gate.


4.30.1. Gate::constructor()

Synopsis

Creates a new Gate object.

Prototype

Gate::constructor()

Example
my Gate $gate();

4.30.2. Gate::destructor()

Synopsis

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.

Example
delete $gate;

Table 4.787. Exceptions Thrown by Gate::destructor()

err

desc

LOCK-ERROR

Object deleted while other threads blocked on it.


4.30.3. Gate::copy()

Synopsis

Creates a new Gate object, not based on the original.

Example
my Gate $new_gate = $gate.copy();

4.30.4. Gate::enter()

Synopsis

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.

Prototype

nothingGate::enter()

intGate::enter(timeout$timeout_ms)

Example
$gate.enter();

Table 4.788. Arguments for Gate::enter()

Argument

Description

[timeout$timeout_ms]

An optional timeout value to wait to acquire the lock; integers are interpreted as milliseconds; relative date/time values are interpreted literally.


Table 4.789. Return Values for Gate::enter()

Return Type

Description

nothing or int

Only variants accepting a timeout return a value; they return 0 for success, -1 for timeout


Table 4.790. Exceptions Thrown by Gate::enter()

err

desc

THREAD-DEADLOCK

A deadlock was detected while trying to acquire the lock.

LOCK-ERROR

object deleted in another thread


4.30.5. Gate::tryEnter()

Synopsis

Acquires the lock if it is unlocked or locked by the same thread, otherwise returns immediately.

Prototype

intGate::tryEnter()

Example
my int $rc = $gate.tryEnter();

Table 4.791. Return Values for Gate::tryEnter()

Return Type

Description

int

Returns 0 for success (acquired the lock) or -1 for failure (would block).


4.30.6. Gate::exit()

Synopsis

Decrements the lock counter; if it reaches zero then the lock is unlocked and any blocked threads are awoken.

Prototype

intGate::exit()

Example
$gate.exit();

Table 4.792. Exceptions Thrown by Gate::exit()

err

desc

LOCK-ERROR

lock not held by this thread, object deleted in another thread


4.30.7. Gate::numInside()

Synopsis

Returns the current lock count.

Prototype

intGate::numInside()

Example
my int $num = $gate.numInside();

Table 4.793. Return Values for Gate::numInside()

Return Type

Description

int

Returns the current lock count.


4.30.8. Gate::numWaiting()

Synopsis

Returns the number of threads blocked on this object.

Prototype

intGate::numWaiting()

Example
my int $num = $gate.numWaiting();

Table 4.794. Return Values for Gate::numWaiting()

Return Type

Description

int

Returns the number of threads blocked on this object.


There are no comments yet

Leave a Comment



?
? ?
?

 
Powered by TalkBack