Skip to content

Commit b97bbc5

Browse files
authored
Remove mimicked thread scheduling support (#13051)
1 parent ed05594 commit b97bbc5

15 files changed

+68
-139
lines changed

src/library.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,6 @@ LibraryManager.library = {
324324
case {{{ cDefine('_SC_THREAD_ATTR_STACKADDR') }}}:
325325
case {{{ cDefine('_SC_THREAD_ATTR_STACKSIZE') }}}:
326326
case {{{ cDefine('_SC_THREAD_CPUTIME') }}}:
327-
case {{{ cDefine('_SC_THREAD_PRIO_INHERIT') }}}:
328-
case {{{ cDefine('_SC_THREAD_PRIO_PROTECT') }}}:
329327
case {{{ cDefine('_SC_THREAD_PROCESS_SHARED') }}}:
330328
case {{{ cDefine('_SC_THREAD_SAFE_FUNCTIONS') }}}:
331329
case {{{ cDefine('_SC_THREADS') }}}:
@@ -338,7 +336,6 @@ LibraryManager.library = {
338336
case {{{ cDefine('_SC_2_LOCALEDEF') }}}:
339337
case {{{ cDefine('_SC_2_SW_DEV') }}}:
340338
case {{{ cDefine('_SC_2_VERSION') }}}:
341-
case {{{ cDefine('_SC_THREAD_PRIORITY_SCHEDULING') }}}:
342339
return 200809;
343340
case {{{ cDefine('_SC_MQ_OPEN_MAX') }}}:
344341
case {{{ cDefine('_SC_XOPEN_STREAMS') }}}:
@@ -372,6 +369,9 @@ LibraryManager.library = {
372369
case {{{ cDefine('_SC_SEM_NSEMS_MAX') }}}:
373370
case {{{ cDefine('_SC_SYMLOOP_MAX') }}}:
374371
case {{{ cDefine('_SC_TIMER_MAX') }}}:
372+
case {{{ cDefine('_SC_THREAD_PRIO_INHERIT') }}}:
373+
case {{{ cDefine('_SC_THREAD_PRIO_PROTECT') }}}:
374+
case {{{ cDefine('_SC_THREAD_PRIORITY_SCHEDULING') }}}:
375375
return -1;
376376
case {{{ cDefine('_SC_V6_ILP32_OFF32') }}}:
377377
case {{{ cDefine('_SC_V6_ILP32_OFFBIG') }}}:

src/library_pthread.js

Lines changed: 1 addition & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,6 @@ var LibraryPThread = {
521521
Atomics.store(HEAPU32, tis + ({{{ C_STRUCTS.pthread.attr }}} >> 2), threadParams.stackSize);
522522
Atomics.store(HEAPU32, tis + ({{{ C_STRUCTS.pthread.attr }}} + 8 >> 2), stackHigh);
523523
Atomics.store(HEAPU32, tis + ({{{ C_STRUCTS.pthread.attr }}} + 12 >> 2), threadParams.detached);
524-
Atomics.store(HEAPU32, tis + ({{{ C_STRUCTS.pthread.attr }}} + 20 >> 2), threadParams.schedPolicy);
525-
Atomics.store(HEAPU32, tis + ({{{ C_STRUCTS.pthread.attr }}} + 24 >> 2), threadParams.schedPrio);
526524

527525
var global_libc = _emscripten_get_global_libc();
528526
var global_locale = global_libc + {{{ C_STRUCTS.libc.global_locale }}};
@@ -569,7 +567,7 @@ var LibraryPThread = {
569567
return navigator['hardwareConcurrency'];
570568
},
571569

572-
{{{ USE_LSAN || USE_ASAN ? 'emscripten_builtin_' : '' }}}pthread_create__deps: ['$spawnThread', 'pthread_getschedparam', 'pthread_self', 'memalign', 'emscripten_sync_run_in_main_thread_4'],
570+
{{{ USE_LSAN || USE_ASAN ? 'emscripten_builtin_' : '' }}}pthread_create__deps: ['$spawnThread', 'pthread_self', 'memalign', 'emscripten_sync_run_in_main_thread_4'],
573571
{{{ USE_LSAN || USE_ASAN ? 'emscripten_builtin_' : '' }}}pthread_create: function(pthread_ptr, attr, start_routine, arg) {
574572
if (typeof SharedArrayBuffer === 'undefined') {
575573
err('Current environment does not support SharedArrayBuffer, pthreads are not available!');
@@ -696,8 +694,6 @@ var LibraryPThread = {
696694
var stackBase = 0;
697695
// Default thread attr is PTHREAD_CREATE_JOINABLE, i.e. start as not detached.
698696
var detached = 0;
699-
var schedPolicy = 0; /*SCHED_OTHER*/
700-
var schedPrio = 0;
701697
// When musl creates C11 threads it passes __ATTRP_C11_THREAD (-1) which
702698
// treat as if it was NULL.
703699
if (attr && attr != {{{ cDefine('__ATTRP_C11_THREAD') }}}) {
@@ -713,25 +709,6 @@ var LibraryPThread = {
713709
stackSize += 81920 /*DEFAULT_STACK_SIZE*/;
714710
stackBase = {{{ makeGetValue('attr', 8, 'i32') }}};
715711
detached = {{{ makeGetValue('attr', 12/*_a_detach*/, 'i32') }}} !== 0/*PTHREAD_CREATE_JOINABLE*/;
716-
var inheritSched = {{{ makeGetValue('attr', 16/*_a_sched*/, 'i32') }}} === 0/*PTHREAD_INHERIT_SCHED*/;
717-
if (inheritSched) {
718-
var prevSchedPolicy = {{{ makeGetValue('attr', 20/*_a_policy*/, 'i32') }}};
719-
var prevSchedPrio = {{{ makeGetValue('attr', 24/*_a_prio*/, 'i32') }}};
720-
// If we are inheriting the scheduling properties from the parent
721-
// thread, we need to identify the parent thread properly - this
722-
// function call may be getting proxied, in which case _pthread_self()
723-
// will point to the thread performing the proxying, not the thread that
724-
// initiated the call.
725-
var parentThreadPtr = PThread.currentProxiedOperationCallerThread ? PThread.currentProxiedOperationCallerThread : _pthread_self();
726-
_pthread_getschedparam(parentThreadPtr, attr + 20, attr + 24);
727-
schedPolicy = {{{ makeGetValue('attr', 20/*_a_policy*/, 'i32') }}};
728-
schedPrio = {{{ makeGetValue('attr', 24/*_a_prio*/, 'i32') }}};
729-
{{{ makeSetValue('attr', 20/*_a_policy*/, 'prevSchedPolicy', 'i32') }}};
730-
{{{ makeSetValue('attr', 24/*_a_prio*/, 'prevSchedPrio', 'i32') }}};
731-
} else {
732-
schedPolicy = {{{ makeGetValue('attr', 20/*_a_policy*/, 'i32') }}};
733-
schedPrio = {{{ makeGetValue('attr', 24/*_a_prio*/, 'i32') }}};
734-
}
735712
} else {
736713
// According to
737714
// http://man7.org/linux/man-pages/man3/pthread_create.3.html, default
@@ -779,8 +756,6 @@ var LibraryPThread = {
779756
stackBase: stackBase,
780757
stackSize: stackSize,
781758
allocatedOwnStack: allocatedOwnStack,
782-
schedPolicy: schedPolicy,
783-
schedPrio: schedPrio,
784759
detached: detached,
785760
startRoutine: start_routine,
786761
pthread_ptr: threadInfoStruct,
@@ -991,102 +966,6 @@ var LibraryPThread = {
991966
throw 'unwind';
992967
},
993968

994-
pthread_getschedparam: function(thread, policy, schedparam) {
995-
if (!policy && !schedparam) return ERRNO_CODES.EINVAL;
996-
997-
if (!thread) {
998-
err('pthread_getschedparam called with a null thread pointer!');
999-
return ERRNO_CODES.ESRCH;
1000-
}
1001-
var self = {{{ makeGetValue('thread', C_STRUCTS.pthread.self, 'i32') }}};
1002-
if (self !== thread) {
1003-
err('pthread_getschedparam attempted on thread ' + thread + ', which does not point to a valid thread, or does not exist anymore!');
1004-
return ERRNO_CODES.ESRCH;
1005-
}
1006-
1007-
var schedPolicy = Atomics.load(HEAPU32, (thread + {{{ C_STRUCTS.pthread.attr }}} + 20 ) >> 2);
1008-
var schedPrio = Atomics.load(HEAPU32, (thread + {{{ C_STRUCTS.pthread.attr }}} + 24 ) >> 2);
1009-
1010-
if (policy) {{{ makeSetValue('policy', 0, 'schedPolicy', 'i32') }}};
1011-
if (schedparam) {{{ makeSetValue('schedparam', 0, 'schedPrio', 'i32') }}};
1012-
return 0;
1013-
},
1014-
1015-
pthread_setschedparam: function(thread, policy, schedparam) {
1016-
if (!thread) {
1017-
err('pthread_setschedparam called with a null thread pointer!');
1018-
return ERRNO_CODES.ESRCH;
1019-
}
1020-
var self = {{{ makeGetValue('thread', C_STRUCTS.pthread.self, 'i32') }}};
1021-
if (self !== thread) {
1022-
err('pthread_setschedparam attempted on thread ' + thread + ', which does not point to a valid thread, or does not exist anymore!');
1023-
return ERRNO_CODES.ESRCH;
1024-
}
1025-
1026-
if (!schedparam) return ERRNO_CODES.EINVAL;
1027-
1028-
var newSchedPrio = {{{ makeGetValue('schedparam', 0, 'i32') }}};
1029-
if (newSchedPrio < 0) return ERRNO_CODES.EINVAL;
1030-
if (policy == 1/*SCHED_FIFO*/ || policy == 2/*SCHED_RR*/) {
1031-
if (newSchedPrio > 99) return ERRNO_CODES.EINVAL;
1032-
} else {
1033-
if (newSchedPrio > 1) return ERRNO_CODES.EINVAL;
1034-
}
1035-
1036-
Atomics.store(HEAPU32, (thread + {{{ C_STRUCTS.pthread.attr }}} + 20) >> 2, policy);
1037-
Atomics.store(HEAPU32, (thread + {{{ C_STRUCTS.pthread.attr }}} + 24) >> 2, newSchedPrio);
1038-
return 0;
1039-
},
1040-
1041-
// Marked as obsolescent in pthreads specification: http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_getconcurrency.html
1042-
pthread_getconcurrency: function() {
1043-
return 0;
1044-
},
1045-
1046-
// Marked as obsolescent in pthreads specification.
1047-
pthread_setconcurrency: function(new_level) {
1048-
// no-op
1049-
return 0;
1050-
},
1051-
1052-
pthread_mutexattr_getprioceiling: function(attr, prioceiling) {
1053-
// Not supported either in Emscripten or musl, return a faked value.
1054-
if (prioceiling) {{{ makeSetValue('prioceiling', 0, 99, 'i32') }}};
1055-
return 0;
1056-
},
1057-
1058-
pthread_mutexattr_setprioceiling: function(attr, prioceiling) {
1059-
// Not supported either in Emscripten or musl, return an error.
1060-
return ERRNO_CODES.EPERM;
1061-
},
1062-
1063-
pthread_getcpuclockid: function(thread, clock_id) {
1064-
return ERRNO_CODES.ENOENT; // pthread API recommends returning this error when "Per-thread CPU time clocks are not supported by the system."
1065-
},
1066-
1067-
pthread_setschedprio: function(thread, prio) {
1068-
if (!thread) {
1069-
err('pthread_setschedprio called with a null thread pointer!');
1070-
return ERRNO_CODES.ESRCH;
1071-
}
1072-
var self = {{{ makeGetValue('thread', C_STRUCTS.pthread.self, 'i32') }}};
1073-
if (self !== thread) {
1074-
err('pthread_setschedprio attempted on thread ' + thread + ', which does not point to a valid thread, or does not exist anymore!');
1075-
return ERRNO_CODES.ESRCH;
1076-
}
1077-
if (prio < 0) return ERRNO_CODES.EINVAL;
1078-
1079-
var schedPolicy = Atomics.load(HEAPU32, (thread + {{{ C_STRUCTS.pthread.attr }}} + 20 ) >> 2);
1080-
if (schedPolicy == 1/*SCHED_FIFO*/ || schedPolicy == 2/*SCHED_RR*/) {
1081-
if (prio > 99) return ERRNO_CODES.EINVAL;
1082-
} else {
1083-
if (prio > 1) return ERRNO_CODES.EINVAL;
1084-
}
1085-
1086-
Atomics.store(HEAPU32, (thread + {{{ C_STRUCTS.pthread.attr }}} + 24) >> 2, prio);
1087-
return 0;
1088-
},
1089-
1090969
pthread_cleanup_push__sig: 'vii',
1091970
pthread_cleanup_push: function(routine, arg) {
1092971
PThread.threadExitHandlers.push(function() { {{{ makeDynCall('vi', 'routine') }}}(arg) });
@@ -1097,11 +976,6 @@ var LibraryPThread = {
1097976
if (execute) routine();
1098977
},
1099978

1100-
pthread_atfork: function(prepare, parent, child) {
1101-
err('fork() is not supported: pthread_atfork is a no-op.');
1102-
return 0;
1103-
},
1104-
1105979
// Returns 0 on success, or one of the values -ETIMEDOUT, -EWOULDBLOCK or -EINVAL on error.
1106980
emscripten_futex_wait__deps: ['emscripten_main_thread_process_queued_calls'],
1107981
emscripten_futex_wait: function(addr, val, timeout) {

system/lib/libc/musl/src/conf/sysconf.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ long sysconf(int name)
8888
[_SC_THREAD_THREADS_MAX] = -1,
8989
[_SC_THREAD_ATTR_STACKADDR] = VER,
9090
[_SC_THREAD_ATTR_STACKSIZE] = VER,
91-
[_SC_THREAD_PRIORITY_SCHEDULING] = VER,
92-
[_SC_THREAD_PRIO_INHERIT] = 200809, // XXX EMSCRIPTEN replace -1,
93-
[_SC_THREAD_PRIO_PROTECT] = 200809, // XXX EMSCRIPTEN replace -1,
91+
[_SC_THREAD_PRIORITY_SCHEDULING] = -1, // XXX EMSCRIPTEN replace VER,
92+
[_SC_THREAD_PRIO_INHERIT] = -1,
93+
[_SC_THREAD_PRIO_PROTECT] = -1,
9494
[_SC_THREAD_PROCESS_SHARED] = VER,
9595
[_SC_NPROCESSORS_CONF] = JT_NPROCESSORS_CONF,
9696
[_SC_NPROCESSORS_ONLN] = JT_NPROCESSORS_ONLN,

system/lib/libc/musl/src/thread/pthread_atfork.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <pthread.h>
22
#include "libc.h"
33

4+
#ifndef __EMSCRIPTEN__ // XXX Emscripten fork() is not supported: pthread_atfork is a no-op
45
static struct atfork_funcs {
56
void (*prepare)(void);
67
void (*parent)(void);
@@ -29,9 +30,13 @@ void __fork_handler(int who)
2930
UNLOCK(lock);
3031
}
3132
}
33+
#endif
3234

3335
int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
3436
{
37+
#ifdef __EMSCRIPTEN__ // XXX Emscripten fork() is not supported: pthread_atfork is a no-op
38+
return 0;
39+
#else
3540
struct atfork_funcs *new = malloc(sizeof *new);
3641
if (!new) return -1;
3742

@@ -45,4 +50,5 @@ int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(vo
4550
funcs = new;
4651
UNLOCK(lock);
4752
return 0;
53+
#endif
4854
}

system/lib/libc/musl/src/thread/pthread_attr_setschedpolicy.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
int pthread_attr_setschedpolicy(pthread_attr_t *a, int policy)
44
{
5-
#ifdef __EMSCRIPTEN__ // XXX Emscripten: upstream this fix to musl.
6-
if (policy < SCHED_OTHER || (policy & ~SCHED_RESET_ON_FORK) > SCHED_IDLE) return EINVAL;
7-
#endif
85
a->_a_policy = policy;
96
return 0;
107
}

system/lib/libc/musl/src/thread/pthread_getconcurrency.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <pthread.h>
22

3+
// XXX Emscripten marked as obsolescent in pthreads specification:
4+
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_getconcurrency.html
35
int pthread_getconcurrency()
46
{
57
return 0;

system/lib/libc/musl/src/thread/pthread_getcpuclockid.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
int pthread_getcpuclockid(pthread_t t, clockid_t *clockid)
44
{
5+
#ifdef __EMSCRIPTEN__ // XXX Emscipten per-thread CPU time clocks are not supported
6+
// pthread API recommends returning this error when "Per-thread CPU time clocks are not supported by the system."
7+
return ENOENT;
8+
#else
59
*clockid = (-t->tid-1)*8U + 6;
610
return 0;
11+
#endif
712
}

system/lib/libc/musl/src/thread/pthread_getschedparam.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
int pthread_getschedparam(pthread_t t, int *restrict policy, struct sched_param *restrict param)
44
{
5+
#ifdef __EMSCRIPTEN__ // XXX Emscripten web or Node workers doesn't support prioritizing threads
6+
// no-op
7+
return 0;
8+
#else
59
int r;
610
__lock(t->killlock);
711
if (t->dead) {
@@ -14,4 +18,5 @@ int pthread_getschedparam(pthread_t t, int *restrict policy, struct sched_param
1418
}
1519
__unlock(t->killlock);
1620
return r;
21+
#endif
1722
}

system/lib/libc/musl/src/thread/pthread_setconcurrency.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
int pthread_setconcurrency(int val)
55
{
6+
#ifndef __EMSCRIPTEN__ // XXX Emscripten marked as obsolescent in pthreads specification
67
if (val < 0) return EINVAL;
78
if (val > 0) return EAGAIN;
9+
#endif
810
return 0;
911
}

system/lib/libc/musl/src/thread/pthread_setschedparam.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
int pthread_setschedparam(pthread_t t, int policy, const struct sched_param *param)
44
{
5+
#ifdef __EMSCRIPTEN__ // XXX Emscripten web or Node workers doesn't support prioritizing threads
6+
// no-op
7+
return 0;
8+
#else
59
int r;
610
__lock(t->killlock);
711
r = t->dead ? ESRCH : -__syscall(SYS_sched_setscheduler, t->tid, policy, param);
812
__unlock(t->killlock);
913
return r;
14+
#endif
1015
}

0 commit comments

Comments
 (0)