Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ responding to new issues.
Primary GPG keys for Node.js Releasers (some Releasers sign with subkeys):

* **Antoine du Hamel** <<duhamelantoine1995@gmail.com>>
`C0D6248439F1D5604AAFFB4021D900FFDB233756`
`5BE8A3F6C8A5C01D106C0AD820B1A390B168D356`
* **Juan José Arboleda** <<soyjuanarbol@gmail.com>>
`DD792F5973C6DE52C432CBDAC77ABFA00DDBF2B7`
* **Marco Ippolito** <<marcoippolito54@gmail.com>>
Expand All @@ -810,7 +810,7 @@ To import the full set of trusted release keys (including subkeys possibly used
to sign releases):

```bash
gpg --keyserver hkps://keys.openpgp.org --recv-keys C0D6248439F1D5604AAFFB4021D900FFDB233756 # Antoine du Hamel
gpg --keyserver hkps://keys.openpgp.org --recv-keys 5BE8A3F6C8A5C01D106C0AD820B1A390B168D356 # Antoine du Hamel
gpg --keyserver hkps://keys.openpgp.org --recv-keys DD792F5973C6DE52C432CBDAC77ABFA00DDBF2B7 # Juan José Arboleda
gpg --keyserver hkps://keys.openpgp.org --recv-keys CC68F5A3106FF448322E48ED27F5E38D5B0A215F # Marco Ippolito
gpg --keyserver hkps://keys.openpgp.org --recv-keys 8FCCA13FEF1D0C2E91008E09770F7A9A5AE15600 # Michaël Zasso
Expand All @@ -827,6 +827,8 @@ verify a downloaded file.

<summary>Other keys used to sign some previous releases</summary>

* **Antoine du Hamel** <<duhamelantoine1995@gmail.com>>
`C0D6248439F1D5604AAFFB4021D900FFDB233756`
* **Beth Griggs** <<bethanyngriggs@gmail.com>>
`4ED778F539E3634C779C87C6D7062848A1AB005C`
* **Bryan English** <<bryan@bryanenglish.com>>
Expand Down
52 changes: 52 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,55 @@ consisting of the runtime name and major version number.
console.log(`The user-agent is ${navigator.userAgent}`); // Prints "Node.js/21"
```

### `navigator.locks`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

The `navigator.locks` read-only property returns a [`LockManager`][] instance that
can be used to coordinate access to resources that may be shared across multiple
threads within the same process. This global implementation matches the semantics
of the [browser `LockManager`][] API.

```mjs
// Request an exclusive lock
await navigator.locks.request('my_resource', async (lock) => {
// The lock has been acquired.
console.log(`Lock acquired: ${lock.name}`);
// Lock is automatically released when the function returns
});

// Request a shared lock
await navigator.locks.request('shared_resource', { mode: 'shared' }, async (lock) => {
// Multiple shared locks can be held simultaneously
console.log(`Shared lock acquired: ${lock.name}`);
});
```

```cjs
// Request an exclusive lock
navigator.locks.request('my_resource', async (lock) => {
// The lock has been acquired.
console.log(`Lock acquired: ${lock.name}`);
// Lock is automatically released when the function returns
}).then(() => {
console.log('Lock released');
});

// Request a shared lock
navigator.locks.request('shared_resource', { mode: 'shared' }, async (lock) => {
// Multiple shared locks can be held simultaneously
console.log(`Shared lock acquired: ${lock.name}`);
}).then(() => {
console.log('Shared lock released');
});
```

See [`worker.locks`][] for detailed API documentation.

## Class: `PerformanceEntry`

