Skip to content

infinity.lock

Provides named read/write locks for synchronizing access to shared resources (e.g. values in infinity.memory) between the main program, threads and scheduler tasks.

Module: none (built-in)

A lock is identified by an arbitrary, non-empty name; it is created on first use and is shared by all runtimes (main program, threads, HTTP handlers, ...) of the INFINITY.JS instance. The lock names are independent of infinity.memory namespaces: locking a name does not lock anything by itself, all code accessing the protected resource has to use the same lock name.

Every beginRead() must be followed by exactly one endRead() and every beginWrite() by exactly one endWrite() with the same name, preferably in a try/finally block. The thread that holds the write lock of a name may call beginWrite() or beginRead() for it again (since 3.12; before, this blocked forever on Windows); each call needs its own endWrite()/endRead(). Do not call beginWrite() while holding only a read lock of the same name, this blocks forever. On Windows, several readers may hold a lock at the same time; on Linux, read locks are exclusive as well.

Example:

main.ts:

infinity.loadModule('infinity.thread');
infinity.loadModule('infinity.memory');

let thread = new infinity.thread('thread.js');
thread.start();

while (!infinity.terminated) {

  infinity.sleep(500);
  infinity.lock.beginRead('res');
  console.debug('main.js: Reading ' + infinity.memory.getNumber('global', 'value') + ' from memory');

  infinity.lock.endRead('res');
}

thread.stop();

thread.ts:

infinity.loadModule('infinity.memory');

while (!infinity.terminated) {

  infinity.sleep(500);
  infinity.lock.beginWrite('res');

  sub();

  infinity.lock.endWrite('res');
}


function sub() {

  let v =  infinity.memory.getNumber('global', 'value');
  if(isNaN(v)){
    v = 0;
  } else {
    v++;
  }
  console.debug('thread.js: Writing ' + v + ' to memory');
  infinity.memory.putNumber('global', 'value', v);

}
Console output:

#We see both processes competing for access at roughly the same moment, thus occasionally blocking one another
[I 2021-03-31 19:08:57,993] thread.js: Writing 0 to memory
[I 2021-03-31 19:08:57,993] main.js: Reading 0 from memory
[I 2021-03-31 19:08:58,494] main.js: Reading 0 from memory
[I 2021-03-31 19:08:58,494] thread.js: Writing 1 to memory
[I 2021-03-31 19:08:58,998] main.js: Reading 1 from memory
[I 2021-03-31 19:08:58,998] thread.js: Writing 2 to memory
[I 2021-03-31 19:08:59,500] thread.js: Writing 3 to memory
[I 2021-03-31 19:08:59,500] main.js: Reading 3 from memory
[I 2021-03-31 19:09:00,014] main.js: Reading 3 from memory
[I 2021-03-31 19:09:00,014] thread.js: Writing 4 to memory
[I 2021-03-31 19:09:00,515] main.js: Reading 4 from memory
[I 2021-03-31 19:09:00,515] thread.js: Writing 5 to memory
[I 2021-03-31 19:09:01,030] thread.js: Writing 6 to memory
[I 2021-03-31 19:09:01,030] main.js: Reading 6 from memory
[I 2021-03-31 19:09:01,545] main.js: Reading 6 from memory
[I 2021-03-31 19:09:01,545] thread.js: Writing 7 to memory
[I 2021-03-31 19:09:02,049] thread.js: Writing 8 to memory
[I 2021-03-31 19:09:02,049] main.js: Reading 8 from memory
[I 2021-03-31 19:09:02,551] main.js: Reading 8 from memory
[I 2021-03-31 19:09:02,551] thread.js: Writing 9 to memory
[I 2021-03-31 19:09:03,054] main.js: Reading 9 from memory
[I 2021-03-31 19:09:03,054] thread.js: Writing 10 to memory
[I 2021-03-31 19:09:03,557] thread.js: Writing 11 to memory
[I 2021-03-31 19:09:03,557] main.js: Reading 11 from memory
[I 2021-03-31 19:09:04,061] thread.js: Writing 12 to memory
[I 2021-03-31 19:09:04,061] main.js: Reading 12 from memory
[I 2021-03-31 19:09:04,567] thread.js: Writing 13 to memory
[I 2021-03-31 19:09:04,567] main.js: Reading 13 from memory
[I 2021-03-31 19:09:05,071] thread.js: Writing 14 to memory
[I 2021-03-31 19:09:05,071] main.js: Reading 14 from memory
[I 2021-03-31 19:09:05,573] thread.js: Writing 15 to memory
[I 2021-03-31 19:09:05,573] main.js: Reading 15 from memory
...

Functions

beginRead()

Acquires the lock with the specified name for reading. Blocks until no other thread holds the lock for writing.

Signature:

beginRead( nameSpace: string ): void

Parameters:

  • nameSpace: string

    The name of the lock (must not be empty).

Example:

infinity.lock.beginRead('myNameSpace');

beginWrite()

Acquires the lock with the specified name exclusively for writing. Blocks until no other thread holds the lock.

Signature:

beginWrite( nameSpace: string ): boolean

Parameters:

  • nameSpace: string

    The name of the lock (must not be empty).

Return type: boolean

Always returns true once the lock has been acquired.

Example:

infinity.lock.beginWrite('myNameSpace');
try {
  // modify the shared resource
} finally {
  infinity.lock.endWrite('myNameSpace');
}

endRead()

Releases a read lock acquired with beginRead().

Signature:

endRead( nameSpace: string ): void

Parameters:

  • nameSpace: string

    The name of the lock (must not be empty).

Example:

infinity.lock.endRead('myNameSpace');

endWrite()

Releases a write lock acquired with beginWrite().

Signature:

endWrite( nameSpace: string ): void

Parameters:

  • nameSpace: string

    The name of the lock (must not be empty).

Example:

infinity.lock.endWrite('myNameSpace');