From 38adedd55833c32534851efbbe72a90fcf226dd6 Mon Sep 17 00:00:00 2001 From: Jorrit Rouwe Date: Mon, 21 Oct 2024 21:12:52 +0200 Subject: [PATCH] Make temporaries in the glue script thread safe This emits all temporaries as 'static thread_local temp;' instead of 'static temp;' and prevents race conditions when you use the resulting library in a multithreaded environment. When compiling without -pthread, the thread_local attribute is ignored and the generated code is as it was before. --- tools/webidl_binder.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/webidl_binder.py b/tools/webidl_binder.py index 9cdb6a83f57db..d7ce3acfc8c3f 100644 --- a/tools/webidl_binder.py +++ b/tools/webidl_binder.py @@ -629,7 +629,8 @@ def make_call_args(i): if non_pointer: return_prefix += '&' if copy: - pre += ' static %s temp;\n' % type_to_c(return_type, non_pointing=True) + # Avoid sharing this static temp var between threads, which could race. + pre += ' static thread_local %s temp;\n' % type_to_c(return_type, non_pointing=True) return_prefix += '(temp = ' return_postfix += ', &temp)'