8000 - added Atomics.pause (bnoordhuis) · bellard/quickjs@8807fed · GitHub
[go: up one dir, main page]

Skip to content

Commit 8807fed

Browse files
author
Fabrice Bellard
committed
- added Atomics.pause (bnoordhuis)
- use the pause instruction for x86 and ARM64 in Atomics.pause()
1 parent f021d77 commit 8807fed

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

quickjs.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57450,6 +57450,49 @@ static pthread_mutex_t js_atomics_mutex = PTHREAD_MUTEX_INITIALIZER;
5745057450
static 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+
5745357496
static 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 ),

test262.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ arrow-function
6969
async-functions
7070
async-iteration
7171
Atomics
72-
Atomics.pause=skip
72+
Atomics.pause
7373
Atomics.waitAsync=skip
7474
BigInt
7575
caller

0 commit comments

Comments
 (0)
0