From 37af09adf0bc94f1a8212d4cae934c623e41f544 Mon Sep 17 00:00:00 2001 From: Ievgenii Meshcheriakov Date: Wed, 6 Sep 2023 11:56:24 +0200 Subject: [PATCH] QtDBus: Resolve libdbus functions in a thread-safe way Use atomic pointers. While at it, declare the function type with `using` instead of a typedef for better readability, do the declaration locally so that less preprocessor concatenation is needed, use reinterpret_cast instead of C-style cast. Change-Id: I5ed0d35b7ddfdd62ef6c12403fe7052019453f34 Reviewed-by: Thiago Macieira --- src/dbus/qdbus_symbols_p.h | 48 ++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/dbus/qdbus_symbols_p.h b/src/dbus/qdbus_symbols_p.h index 481a8b16f3..78b7e049ef 100644 --- a/src/dbus/qdbus_symbols_p.h +++ b/src/dbus/qdbus_symbols_p.h @@ -27,6 +27,8 @@ # include "dbus_minimal_p.h" #endif +#include + #ifdef interface # undef interface #endif @@ -114,28 +116,34 @@ template <> struct TraceReturn { typedef void Type; }; # define DEBUGRET(ret) # endif -# define DEFINEFUNC(ret, func, args, argcall, funcret) \ - typedef ret (* _q_PTR_##func) args; \ - static inline ret q_##func args \ - { \ - static _q_PTR_##func ptr; \ - DEBUGCALL(#func, argcall); \ - if (!ptr) \ - ptr = (_q_PTR_##func) qdbus_resolve_me(#func); \ - funcret DEBUGRET(ret) ptr argcall; \ +# define DEFINEFUNC(ret, func, args, argcall, funcret) \ + static inline ret q_##func args \ + { \ + using func_ptr = ret (*) args; \ + static std::atomic atomic_ptr; \ + func_ptr ptr = atomic_ptr.load(std::memory_order_relaxed); \ + DEBUGCALL(#func, argcall); \ + if (!ptr) { \ + ptr = reinterpret_cast(qdbus_resolve_me(#func)); \ + atomic_ptr.store(ptr, std::memory_order_relaxed); \ + } \ + funcret DEBUGRET(ret) ptr argcall; \ } -# define DEFINEFUNC_CONDITIONALLY(ret, func, args, argcall, funcret, failret) \ - typedef ret (* _q_PTR_##func) args; \ - static inline ret q_##func args \ - { \ - static _q_PTR_##func ptr; \ - DEBUGCALL(#func, argcall); \ - if (!ptr) \ - ptr = (_q_PTR_##func) qdbus_resolve_conditionally(#func); \ - if (!ptr) \ - failret; \ - funcret DEBUGRET(ret) ptr argcall; \ +# define DEFINEFUNC_CONDITIONALLY(ret, func, args, argcall, funcret, failret) \ + static inline ret q_##func args \ + { \ + using func_ptr = ret (*) args; \ + static std::atomic atomic_ptr; \ + func_ptr ptr = atomic_ptr.load(std::memory_order_relaxed); \ + DEBUGCALL(#func, argcall); \ + if (!ptr) { \ + ptr = reinterpret_cast(qdbus_resolve_conditionally(#func)); \ + atomic_ptr.store(ptr, std::memory_order_relaxed); \ + } \ + if (!ptr) \ + failret; \ + funcret DEBUGRET(ret) ptr argcall; \ } #else // defined QT_LINKED_LIBDBUS