From 8de4538ff3761e5d0adfa317069a722941848ce1 Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Tue, 2 Jan 2024 16:08:48 +0100 Subject: [PATCH] make JS_NewClassID thread safe --- quickjs.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index 7eaee1a..06cf581 100644 --- a/quickjs.c +++ b/quickjs.c @@ -3384,16 +3384,25 @@ static inline BOOL JS_IsEmptyString(JSValueConst v) /* JSClass support */ +#ifdef CONFIG_ATOMICS +static pthread_mutex_t js_class_id_mutex = PTHREAD_MUTEX_INITIALIZER; +#endif + /* a new class ID is allocated if *pclass_id != 0 */ JSClassID JS_NewClassID(JSClassID *pclass_id) { JSClassID class_id; - /* XXX: make it thread safe */ +#ifdef CONFIG_ATOMICS + pthread_mutex_lock(&js_class_id_mutex); +#endif class_id = *pclass_id; if (class_id == 0) { class_id = js_class_id_alloc++; *pclass_id = class_id; } +#ifdef CONFIG_ATOMICS + pthread_mutex_unlock(&js_class_id_mutex); +#endif return class_id; }