Semaphore : Semaphore

更新时间:
2024-03-04
下载文档

Semaphore : Semaphore

Semaphore is a synchronous communication mechanism and often used in synchronous multitasking environments. JSRE also provides asynchronous communication and synchronous communication mechanism, you can choose the suitable communication component according to the application design framework.

User can use the following code to import the Semaphore module.

var Semaphore = require("semaphore");

Example

  • main.js
var Semaphore = require("semaphore");

var sem = new Semaphore("SEM-1");
var t = new Task("./task.js");

while (true) {
  // Post per second.
  sem.post();
  sys.sleep(1000);
}
  • task.js
var Semaphore = require("semaphore");

var sem = new Semaphore("SEM-1");

while (true) {
  sem.wait();
  console.log("sem event!");
}

Support

The following shows Semaphore module APIs available for each permissions.

 User ModePrivilege Mode
Semaphore
sem.wait
sem.post
sem.stat

Semaphore Class

new Semaphore(name[, value[, maxValue[, prioQueue]]])

  • name {String} Semaphore name.
  • value {Integer} Semaphore initial value. default: 0.
  • maxValue {Integer} Semaphore maximum value. default: 2147483647.
  • prioQueue {Boolean} Priority waiting queue.
  • Returns: {Object} A new semaphore object.

Create a semaphore object. first try to open the semaphore object with same name. If not found, create a new object. The semaphore parameter is valid for all tasks of the entire process. Different tasks can create semaphore objects with same name for inter-task synchronous communication.

If prioQueue is true, multitasking waits for the same semaphore to be queued by task priority.

Example

var sem = new Semaphore("SEM-1");
var sem = new Semaphore("SEM-1", 0);
var sem = new Semaphore("SEM-1", 1, 1);

Semaphore Object

sem.wait([timeout])

  • timeout {Integer} Wait timeout in milliseconds. default: undefined means wait forever until get this semaphore.
  • Returns: {Boolean} Get successfully returns true, otherwise false means timed out.

A semaphore can be understood as a counter. If the counter is not 0, sem.wait() decrements the counter and return true. If the counter value is 0, current task block until the other task post semaphore we get.

Semaphore can be used not only for notifications, but also as a lock for multitasking shared resources. For example, there are 10 resources available. We can create a semaphore with an initial value of 10, and call sem.wait() before get resource. After using the resource, call sem.post() to release it.

sem.post()

  • Returns: {Boolean} Post successfully returns true, otherwise false.

If the current semaphore counter has reached its maximum value, post fails and the counter value does not change. Otherwise the counter is increase one and if there are other tasks waiting for this semaphore, activate the waiting task immediately.

sem.stat()

  • Returns: {Integer} Current semaphore counter.

Get the current semaphore counter immediately.

文档内容是否对您有所帮助?
有帮助
没帮助