* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <set>
#include "utils/ResourceMap.h"
namespace gluten {
using StoreHandle = int32_t;
using ObjectHandle = int64_t;
constexpr static ObjectHandle kInvalidObjectHandle = -1;
class ObjectStore {
public:
static std::unique_ptr<ObjectStore> create() {
static std::mutex mtx;
std::lock_guard<std::mutex> lock(mtx);
StoreHandle nextId = stores().nextId();
auto store = std::unique_ptr<ObjectStore>(new ObjectStore(nextId));
StoreHandle storeId = safeCast<StoreHandle>(stores().insert(store.get()));
GLUTEN_CHECK(storeId == nextId, "Store ID mismatched, this should not happen");
return store;
}
static void release(ObjectHandle handle) {
ResourceHandle storeId = safeCast<ResourceHandle>(handle >> (sizeof(ResourceHandle) * 8));
ResourceHandle resourceId = safeCast<ResourceHandle>(handle & std::numeric_limits<ResourceHandle>::max());
auto store = stores().lookup(storeId);
store->releaseInternal(resourceId);
}
template <typename T>
static std::shared_ptr<T> retrieve(ObjectHandle handle) {
ResourceHandle storeId = safeCast<ResourceHandle>(handle >> (sizeof(ResourceHandle) * 8));
ResourceHandle resourceId = safeCast<ResourceHandle>(handle & std::numeric_limits<ResourceHandle>::max());
auto store = stores().lookup(storeId);
return store->retrieveInternal<T>(resourceId);
}
virtual ~ObjectStore();
StoreHandle id() {
return storeId_;
}
ObjectHandle save(std::shared_ptr<void> obj);
private:
static ResourceMap<ObjectStore*>& stores();
ObjectHandle toObjHandle(ResourceHandle rh) {
ObjectHandle prefix = static_cast<ObjectHandle>(storeId_) << (sizeof(ResourceHandle) * 8);
ObjectHandle objHandle = prefix + rh;
return objHandle;
}
template <typename T>
std::shared_ptr<T> retrieveInternal(ResourceHandle handle) {
const std::lock_guard<std::mutex> lock(mtx_);
std::shared_ptr<void> object = store_.lookup(handle);
auto casted = std::static_pointer_cast<T>(object);
return casted;
}
void releaseInternal(ResourceHandle handle);
ObjectStore(StoreHandle storeId) : storeId_(storeId){};
StoreHandle storeId_;
ResourceMap<std::shared_ptr<void>> store_;
std::set<ResourceHandle> aliveObjects_;
std::mutex mtx_;
};
}