Fix bug on mgp dispatcher guard (#1225)

This commit is contained in:
Antonio Filipovic 2023-09-07 17:42:27 +02:00 committed by GitHub
parent d9464c6ffd
commit 974a6e3027
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -126,6 +126,12 @@ class MemoryDispatcher final {
map_.erase(this_id); map_.erase(this_id);
} }
bool IsThisThreadRegistered() noexcept {
const auto this_id = std::this_thread::get_id();
std::shared_lock lock(mut_);
return map_.contains(this_id);
}
private: private:
std::unordered_map<std::thread::id, mgp_memory *> map_; std::unordered_map<std::thread::id, mgp_memory *> map_;
std::shared_mutex mut_; std::shared_mutex mut_;
@ -136,7 +142,7 @@ class MemoryDispatcher final {
// header. The use of the 'mgp_memory *memory' pointer is deprecated // header. The use of the 'mgp_memory *memory' pointer is deprecated
// and will be removed in upcoming releases. // and will be removed in upcoming releases.
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
inline extern MemoryDispatcher mrd{}; inline MemoryDispatcher mrd{};
// TODO - Once we deprecate this we should remove this // TODO - Once we deprecate this we should remove this
// and make sure nothing relies on it anymore. This alone // and make sure nothing relies on it anymore. This alone
@ -164,7 +170,7 @@ class MemoryDispatcherGuard final {
// the mapping instead. // the mapping instead.
template <typename Func, typename... Args> template <typename Func, typename... Args>
inline decltype(auto) MemHandlerCallback(Func &&func, Args &&...args) { inline decltype(auto) MemHandlerCallback(Func &&func, Args &&...args) {
if (memory) { if (!mrd.IsThisThreadRegistered()) {
return std::forward<Func>(func)(std::forward<Args>(args)..., memory); return std::forward<Func>(func)(std::forward<Args>(args)..., memory);
} }
return std::forward<Func>(func)(std::forward<Args>(args)..., mrd.GetMemoryResource()); return std::forward<Func>(func)(std::forward<Args>(args)..., mrd.GetMemoryResource());