JavaScript "sync"

         
/**
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
export const MAX_DATA = 128;
let fs;
if (typeof WorkerGlobalScope === 'undefined') {
fs = "";
} else {
fs = undefined;
}
/******************************************************************/
/* CPU Semaphore Class */
/******************************************************************/
function Semaphore(shared, index, value) {
this.shared = shared;
this.index = index;
shared[index] = value;
}
async function cpu_lock_acquire(lock, en) {
while (en === null || en.signal === undefined) {
let res = Atomics.waitAsync(lock.shared, lock.index, 0);
if (res.async) await res.value;
let value = lock.shared[lock.index];
if (value === 0)
continue;
if (!Atomics.compareExchange(lock.shared, lock.index, value, value - 1))
continue;
return true;
}
return false;
}
function cpu_lock_cancel(lock) {
Atomics.notify(lock.shared, lock.index, Infinity);
}
function cpu_lock_tryacquire(lock) {
Atomics.wait(lock.shared, lock.index, 0, 0);
let value = lock.shared[lock.index];
if (value === 0)
return false;
if (!Atomics.compareExchange(lock.shared, lock.index, value, value-1))
return false;
return true;
}
function cpu_lock_release(lock) {
Atomics.add(lock.shared, lock.index, 1);
Atomics.notify(lock.shared, lock.index, 1);
}
/******************************************************************/
/* CPU Data Class */
/******************************************************************/
function Data(shared, index) {
this.shared = shared;
this.index = index;
}
function cpu_data_store(data, src, offset, len) {
data.shared[data.index] = len;
for (let i = 0; i < len; i++)
data.shared[data.index+1+i] = src[offset+i];
}
/**
* cpu_data_load(src, dst, offset, len):
* Unlike streams, this is an all or nothing load.
* @return if != len nothing loaded, otherwise everything loaded.
*/
function cpu_data_load(data, dst, offset, len) {
let res = data.shared[data.index];
if (res !== len)
return res;
for (let i = 0; i < len; i++)
dst[offset+i] = data.shared[data.index+1+i];
return len;
}
/******************************************************************/
/* CPU Crossbar Class */
/******************************************************************/
export function Crossbar(shared, index) {
this.head = new Semaphore(shared, index, 0);
this.tail = new Semaphore(shared, index+1, 1);
this.data = new Data(shared, index+2);
}
export async function cpu_xbar_put(xbar, src, offset, len, en) {
if (!await cpu_lock_acquire(xbar.tail, en))
return false;
cpu_data_store(xbar.data, src, offset, len);
cpu_lock_release(xbar.head);
return true;
}
export function cpu_xbar_unput(xbar) {
cpu_lock_cancel(xbar.tail);
}
/**
* cpu_xbar_take(xbar, dst, offset, len, en):
* Unlike streams, this is an all or nothing take.
* @return -1 if aborted, otherwise all or nothing.
*/
export async function cpu_xbar_take(xbar, dst, offset, len, en) {
if (!await cpu_lock_acquire(xbar.head, en))
return -1;
let res = cpu_data_load(xbar.data, dst, offset, len);
cpu_lock_release(xbar.tail);
return res;
}
export function cpu_xbar_untake(xbar) {
cpu_lock_cancel(xbar.head);
}
export function cpu_xbar_offer(xbar, src, offset, len) {
if (!cpu_lock_tryacquire(xbar.tail))
return false;
cpu_data_store(xbar.data, src, offset, len);
cpu_lock_release(xbar.head);
return true;
}
/**
* cpu_xbar_poll(xbar, dst, offset, len):
* Unlike streams, this is an all or nothing poll.
* @return if -1 nothing available, otherwise all or nothing.
*/
export function cpu_xbar_poll(xbar, dst, offset, len) {
if (!cpu_lock_tryacquire(xbar.head))
return -1;
let res = cpu_data_load(xbar.data, dst, offset, len);
cpu_lock_release(xbar.tail);
return res;
}