Skip to content

Commit 0bf36b9

Browse files
authored
Fix ThreadSanitizer warning in quickjs-libc (#562)
A global function pointer was getting accessed from multiple threads. Replace it with an atomic integer and an immediate function call.
1 parent 6d63b36 commit 0bf36b9

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

quickjs-libc.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ extern char **environ;
8787

8888
#ifdef USE_WORKER
8989
#include <pthread.h>
90-
#include "quickjs-c-atomics.h"
9190
#endif
9291

9392
#include "cutils.h"
9493
#include "list.h"
94+
#include "quickjs-c-atomics.h"
9595
#include "quickjs-libc.h"
9696

9797
#define MAX_SAFE_INTEGER (((int64_t) 1 << 53) - 1)
@@ -167,7 +167,7 @@ typedef struct JSThreadState {
167167
} JSThreadState;
168168

169169
static uint64_t os_pending_signals;
170-
static int (*os_poll_func)(JSContext *ctx);
170+
static _Atomic int can_js_os_poll;
171171

172172
static void js_std_dbuf_init(JSContext *ctx, DynBuf *s)
173173
{
@@ -3827,7 +3827,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
38273827

38283828
static int js_os_init(JSContext *ctx, JSModuleDef *m)
38293829
{
3830-
os_poll_func = js_os_poll;
3830+
atomic_store(&can_js_os_poll, TRUE);
38313831

38323832
#ifdef USE_WORKER
38333833
{
@@ -4058,7 +4058,7 @@ JSValue js_std_loop(JSContext *ctx)
40584058
}
40594059
}
40604060

4061-
if (!os_poll_func || os_poll_func(ctx))
4061+
if (!atomic_load(&can_js_os_poll) || js_os_poll(ctx))
40624062
break;
40634063
}
40644064
done:
@@ -4090,8 +4090,8 @@ JSValue js_std_await(JSContext *ctx, JSValue obj)
40904090
if (err < 0) {
40914091
js_std_dump_error(ctx1);
40924092
}
4093-
if (os_poll_func)
4094-
os_poll_func(ctx);
4093+
if (atomic_load(&can_js_os_poll))
4094+
js_os_poll(ctx);
40954095
} else {
40964096
/* not a promise */
40974097
ret = obj;

0 commit comments

Comments
 (0)