@@ -57450,6 +57450,49 @@ static pthread_mutex_t js_atomics_mutex = PTHREAD_MUTEX_INITIALIZER;
5745057450static struct list_head js_atomics_waiter_list =
5745157451 LIST_HEAD_INIT(js_atomics_waiter_list);
5745257452
57453+ #if defined(__aarch64__)
57454+ static inline void cpu_pause(void)
57455+ {
57456+ asm volatile("yield" ::: "memory");
57457+ }
57458+ #elif defined(__x86_64) || defined(__i386__)
57459+ static inline void cpu_pause(void)
57460+ {
57461+ asm volatile("pause" ::: "memory");
57462+ }
57463+ #else
57464+ static inline void cpu_pause(void)
57465+ {
57466+ }
57467+ #endif
57468+
57469+ // no-op: Atomics.pause() is not allowed to block or yield to another
57470+ // thread, only to hint the CPU that it should back off for a bit;
57471+ // the amount of work we do here is a good enough substitute
57472+ static JSValue js_atomics_pause(JSContext *ctx, JSValueConst this_obj,
57473+ int argc, JSValueConst *argv)
57474+ {
57475+ double d;
57476+
57477+ if (argc > 0) {
57478+ switch (JS_VALUE_GET_TAG(argv[0])) {
57479+ case JS_TAG_FLOAT64: // accepted if and only if fraction == 0.0
57480+ d = JS_VALUE_GET_FLOAT64(argv[0]);
57481+ if (isfinite(d))
57482+ if (0 == modf(d, &d))
57483+ break;
57484+ // fallthru
57485+ default:
57486+ return JS_ThrowTypeError(ctx, "not an integral number");
57487+ case JS_TAG_UNDEFINED:
57488+ case JS_TAG_INT:
57489+ break;
57490+ }
57491+ }
57492+ cpu_pause();
57493+ return JS_UNDEFINED;
57494+ }
57495+
5745357496static JSValue js_atomics_wait(JSContext *ctx,
5745457497 JSValueConst this_obj,
5745557498 int argc, JSValueConst *argv)
@@ -57587,6 +57630,7 @@ static const JSCFunctionListEntry js_atomics_funcs[] = {
5758757630 JS_CFUNC_MAGIC_DEF("load", 2, js_atomics_op, ATOMICS_OP_LOAD ),
5758857631 JS_CFUNC_DEF("store", 3, js_atomics_store ),
5758957632 JS_CFUNC_DEF("isLockFree", 1, js_atomics_isLockFree ),
57633+ JS_CFUNC_DEF("pause", 0, js_atomics_pause ),
5759057634 JS_CFUNC_DEF("wait", 4, js_atomics_wait ),
5759157635 JS_CFUNC_DEF("notify", 3, js_atomics_notify ),
5759257636 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Atomics", JS_PROP_CONFIGURABLE ),
0 commit comments