Skip to content

Commit fc35f34

Browse files
committed
Merge branch 'main' into tty-test
2 parents e9a03c0 + 7fba652 commit fc35f34

16 files changed

Lines changed: 73 additions & 81 deletions

src/deterministic.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ function deterministicNow() {
1717

1818
Date.now = deterministicNow;
1919

20-
// Setting performance.now to deterministicNow doesn't work so we instead
21-
// use a helper function in parseTools (getPerformanceNow()) to call it
22-
// directly.
23-
// if (globalThis.performance) performance.now = Date.now;
20+
// Note: this approach does not work on certain versions of Node.js
21+
// Specifically it seems like its not possible to override performance.now on
22+
// node v16 through v18.
23+
// See getPerformanceNow in parseTools.mjs for how we deal with this.
24+
if (globalThis.performance) performance.now = deterministicNow;
2425

2526
Module['thisProgram'] = 'thisProgram'; // for consistency between different builds than between runs of the same build
2627

src/jsifier.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,9 @@ function(${args}) {
713713
} else if (typeof snippet == 'object') {
714714
snippet = stringifyWithFunctions(snippet);
715715
addImplicitDeps(snippet, deps);
716+
} else if (typeof snippet == 'string' && (snippet.match(/^\s*\([^}]*\)\s*=>/) || snippet.match(/^function\b/))) {
717+
// Support functions that are already "stringified"
718+
isFunction = true;
716719
}
717720

718721
if (isFunction) {

src/lib/libcore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2593,7 +2593,7 @@ function wrapSyscallFunction(x, library, isWasi) {
25932593
t = modifyJSFunction(t, (args, body, async_) => `${async_}function (${args}) {\n${pre}${body}${post}}\n`);
25942594
}
25952595

2596-
library[x] = eval(`(${t})`);
2596+
library[x] = t;
25972597
// Automatically add dependency on `$SYSCALLS`
25982598
if (!WASMFS && t.includes('SYSCALLS')) {
25992599
library[x + '__deps'].push('$SYSCALLS');

src/lib/libsyscall.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,8 +1028,8 @@ var SyscallsLibrary = {
10281028
},
10291029
};
10301030

1031-
for (var x in SyscallsLibrary) {
1032-
wrapSyscallFunction(x, SyscallsLibrary, false);
1031+
for (const name of Object.keys(SyscallsLibrary)) {
1032+
wrapSyscallFunction(name, SyscallsLibrary, false);
10331033
}
10341034

10351035
addToLibrary(SyscallsLibrary);

src/lib/libwasi.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,8 @@ var WasiLibrary = {
620620
random_get: (buffer, size) => randomFill(HEAPU8.subarray(buffer, buffer + size)),
621621
};
622622

623-
for (var x in WasiLibrary) {
624-
wrapSyscallFunction(x, WasiLibrary, true);
623+
for (const name of Object.keys(WasiLibrary)) {
624+
wrapSyscallFunction(name, WasiLibrary, true);
625625
}
626626

627627
addToLibrary(WasiLibrary);

src/parseTools.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,10 @@ function formattedMinNodeVersion() {
11061106
}
11071107

11081108
function getPerformanceNow() {
1109-
if (DETERMINISTIC) {
1109+
// This is needed to support Node.js v16 - v18 where `performance.now`
1110+
// cannot be overridden in the normal way.
1111+
// TODO(sbc): remove this once we drop support for these versions.
1112+
if (DETERMINISTIC && ENVIRONMENT_MAY_BE_NODE) {
11101113
return 'deterministicNow';
11111114
} else {
11121115
return 'performance.now';

system/lib/pthread/emscripten_futex_wait.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,15 @@ int emscripten_futex_wait(volatile void *addr, uint32_t val, double max_wait_ms)
172172
wakeup_interval = 100 * 1000000;
173173
}
174174

175-
// When wakeup_interval is set, we use remainder_ns to track how many ns
176-
// remain of the intiial max_wait_ns.
177-
int64_t remainder_ns = 0;
175+
// When wakeup_interval is set, we use end_time to track the absolute
176+
// time when the wait should end.
177+
double end_time = 0;
178178
if (wakeup_interval) {
179-
remainder_ns = max_wait_ns;
180-
if (remainder_ns < 0) {
179+
if (max_wait_ms == INFINITY) {
181180
max_wait_ns = wakeup_interval;
182181
} else {
183-
max_wait_ns = MIN(remainder_ns, wakeup_interval);
182+
end_time = emscripten_get_now() + max_wait_ms;
183+
max_wait_ns = MIN(max_wait_ns, wakeup_interval);
184184
}
185185
}
186186

@@ -222,14 +222,12 @@ int emscripten_futex_wait(volatile void *addr, uint32_t val, double max_wait_ms)
222222
emscripten_conditional_set_current_thread_status(EM_THREAD_STATUS_WAITFUTEX, EM_THREAD_STATUS_RUNNING);
223223
return -ECANCELED;
224224
}
225-
// If remainder_ns is negative it means we want wait forever, and we don't
226-
// need to decrement remainder_ns in that case.
227-
if (wakeup_interval && remainder_ns >= 0) {
228-
remainder_ns -= wakeup_interval;
229-
if (remainder_ns <= 0) {
225+
if (wakeup_interval && max_wait_ms != INFINITY) {
226+
double remainder_ms = end_time - emscripten_get_now();
227+
if (remainder_ms <= 0) {
230228
break;
231229
}
232-
max_wait_ns = MIN(remainder_ns, wakeup_interval);
230+
max_wait_ns = MIN((int64_t)(remainder_ms * 1e6), wakeup_interval);
233231
}
234232
} while (wakeup_interval && ret == ATOMICS_WAIT_TIMED_OUT);
235233
#endif

system/lib/pthread/library_pthread.c

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -65,54 +65,32 @@ int sched_get_priority_min(int policy) {
6565
return 0;
6666
}
6767

68-
int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *restrict attr, int *restrict prioceiling)
69-
{
68+
int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *restrict attr, int *restrict prioceiling) {
7069
// Not supported either in Emscripten or musl, return a faked value.
7170
if (prioceiling) *prioceiling = 99;
7271
return 0;
7372
}
7473

75-
int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling)
76-
{
74+
int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling) {
7775
// Not supported either in Emscripten or musl, return an error.
7876
return EPERM;
7977
}
8078

81-
static uint32_t dummyZeroAddress = 0;
82-
8379
void emscripten_thread_sleep(double msecs) {
84-
double now = emscripten_get_now();
85-
double target = now + msecs;
86-
87-
// If we have less than this many msecs left to wait, busy spin that instead.
88-
double min_ms_slice_to_sleep = 0.1;
89-
90-
// Break up sleeping so that we process proxied work at regular intervals.
91-
// TODO(sbc): This should be removed and/or moved down into
92-
// `emscripten_futex_wait`.
93-
double max_ms_slice_to_sleep = 100;
94-
95-
emscripten_conditional_set_current_thread_status(
96-
EM_THREAD_STATUS_RUNNING, EM_THREAD_STATUS_SLEEPING);
97-
98-
do {
99-
// Keep processing the main loop of the calling thread.
100-
__pthread_testcancel(); // pthreads spec: sleep is a cancellation point, so must test if this
101-
// thread is cancelled during the sleep.
102-
emscripten_current_thread_process_queued_calls();
103-
104-
now = emscripten_get_now();
105-
double ms_to_sleep = target - now;
106-
if (ms_to_sleep < min_ms_slice_to_sleep)
107-
continue;
108-
if (ms_to_sleep > max_ms_slice_to_sleep)
109-
ms_to_sleep = max_ms_slice_to_sleep;
110-
emscripten_futex_wait(&dummyZeroAddress, 0, ms_to_sleep);
111-
now = emscripten_get_now();
112-
} while (now < target);
113-
114-
emscripten_conditional_set_current_thread_status(
115-
EM_THREAD_STATUS_SLEEPING, EM_THREAD_STATUS_RUNNING);
80+
// We include emscripten_current_thread_process_queued_calls before and
81+
// after sleeping since that is how we recieve "async" signals.
82+
// We include __pthread_testcancel here becuase clock_nanosleep is
83+
// a pthread cancelation point.
84+
emscripten_current_thread_process_queued_calls();
85+
__pthread_testcancel();
86+
emscripten_conditional_set_current_thread_status(EM_THREAD_STATUS_RUNNING,
87+
EM_THREAD_STATUS_SLEEPING);
88+
uint32_t dummyZeroAddress = 0;
89+
emscripten_futex_wait(&dummyZeroAddress, 0, msecs);
90+
emscripten_conditional_set_current_thread_status(EM_THREAD_STATUS_SLEEPING,
91+
EM_THREAD_STATUS_RUNNING);
92+
emscripten_current_thread_process_queued_calls();
93+
__pthread_testcancel();
11694
}
11795

11896
static struct pthread __main_pthread;

system/lib/pthread/library_pthread_stub.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ int pthread_cancel(pthread_t thread) {
231231
return 0;
232232
}
233233

234-
void pthread_testcancel() {}
234+
void __pthread_testcancel() {}
235+
236+
weak_alias(__pthread_testcancel, pthread_testcancel);
235237

236238
_Noreturn void __pthread_exit(void* status) {
237239
exit(0);

test/codesize/test_codesize_cxx_mangle.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"a.out.js": 23224,
33
"a.out.js.gz": 8983,
44
"a.out.nodebug.wasm": 238957,
5-
"a.out.nodebug.wasm.gz": 79847,
5+
"a.out.nodebug.wasm.gz": 79842,
66
"total": 262181,
7-
"total_gz": 88830,
7+
"total_gz": 88825,
88
"sent": [
99
"__cxa_begin_catch",
1010
"__cxa_end_catch",

0 commit comments

Comments
 (0)