about summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorFabrice Bellard <fabrice@bellard.org>2024-01-02 16:08:48 +0100
committerbptato <nincsnevem662@gmail.com>2024-01-11 18:40:03 +0100
commitef449571b8e9be4eb098369d05fd679b432ba4f6 (patch)
tree9a32f1f7cd708b5afb2ccc42a134aa3d47060e9f /lib
parent7aa1f7014caa781231eb0d78584b663d081a550a (diff)
downloadchawan-ef449571b8e9be4eb098369d05fd679b432ba4f6.tar.gz
make JS_NewClassID thread safe
Diffstat (limited to 'lib')
-rw-r--r--lib/quickjs/quickjs.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/quickjs/quickjs.c b/lib/quickjs/quickjs.c
index 0a1ea0b0..87859259 100644
--- a/lib/quickjs/quickjs.c
+++ b/lib/quickjs/quickjs.c
@@ -3388,16 +3388,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;
 }