Object built-in object

ObjectSemaphore

fiber semaphore object

The semaphore object manages an internal counter, which is decremented by one after calling acquire or wait, and incremented by one after calling release or post. The counter will not decrease to a negative number, because when acquire and wait find that the value is 0, they will sleep the current fiber until other fibers increase the counter value through release or post.

Commonly used occasions for semaphores are to limit the concurrent use of resources and the application of the producer/consumer model.

Take database requests as an example to limit the concurrent use of resources:

1 2 3 4 5 6 7 8 9 10
var maxconnections = 5; var l = new coroutine.Semaphore(maxconnections); ...... l.acquire(); var conn = connectdb() ..... conn.close(); l.release();

The producer/consumer pattern usually uses semaphores with queues. The producer adds data to the queue and posts a signal. The consumer waits for the signal first, gets the signal, and then goes to the queue to query the data.

inheritance relationship

Constructor

Semaphore

semaphore constructor

1
new Semaphore(Integer value = 1);

Call parameters:

  • value: Integer, initial value of counter

member function

wait

wait for a semaphore

1
Boolean Semaphore.wait(Integer timeout = -1);

Call parameters:

  • timeout: Integer, specifies the timeout in milliseconds. The default is -1, which means it will never timeout.

Return results:

  • Boolean, returns true if the acquisition is successful, returns false if it times out.

post

Release a semaphore, equivalent to release()

1
Semaphore.post();

trywait

Try to acquire a signal. If it cannot be acquired, it will return immediately and return false, which is equivalent to acquire(false)

1
Boolean Semaphore.trywait();

Return results:

  • Boolean, returns true if the acquisition is successful, returns false if it times out.

acquire

Get ownership of the lock

1
Boolean Semaphore.acquire(Boolean blocking = true);

Call parameters:

  • blocking: Boolean, specifies whether to wait, wait when true, default is true

Return results:

  • Boolean, returns whether the lock was successfully acquired, true indicates successful acquisition.

The acquire method is used to obtain ownership of the lock. When the lock is in the acquireable state, this method returns true immediately.

When the lock cannot be acquired and blocking is true, the current fiber goes to sleep. When other fibers release the lock, this method returns true.

When the lock cannot be acquired and blocking is false, the method returns false.


release

Release ownership of the lock

1
Semaphore.release();

This method will release ownership of the lock or throw an error if the current fiber does not own the lock.


count

Query the current number of waiting tasks

1
Integer Semaphore.count();

Return results:

  • Integer, returns the number of tasks

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String Semaphore.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

1
Value Semaphore.toJSON(String key = "");

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable