Skip to content

Commit 5ce2957

Browse files
committed
Make JS_NewClassID thread aware
It's as thread-safe as JSRuntime, which isn't thread-safe, but multiple threads can now allocate them on different runtimes without a problem.
1 parent b56a82d commit 5ce2957

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

examples/point.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,11 @@ static const JSCFunctionListEntry js_point_proto_funcs[] = {
123123
static int js_point_init(JSContext *ctx, JSModuleDef *m)
124124
{
125125
JSValue point_proto, point_class;
126+
JSRuntime *rt = JS_GetRuntime(ctx);
126127

127128
/* create the Point class */
128-
JS_NewClassID(&js_point_class_id);
129-
JS_NewClass(JS_GetRuntime(ctx), js_point_class_id, &js_point_class);
129+
JS_NewClassID(rt, &js_point_class_id);
130+
JS_NewClass(rt, js_point_class_id, &js_point_class);
130131

131132
point_proto = JS_NewObject(ctx);
132133
JS_SetPropertyFunctionList(ctx, point_proto, js_point_proto_funcs, countof(js_point_proto_funcs));

quickjs-libc.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,12 +1516,13 @@ static const JSCFunctionListEntry js_std_file_proto_funcs[] = {
15161516
static int js_std_init(JSContext *ctx, JSModuleDef *m)
15171517
{
15181518
JSValue proto;
1519+
JSRuntime *rt = JS_GetRuntime(ctx);
15191520

15201521
/* FILE class */
15211522
/* the class ID is created once */
1522-
JS_NewClassID(&js_std_file_class_id);
1523+
JS_NewClassID(rt, &js_std_file_class_id);
15231524
/* the class is created once per runtime */
1524-
JS_NewClass(JS_GetRuntime(ctx), js_std_file_class_id, &js_std_file_class);
1525+
JS_NewClass(rt, js_std_file_class_id, &js_std_file_class);
15251526
proto = JS_NewObject(ctx);
15261527
JS_SetPropertyFunctionList(ctx, proto, js_std_file_proto_funcs,
15271528
countof(js_std_file_proto_funcs));
@@ -3658,20 +3659,20 @@ static const JSCFunctionListEntry js_os_funcs[] = {
36583659

36593660
static int js_os_init(JSContext *ctx, JSModuleDef *m)
36603661
{
3662+
JSRuntime *rt = JS_GetRuntime(ctx);
36613663
os_poll_func = js_os_poll;
36623664

36633665
/* OSTimer class */
3664-
JS_NewClassID(&js_os_timer_class_id);
3665-
JS_NewClass(JS_GetRuntime(ctx), js_os_timer_class_id, &js_os_timer_class);
3666+
JS_NewClassID(rt, &js_os_timer_class_id);
3667+
JS_NewClass(rt, js_os_timer_class_id, &js_os_timer_class);
36663668

36673669
#ifdef USE_WORKER
36683670
{
3669-
JSRuntime *rt = JS_GetRuntime(ctx);
36703671
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
36713672
JSValue proto, obj;
36723673
/* Worker class */
3673-
JS_NewClassID(&js_worker_class_id);
3674-
JS_NewClass(JS_GetRuntime(ctx), js_worker_class_id, &js_worker_class);
3674+
JS_NewClassID(rt, &js_worker_class_id);
3675+
JS_NewClass(rt, js_worker_class_id, &js_worker_class);
36753676
proto = JS_NewObject(ctx);
36763677
JS_SetPropertyFunctionList(ctx, proto, js_worker_proto_funcs, countof(js_worker_proto_funcs));
36773678

quickjs.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ struct JSRuntime {
228228
JSAtomStruct **atom_array;
229229
int atom_free_index; /* 0 = none */
230230

231+
JSClassID js_class_id_alloc; /* counter for user defined classes */
231232
int class_count; /* size of class_array */
232233
JSClass *class_array;
233234

@@ -1149,7 +1150,6 @@ static const JSClassExoticMethods js_arguments_exotic_methods;
11491150
static const JSClassExoticMethods js_string_exotic_methods;
11501151
static const JSClassExoticMethods js_proxy_exotic_methods;
11511152
static const JSClassExoticMethods js_module_ns_exotic_methods;
1152-
static JSClassID js_class_id_alloc = JS_CLASS_INIT_COUNT;
11531153

11541154
static int compare_u32(uint32_t a, uint32_t b)
11551155
{
@@ -1483,6 +1483,8 @@ JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque)
14831483
if (init_shape_hash(rt))
14841484
goto fail;
14851485

1486+
rt->js_class_id_alloc = JS_CLASS_INIT_COUNT;
1487+
14861488
rt->stack_size = JS_DEFAULT_STACK_SIZE;
14871489
JS_UpdateStackTop(rt);
14881490

@@ -3181,13 +3183,11 @@ static inline BOOL JS_IsEmptyString(JSValueConst v)
31813183
/* JSClass support */
31823184

31833185
/* a new class ID is allocated if *pclass_id != 0 */
3184-
JSClassID JS_NewClassID(JSClassID *pclass_id)
3186+
JSClassID JS_NewClassID(JSRuntime *rt, JSClassID *pclass_id)
31853187
{
3186-
JSClassID class_id;
3187-
/* XXX: make it thread safe */
3188-
class_id = *pclass_id;
3188+
JSClassID class_id = *pclass_id;
31893189
if (class_id == 0) {
3190-
class_id = js_class_id_alloc++;
3190+
class_id = rt->js_class_id_alloc++;
31913191
*pclass_id = class_id;
31923192
}
31933193
return class_id;

quickjs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ typedef struct JSClassDef {
484484
JSClassExoticMethods *exotic;
485485
} JSClassDef;
486486

487-
JSClassID JS_NewClassID(JSClassID *pclass_id);
487+
JSClassID JS_NewClassID(JSRuntime *rt, JSClassID *pclass_id);
488488
int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def);
489489
int JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id);
490490

0 commit comments

Comments
 (0)