Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cpp/core/utils/ObjectStore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

// static
std::unique_ptr<gluten::ObjectStore> gluten::ObjectStore::create() {
static std::mutex mtx;
std::lock_guard<std::mutex> lock(mtx);
static std::shared_mutex mtx;
std::unique_lock lock(mtx);
StoreHandle nextId = stores().nextId();
auto store = std::unique_ptr<gluten::ObjectStore>(new gluten::ObjectStore(nextId));
StoreHandle storeId = safeCast<StoreHandle>(stores().insert(store.get()));
Expand Down Expand Up @@ -51,7 +51,7 @@ gluten::ObjectStore::~ObjectStore() {
}
std::shared_ptr<void> tempObj;
{
const std::lock_guard<std::mutex> lock(mtx_);
std::unique_lock lock(mtx_);
// destructing in reversed order (the last added object destructed first)
auto itr = aliveObjects_.rbegin();
const ResourceHandle handle = (*itr).first;
Expand All @@ -75,7 +75,7 @@ gluten::ObjectStore::~ObjectStore() {
}

void gluten::ObjectStore::releaseInternal(gluten::ResourceHandle handle) {
const std::lock_guard<std::mutex> lock(mtx_);
std::unique_lock lock(mtx_);
store_.erase(handle);
aliveObjects_.erase(handle);
}
7 changes: 4 additions & 3 deletions cpp/core/utils/ObjectStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include <map>
#include <shared_mutex>
#include "utils/ResourceMap.h"

namespace gluten {
Expand Down Expand Up @@ -70,7 +71,7 @@ class ObjectStore {

template <typename T>
ObjectHandle save(std::shared_ptr<T> obj) {
const std::lock_guard<std::mutex> lock(mtx_);
std::unique_lock lock(mtx_);
const std::string_view typeName = typeid(T).name();
const size_t size = SafeSizeOf<T>::value;
ResourceHandle handle = store_.insert(std::move(obj));
Expand All @@ -96,7 +97,7 @@ class ObjectStore {

template <typename T>
std::shared_ptr<T> retrieveInternal(ResourceHandle handle) {
const std::lock_guard<std::mutex> lock(mtx_);
std::shared_lock lock(mtx_);
std::shared_ptr<void> object = store_.lookup(handle);
// Programming carefully. This will lead to ub if wrong typename T was passed in.
auto casted = std::static_pointer_cast<T>(object);
Expand All @@ -110,6 +111,6 @@ class ObjectStore {
ResourceMap<std::shared_ptr<void>> store_;
// Preserves handles of objects in the store in order, with additional attributes associated with them.
std::map<ResourceHandle, ObjectDebugInfo> aliveObjects_{};
std::mutex mtx_;
std::shared_mutex mtx_;
};
} // namespace gluten
Loading