os: add availableParallelism() · nodejs/node@d71883e · GitHub
Skip to content

Commit d71883e

Browse files
cjihrigRafaelGSS
authored andcommitted
os: add availableParallelism()
This commit exposes uv_available_parallelism() as an alternative to cpus().length. uv_available_parallelism() is inspired by Rust's available_parallelism(). PR-URL: #45895 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent e7b98a3 commit d71883e

4 files changed

Lines changed: 28 additions & 0 deletions

File tree

doc/api/os.md

Lines changed: 14 additions & 0 deletions

lib/os.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const {
4545
const { validateInt32 } = require('internal/validators');
4646

4747
const {
48+
getAvailableParallelism,
4849
getCPUs,
4950
getFreeMem,
5051
getHomeDirectory: _getHomeDirectory,
@@ -100,6 +101,7 @@ const getOSVersion = () => version;
100101
*/
101102
const getMachine = () => machine;
102103

104+
getAvailableParallelism[SymbolToPrimitive] = () => getAvailableParallelism();
103105
getFreeMem[SymbolToPrimitive] = () => getFreeMem();
104106
getHostname[SymbolToPrimitive] = () => getHostname();
105107
getOSVersion[SymbolToPrimitive] = () => getOSVersion();
@@ -364,6 +366,7 @@ function userInfo(options) {
364366

365367
module.exports = {
366368
arch,
369+
availableParallelism: getAvailableParallelism,
367370
cpus,
368371
endianness,
369372
freemem: getFreeMem,

src/node_os.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ static void GetPriority(const FunctionCallbackInfo<Value>& args) {
380380
args.GetReturnValue().Set(priority);
381381
}
382382

383+
static void GetAvailableParallelism(const FunctionCallbackInfo<Value>& args) {
384+
unsigned int parallelism = uv_available_parallelism();
385+
args.GetReturnValue().Set(parallelism);
386+
}
383387

384388
void Initialize(Local<Object> target,
385389
Local<Value> unused,
@@ -397,6 +401,8 @@ void Initialize(Local<Object> target,
397401
SetMethod(context, target, "getUserInfo", GetUserInfo);
398402
SetMethod(context, target, "setPriority", SetPriority);
399403
SetMethod(context, target, "getPriority", GetPriority);
404+
SetMethod(
405+
context, target, "getAvailableParallelism", GetAvailableParallelism);
400406
SetMethod(context, target, "getOSInformation", GetOSInformation);
401407
target
402408
->Set(context,
@@ -417,6 +423,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
417423
registry->Register(GetUserInfo);
418424
registry->Register(SetPriority);
419425
registry->Register(GetPriority);
426+
registry->Register(GetAvailableParallelism);
420427
registry->Register(GetOSInformation);
421428
}
422429

test/parallel/test-os.js

Lines changed: 4 additions & 0 deletions

0 commit comments

Comments
 (0)