diff options
author | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2023-09-06 11:56:24 +0200 |
---|---|---|
committer | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2023-09-14 11:50:28 +0200 |
commit | 37af09adf0bc94f1a8212d4cae934c623e41f544 (patch) | |
tree | ece272e6c680aca8d4135bc3d65f9fc6860e4836 /src/dbus | |
parent | bfdefa81f83592f6432492206ebe720f57916846 (diff) |
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src/dbus')
-rw-r--r-- | src/dbus/qdbus_symbols_p.h | 48 |
1 files 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 <atomic> + #ifdef interface # undef interface #endif @@ -114,28 +116,34 @@ template <> struct TraceReturn<void> { 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<func_ptr> atomic_ptr; \ + func_ptr ptr = atomic_ptr.load(std::memory_order_relaxed); \ + DEBUGCALL(#func, argcall); \ + if (!ptr) { \ + ptr = reinterpret_cast<func_ptr>(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<func_ptr> atomic_ptr; \ + func_ptr ptr = atomic_ptr.load(std::memory_order_relaxed); \ + DEBUGCALL(#func, argcall); \ + if (!ptr) { \ + ptr = reinterpret_cast<func_ptr>(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 |