Skip to content

Commit

Permalink
Remove PTHREAD_HINT_NUM_CORES (emscripten-core#10166)
Browse files Browse the repository at this point in the history
We can assume almost all browsers have
navigator.hardwareConcurrency these days, and
basically all those where we can use pthreads (so
not IE11).
  • Loading branch information
kripken authored Jan 9, 2020
1 parent 9f1d3f3 commit fd717f0
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 76 deletions.
3 changes: 1 addition & 2 deletions site/source/docs/porting/pthreads.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ Compiling with pthreads enabled
By default, support for pthreads is not enabled. To enable code generation for pthreads, the following command line flags exist:

- Pass the compiler flag ``-s USE_PTHREADS=1`` when compiling any .c/.cpp files, AND when linking to generate the final output .js file.
- Optionally, pass the linker flag ``-s PTHREAD_POOL_SIZE=<integer>`` to specify a predefined pool of web workers to populate at page preRun time before application main() is called. This is important because if the workers do not already exist then we may need to wait for the next browser event iteration for certain things, see below. (If -1 is passed to both PTHREAD_POOL_SIZE and PTHREAD_HINT_NUM_CORES, then a popup dialog will ask the user the size of the pool, which is useful for testing.)
- Optionally, pass the linker flag ``-s PTHREAD_HINT_NUM_CORES=<integer>`` to choose what the function emscripten_num_logical_cores(); will return if navigator.hardwareConcurrency is not supported. If -1 is specified here, a popup dialog will be shown at startup to let the user specify the value that is returned here. This can be helpful in order to dynamically test how an application behaves with different values here.
- Optionally, pass the linker flag ``-s PTHREAD_POOL_SIZE=<integer>`` to specify a predefined pool of web workers to populate at page preRun time before application main() is called. This is important because if the workers do not already exist then we may need to wait for the next browser event iteration for certain things, see below.

There should be no other changes required. In C/C++ code, the preprocessor check ``#ifdef __EMSCRIPTEN_PTHREADS__`` can be used to detect whether Emscripten is currently targeting pthreads.

Expand Down
10 changes: 1 addition & 9 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,20 +582,12 @@ var LibraryPThread = {
}
},

_num_logical_cores__deps: ['emscripten_force_num_logical_cores'],
_num_logical_cores: '{{{ makeStaticAlloc(4) }}}; HEAPU32[__num_logical_cores>>2] = navigator["hardwareConcurrency"] || ' + {{{ PTHREAD_HINT_NUM_CORES }}},

emscripten_has_threading_support: function() {
return typeof SharedArrayBuffer !== 'undefined';
},

emscripten_num_logical_cores__deps: ['_num_logical_cores'],
emscripten_num_logical_cores: function() {
return {{{ makeGetValue('__num_logical_cores', 0, 'i32') }}};
},

emscripten_force_num_logical_cores: function(cores) {
{{{ makeSetValue('__num_logical_cores', 0, 'cores', 'i32') }}};
return navigator['hardwareConcurrency'];
},

{{{ USE_LSAN || USE_ASAN ? 'emscripten_builtin_' : '' }}}pthread_create__deps: ['_spawn_thread', 'pthread_getschedparam', 'pthread_self', 'memalign'],
Expand Down
37 changes: 0 additions & 37 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,43 +780,6 @@ var memoryInitializer = null;
#include "memoryprofiler.js"
#endif

