Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 8 additions & 2 deletions lib/node_modules/@stdlib/utils/parallel/lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// MODULES //

var numCPUs = require( '@stdlib/os/num-cpus' );
var max = require( '@stdlib/math/base/special/fast/max' );


// MAIN //
Expand All @@ -36,12 +37,17 @@ var numCPUs = require( '@stdlib/os/num-cpus' );
* // returns {...}
*/
function defaults() {
var n;

// Ensure at least 1 worker and 1 concurrent script to prevent deadlock on single-core systems:
n = max( 1, numCPUs - 1 );

return {
// Number of workers:
'workers': numCPUs - 1,
'workers': n,

// Number of scripts to execute concurrently:
'concurrency': numCPUs - 1,
'concurrency': n,

// Executable file/command:
'cmd': 'node',
Expand Down
7 changes: 7 additions & 0 deletions lib/node_modules/@stdlib/utils/parallel/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ function parallel() {
if ( opts.workers > opts.concurrency ) {
opts.workers = opts.concurrency;
}
// Ensure at least 1 worker and 1 concurrent script to prevent deadlock (even with empty arrays or single-core systems):
if ( opts.workers < 1 ) {
opts.workers = 1;
}
if ( opts.concurrency < 1 ) {
opts.concurrency = 1;
}
// Resolve any relative paths to absolute paths...
dir = cwd();
for ( i = 0; i < files.length; i++ ) {
Expand Down
39 changes: 39 additions & 0 deletions lib/node_modules/@stdlib/utils/parallel/test/test.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,45 @@ tape( 'if the number of workers is greater than the concurrency, the function se
}
});

tape( 'the function ensures at least 1 worker and 1 concurrent script on single-core systems', function test( t ) {
var parallel;

parallel = proxyquire( './../lib/main.js', {
'./node': exec,
'./defaults.js': mockDefaults
});

parallel( files(), done );

function mockDefaults() {
return {
'workers': 1,
'concurrency': 1,
'cmd': 'node',
'ordered': false,
'uid': null,
'gid': null,
'encoding': 'buffer',
'maxBuffer': 200 * 1024 * 1024
};
}

function done( error ) {
if ( error ) {
t.ok( false, error.message );
} else {
t.ok( true, 'executes successfully on single-core systems' );
}
t.end();
}

function exec( files, opts, clbk ) {
t.strictEqual( opts.workers, 1, 'has at least 1 worker' );
t.strictEqual( opts.concurrency, 1, 'has at least 1 concurrency' );
clbk();
}
});

tape( 'the function runs scripts in parallel', function test( t ) {
parallel( files(), done );
function done( error ) {
Expand Down