Object built-in object

Object Lock

Fiber lock object

Unlike operating system locks, fiber locks are implemented purely logically, and the load of locking and unlocking is small

1 2 3 4
var l = new coroutine.Lock(); l.acquire(); ..... l.release();

Inheritance

#lineWidth: 1.5 #font: Helvetica,sans-Serif #fontSize: 10 #leading: 1.6 #.this: fill=lightgray #.class: fill=white [<class>object|toString();toJSON()] [<this>Lock|new Lock()|acquire();release();count()] [<class>Condition] [<class>Event] [<class>Semaphore] [object] <:- [Lock] [Lock] <:- [Condition] [Lock] <:- [Event] [Lock] <:- [Semaphore] Lock new Lock() acquire() release() count() object toString() toJSON() Condition Event Semaphore

Constructor

Lock

Constructor

1
new Lock();

Member function

acquire

Acquire ownership of the lock

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

Call parameters:

  • blocking: Boolean, specify whether to wait, if it is true, wait, the default is true

Return result:

  • Boolean, Returns whether the lock is successfully acquired, true means the acquisition is successful

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

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

When the lock is not available and blocking is false, the method returns false.


release

Release lock ownership

1
Lock.release();

This method will release the ownership of the lock. If the current fiber does not own the lock, this method will throw an error.


count

Query the number of currently waiting tasks

1
Integer Lock.count();

Return result:

  • Integer, Returns the number of tasks

toString

Returns the string representation of the object, generally returns "[Native Object]", the object can be re-implemented according to its own characteristics

1
String Lock.toString();

Return result:

  • String, Returns the string representation of the object

toJSON

Returns the JSON format representation of the object, generally returns a collection of readable attributes defined by the object

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

Call parameters:

  • key: String, unused

Return result:

  • Value, Returns a value containing JSON serializable