#if USE_PTHREADS && PTHREAD_HINT_NUM_CORES < 0
if (!ENVIRONMENT_IS_PTHREAD) addOnPreRun(function() {
addRunDependency('pthreads_querycores');

var bg = document.createElement('div');
bg.style = "position: absolute; top: 0%; left: 0%; width: 100%; height: 100%; background-color: black; z-index:1001; -moz-opacity: 0.8; opacity:.80; filter: alpha(opacity=80);";
var div = document.createElement('div');
var default_num_cores = navigator.hardwareConcurrency || 4;
var hwConcurrency = navigator.hardwareConcurrency ? ("says " + navigator.hardwareConcurrency) : "is not available";
var html = '<div style="width: 100%; text-align:center;"> Thread setup</div> <br /> Number of logical cores: <input type="number" style="width: 50px;" value="'
+ default_num_cores + '" min="1" max="32" id="thread_setup_num_logical_cores"></input> <br /><span style="font-size: 75%;">(<span style="font-family: monospace;">navigator.hardwareConcurrency</span> '
+ hwConcurrency + ')</span> <br />';
#if PTHREAD_POOL_SIZE < 0
html += 'PThread pool size: <input type="number" style="width: 50px;" value="'
+ default_num_cores + '" min="1" max="32" id="thread_setup_pthread_pool_size"></input> <br />';
#endif
html += ' <br /> <input type="button" id="thread_setup_button_go" value="Go"></input>';
div.innerHTML = html;
div.style = 'position: absolute; top: 35%; left: 35%; width: 30%; height: 150px; padding: 16px; border: 16px solid gray; background-color: white; z-index:1002; overflow: auto;';
document.body.appendChild(bg);
document.body.appendChild(div);
var goButton = document.getElementById('thread_setup_button_go');
goButton.onclick = function() {
var num_logical_cores = parseInt(document.getElementById('thread_setup_num_logical_cores').value);
_emscripten_force_num_logical_cores(num_logical_cores);
#if PTHREAD_POOL_SIZE < 0
var pthread_pool_size = parseInt(document.getElementById('thread_setup_pthread_pool_size').value);
PThread.allocateUnusedWorkers(pthread_pool_size, function() { removeRunDependency('pthreads_querycores'); });
#else
removeRunDependency('pthreads_querycores');
#endif
document.body.removeChild(bg);
document.body.removeChild(div);
}
});
#endif

#if PTHREAD_POOL_SIZE > 0 && PTHREAD_POOL_DELAY_LOAD != 1
// To work around https://bugzilla.mozilla.org/show_bug.cgi?id=1049079, warm up a worker pool before starting up the application.
if (!ENVIRONMENT_IS_PTHREAD) addOnPreRun(function() { if (typeof SharedArrayBuffer !== 'undefined') { addRunDependency('pthreads'); PThread.allocateUnusedWorkers({{{PTHREAD_POOL_SIZE}}}, function() { removeRunDependency('pthreads'); }); }});
Expand Down
5 changes: 0 additions & 5 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1393,11 +1393,6 @@ var PTHREAD_POOL_DELAY_LOAD = 0;
// local vars in asm.js/wasm code.
var DEFAULT_PTHREAD_STACK_SIZE = 2*1024*1024;

// Specifies the value returned by the function emscripten_num_logical_cores()
// if navigator.hardwareConcurrency is not supported. Pass in a negative number
// to show a popup dialog at startup so the user can configure this dynamically.
var PTHREAD_HINT_NUM_CORES = 4;

// True when building with --threadprofiler
var PTHREADS_PROFILING = 0;

Expand Down
18 changes: 0 additions & 18 deletions tests/pthread/test_pthread_num_logical_cores.cpp

This file was deleted.

5 changes: 0 additions & 5 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3819,11 +3819,6 @@ def test_pthread_iostream(self):
def test_pthread_setspecific_mainthread(self):
self.btest(path_from_root('tests', 'pthread', 'test_pthread_setspecific_mainthread.cpp'), expected='0', args=['-s', 'TOTAL_MEMORY=64MB', '-O3', '-s', 'USE_PTHREADS=1'], also_asmjs=True)

# Test the -s PTHREAD_HINT_NUM_CORES=x command line variable.
@requires_threads
def test_pthread_num_logical_cores(self):
self.btest(path_from_root('tests', 'pthread', 'test_pthread_num_logical_cores.cpp'), expected='0', args=['-O3', '-s', 'USE_PTHREADS=1', '-s', 'PTHREAD_HINT_NUM_CORES=2'], also_asmjs=True)

# Test that pthreads have access to filesystem.
@requires_threads
def test_pthread_file_io(self):
Expand Down

0 comments on commit fd717f0

Please sign in to comment.