<!-- YAML
Expand Down Expand Up @@ -1263,6 +1312,7 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][].
[`CountQueuingStrategy`]: webstreams.md#class-countqueuingstrategy
[`DecompressionStream`]: webstreams.md#class-decompressionstream
[`EventTarget` and `Event` API]: events.md#eventtarget-and-event-api
[`LockManager`]: worker_threads.md#class-lockmanager
[`MessageChannel`]: worker_threads.md#class-messagechannel
[`MessagePort`]: worker_threads.md#class-messageport
[`PerformanceEntry`]: perf_hooks.md#class-performanceentry
Expand Down Expand Up @@ -1313,6 +1363,8 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][].
[`setTimeout`]: timers.md#settimeoutcallback-delay-args
[`structuredClone`]: https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
[`window.navigator`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/navigator
[`worker.locks`]: worker_threads.md#workerlocks
[browser `LockManager`]: https://developer.mozilla.org/en-US/docs/Web/API/LockManager
[buffer section]: buffer.md
[built-in objects]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
[timers]: timers.md
Expand Down
150 changes: 150 additions & 0 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,153 @@ if (isMainThread) {
}
```

## `worker.locks`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

* {LockManager}

An instance of a [`LockManager`][LockManager] that can be used to coordinate
access to resources that may be shared across multiple threads within the same
process. The API mirrors the semantics of the
[browser `LockManager`][]

### Class: `Lock`

<!-- YAML
added: REPLACEME
-->

The `Lock` interface provides information about a lock that has been granted via
[`locks.request()`][locks.request()]

#### `lock.name`

<!-- YAML
added: REPLACEME
-->

* {string}

The name of the lock.

#### `lock.mode`

<!-- YAML
added: REPLACEME
-->

* {string}

The mode of the lock. Either `shared` or `exclusive`.

### Class: `LockManager`

<!-- YAML
added: REPLACEME
-->

The `LockManager` interface provides methods for requesting and introspecting
locks. To obtain a `LockManager` instance use

```mjs
import { locks } from 'node:worker_threads';
```

```cjs
'use strict';

const { locks } = require('node:worker_threads');
```

This implementation matches the [browser `LockManager`][] API.

#### `locks.request(name[, options], callback)`

<!-- YAML
added: REPLACEME
-->

* `name` {string}
* `options` {Object}
* `mode` {string} Either `'exclusive'` or `'shared'`. **Default:** `'exclusive'`.
* `ifAvailable` {boolean} If `true`, the request will only be granted if the
lock is not already held. If it cannot be granted, `callback` will be
invoked with `null` instead of a `Lock` instance. **Default:** `false`.
* `steal` {boolean} If `true`, any existing locks with the same name are
released and the request is granted immediately, pre-empting any queued
requests. **Default:** `false`.
* `signal` {AbortSignal} that can be used to abort a
pending (but not yet granted) lock request.
* `callback` {Function} Invoked once the lock is granted (or immediately with
`null` if `ifAvailable` is `true` and the lock is unavailable). The lock is
released automatically when the function returns, or—if the function returns
a promise—when that promise settles.
* Returns: {Promise} Resolves once the lock has been released.

```mjs
import { locks } from 'node:worker_threads';

await locks.request('my_resource', async (lock) => {
// The lock has been acquired.
});
// The lock has been released here.
```

```cjs
'use strict';

const { locks } = require('node:worker_threads');

locks.request('my_resource', async (lock) => {
// The lock has been acquired.
}).then(() => {
// The lock has been released here.
});
```

#### `locks.query()`

<!-- YAML
added: REPLACEME
-->

* Returns: {Promise}

Resolves with a `LockManagerSnapshot` describing the currently held and pending
locks for the current process.

```mjs
import { locks } from 'node:worker_threads';

const snapshot = await locks.query();
for (const lock of snapshot.held) {
console.log(`held lock: name ${lock.name}, mode ${lock.mode}`);
}
for (const pending of snapshot.pending) {
console.log(`pending lock: name ${pending.name}, mode ${pending.mode}`);
}
```

```cjs
'use strict';

const { locks } = require('node:worker_threads');

locks.query().then((snapshot) => {
for (const lock of snapshot.held) {
console.log(`held lock: name ${lock.name}, mode ${lock.mode}`);
}
for (const pending of snapshot.pending) {
console.log(`pending lock: name ${pending.name}, mode ${pending.mode}`);
}
});
```

## Class: `BroadcastChannel extends EventTarget`

<!-- YAML
Expand Down Expand Up @@ -1937,6 +2084,7 @@ thread spawned will spawn another until the application crashes.
[Addons worker support]: addons.md#worker-support
[ECMAScript module loader]: esm.md#data-imports
[HTML structured clone algorithm]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
[LockManager]: #class-lockmanager
[Signals events]: process.md#signal-events
[Web Workers]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
[`'close'` event]: #event-close
Expand Down Expand Up @@ -1992,7 +2140,9 @@ thread spawned will spawn another until the application crashes.
[`worker.terminate()`]: #workerterminate
[`worker.threadId`]: #workerthreadid_1
[async-resource-worker-pool]: async_context.md#using-asyncresource-for-a-worker-thread-pool
[browser `LockManager`]: https://developer.mozilla.org/en-US/docs/Web/API/LockManager
[browser `MessagePort`]: https://developer.mozilla.org/en-US/docs/Web/API/MessagePort
[child processes]: child_process.md
[contextified]: vm.md#what-does-it-mean-to-contextify-an-object
[locks.request()]: #locksrequestname-options-callback
[v8.serdes]: v8.md#serialization-api
Loading
Loading