Shared : Shared memory

更新时间:
2024-05-14
下载文档

Shared : Shared memory

JSRE multitasking shared module. The shared memory service can be widely used to share binary data between multiple tasks, avoid memory copying, improve the efficiency of audio, video and AI operation multitasking.

JSRE provides a secure shared memory implementation via ArrayBuffer, which can be used to implement shared memory operations using TypedArray, Buffer, and DataView. The JSRE engine has a strict safety design for shared memory boundaries.

Multi-Task Safety

JSRE supports multi-tasking environment, so the operation of shared memory may cause competition, so for multi-task safety considerations, the operation of shared area should use mutual exclusion lock.

Example

var Mutex = require("mutex");
var m = new Mutex("Named_Lock_1");

m.lock();
// Operate shared resources here.
m.unlock();

Or use a global lock:

synchronize(() => {
  // Operate shared resources here.
});

Not all shared data areas require exclusive access, which can cause serious efficiency problems. For the Producer-Consumer model, we only need to protect those small administrative data updates (possibly requiring operations between some operations), such as a large amount of video frame data, no need for mutual exclusion protection.

If you use other communication methods for status notification, you do not need mutual exclusion protection at all.

Feature

Shared memory is very versatile, such as a video application, multitasking may have different processing, AI tasks for target recognition, transfer tasks for data transfer, and backup tasks for data storage. These three tasks can be executed simultaneously without copying any of data.

Support

The following shows Shared module APIs available for each permissions.

 User ModePrivilege Mode
Shared.ArrayBuffer

Shared.ArrayBuffer(name, size)

  • name {String} Shared memory name.
  • size {Integer} Shared memory size.
  • Returns: {arraybuffer} ArrayBuffer object.

Create a shared memory ArrayBuffer, If the name strings are the same, multitasking is created in the same way and will use one shared memory.

Example

// Multitasking uses same method and parameter to create shared memory.
var arrayBuffer = Shared.ArrayBuffer("SM-1", 1024);

// This shared memory can be used with objects such as Buffer and DataView...
var buffer = new Buffer(arrayBuffer);
var dataview = new DataView(arrayBuffer);

buffer[0] = 1;
console.log(dataview.getInt8(0));
文档内容是否对您有所帮助?
有帮助
没帮助