new Semaphore(name, resourcesNo, debug, supportConsumersLog)
Constructor function. Creates a new semaphore with optional name and resources number
Parameters:
Name | Type | Argument | Default | Description |
---|---|---|---|---|
name |
string |
<optional> |
"semaphore" | The name of the created semaphore |
resourcesNo |
number(integer) |
<optional> |
1 | The total numer of resources available |
debug |
boolean |
<optional> |
false | Defines if debugging messages should be shown during Semaphore operations |
supportConsumersLog |
boolean |
<optional> |
false | defines if we should support consumers log |
Example
// Example of two consumers var QPP = require('./..'); var s = new QPP.Semaphore('airstrip', 1); // airplane (consumer) 1, waits for passangers to board setTimeout(function(){ s.wait() // allocating the resource (airstrip) .then(function(){ // resource is available, consuming resource console.log("Pilot 1: Yes! The airstrip is freee! We are the next one!"); setTimeout(function(){ console.log("Pilot 1: Ah, view is much better here!") s.signal(); // releasing resource (airstrip) }, parseInt(Math.random()*1500)+1); }); }, parseInt(Math.random()*1500)+1); setTimeout(function(){ // airplane (consumer) 2 s.wait() // allocating the resource (airstrip) .then(function(){ // resource is available, consuming resource console.log("Pilot 2: Great we are ready to departure, no one on the airstrip!"); setTimeout(function(){ console.log("Pilot 2: Dear passangers, enjoy our flight!") s.signal(); // releasing resource (airstrip) }, parseInt(Math.random()*2000)+1); }); }, parseInt(Math.random()*1500)+1); // For more examples, please check unit tests for @see qpp.Semaphore
Members
-
#consumersLog :Array.<number(int)>
-
consumers log tracking consumers that are currently using resources
Type:
- Array.<number(int)>
-
#consumerUniqueId :Array.<number(int)>
-
unique id of each new consumption
Type:
- Array.<number(int)>
-
#debug :boolean
-
defines if debugging messages should be shown during Semaphore operations
Type:
- boolean
-
#name :string
-
name of the semaphore
Type:
- string
-
#resourcesNo :string
-
currently available number of resources
Type:
- string
-
#supportConsumersLog :boolean
-
defines if we should support consumers log if set to
true
, everywait()
will remmeber the consumer name and return an wait id, that user can provide tosingnal()
to signal that the specific wait is finishedType:
- boolean
Methods
-
signal(consumerIdName)
-
release resources in semaphore
Parameters:
Name Type Argument Description consumerIdName
number(int) | string <optional>
consumer name (string) or consumer id (integer)
-
wait(consumerName)
-
waits on semaphore
Parameters:
Name Type Description consumerName
string name of the consumer
Returns:
promise that will get resolved after the semaphore is available. The only possibility for promise to get rejected is when semaphore gets destroyed In that case it will get rejected with an @see
Error
.- Type
- external:Promise