new SemaphoreMultiReservation(name, resourcesNo, debug, waitForMoreDemandingConsumers, debug)
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 |
waitForMoreDemandingConsumers |
boolean |
<optional> |
true | Defines if consumer can allocate resources even if other consumer waits for available resources (but needs more resources than currently available) |
debug |
boolean |
<optional> |
false | Defines if debugging messages should be shown during Semaphore operations |
Example
// Example of 3 groups
var QPP = require('qpp');
var s = new QPP.SemaphoreMultiReservation('Nebojsa tower', 5);
// group 1
setTimeout(function(){
s.wait(3) // 3 people
.then(function(){ // resource is available, consuming resource
console.log("Group 1: Let's run to the top!");
setTimeout(function(){
console.log("Group 1: Great experience, but they ask us to leave!")
s.signal(3); // releasing resource (toilet)
}, parseInt(Math.random()*1500)+1);
});
}, parseInt(Math.random()*100)+1);
// group 2
setTimeout(function(){
s.wait(4) // 4 people
.then(function(){ // resource is available, consuming resource
console.log("Group 2: Tower is available for us!");
setTimeout(function(){
console.log("Group 2: Let's give the space for others!")
s.signal(4); // releasing resource (toilet)
}, parseInt(Math.random()*500)+1);
});
}, parseInt(Math.random()*100)+1);
// group 3
setTimeout(function(){
s.wait(2) // 2 people
.then(function(){ // resource is available, consuming resource
console.log("Group 3: Hey, i have to show you the view!");
setTimeout(function(){
console.log("Group 3: Ah, we could stay here forever!")
s.signal(2); // releasing resource (toilet)
}, parseInt(Math.random()*100)+1);
});
}, parseInt(Math.random()*100)+1);
// This is the most interesting scenario:
// Group 2: Tower is available for us
// Group 2: Let's give the space for others
// Group 3: Hey, i have to show you the view
// Group 1: Let's run to the top
// Group 3: Ah, we could stay here forever
// Group 1: Great experience, but they ask us to leave!
// Because both group 1 and 3 ended up at the top of the towe simultaneously
// (there were enough of resources to allocate for both (2+3<=5))
//
// For more examples, please check unit tests for @see qpp.mapBandwidth
Members
-
#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
-
#waitForMoreDemandingConsumers :boolean
-
defines if consumer can allocate resources even if other consumer waits for available resources (but needs more resources than currently available)
Type:
- boolean
- Default Value:
-
- true
- Source:
Example
var s = new Semaphore('s', 3); var wP1 = s.wait(1); // fine, 2 resources left available var wP2 = s.wait(3); // not fine (consumer 1 has to release) // wP3 will be fine and resolved ifthis.waitForMoreDemandingConsumers=== false // or not fine and not resolved until consumer 1's resources are released (signaled) // ifthis.waitForMoreDemandingConsumers=== true (default) var wP3 = s.wait(2);
Methods
-
signal(resourcesReleased)
-
release resources in semaphore
Parameters:
Name Type Argument Default Description resourcesReleasednumber <optional>
"1" The numer of resources released
-
wait(resourcesNoNeeded)
-
waits on semaphore
Parameters:
Name Type Argument Default Description resourcesNoNeedednumber <optional>
"1" The numer of resources needed
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