Class: Semaphore

Semaphore

This is a class that provides promises enabled semaphores. It is possible to create a semaphore with a name (merely fore debugging purposes) and speciffic number of resources that we can wait for to get available, and release them when we do not need them anymore


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

Source:
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)>
Source:

#consumerUniqueId :Array.<number(int)>

unique id of each new consumption

Type:
  • Array.<number(int)>
Source:

#debug :boolean

defines if debugging messages should be shown during Semaphore operations

Type:
  • boolean
Source:

#name :string

name of the semaphore

Type:
  • string
Source:

#resourcesNo :string

currently available number of resources

Type:
  • string
Source:

#supportConsumersLog :boolean

defines if we should support consumers log if set to true, every wait() will remmeber the consumer name and return an wait id, that user can provide to singnal() to signal that the specific wait is finished

Type:
  • boolean
Source:

Methods


signal(consumerIdName)

release resources in semaphore

Parameters:
Name Type Argument Description
consumerIdName number(int) | string <optional>

consumer name (string) or consumer id (integer)

Source:

wait(consumerName)

waits on semaphore

Parameters:
Name Type Description
consumerName string

name of the consumer

Source:
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