Skip to content

Commit

Permalink
apply comments
Browse files Browse the repository at this point in the history
  • Loading branch information
t-horikawa committed Apr 18, 2024
1 parent d0f141f commit d7514a5
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions include/tateyama/api/server/session_store.h
Expand Up @@ -16,7 +16,7 @@
#pragma once

#include <map>
#include <shared_mutex>
#include <boost/thread/shared_mutex.hpp>

#include <tateyama/api/server/session_element.h>

Expand All @@ -41,8 +41,8 @@ class session_store {
*/
template<class T> // static_assert(std::is_base_of_v<session_element, T>)
bool put(id_type element_id, std::shared_ptr<T> element) {
static_assert(std::is_base_of<session_element, T>::value, "T is not a derived class of session_elemsnt");
std::lock_guard<std::shared_mutex> lock(mtx_);
static_assert(std::is_base_of<session_element, T>::value, "T is not a derived class of session_element");
boost::unique_lock<boost::shared_mutex> lock(mtx_);

if (auto itr = element_map_.find(element_id); itr != element_map_.end()) {
return false;
Expand All @@ -60,8 +60,8 @@ class session_store {
*/
template<class T> // static_assert(std::is_base_of_v<session_element, T>)
[[nodiscard]] std::shared_ptr<T> find(id_type element_id) const {
static_assert(std::is_base_of<session_element, T>::value, "T is not a derived class of session_elemsnt");
std::shared_lock<std::shared_mutex> lock(const_cast<session_store*>(this)->mtx_);
static_assert(std::is_base_of<session_element, T>::value, "T is not a derived class of session_element");
boost::shared_lock<boost::shared_mutex> lock(mtx_);

if (auto itr = element_map_.find(element_id); itr != element_map_.end()) {
return std::dynamic_pointer_cast<T>(itr->second);
Expand All @@ -83,13 +83,16 @@ class session_store {
*/
template<class T, class... Args> // static_assert(std::is_base_of_v<session_element, T>)
[[nodiscard]] std::shared_ptr<T> find_or_emplace(id_type element_id, Args&&...args) {
static_assert(std::is_base_of<session_element, T>::value, "T is not a derived class of session_elemsnt");
std::shared_lock<std::shared_mutex> lock(mtx_);
static_assert(std::is_base_of<session_element, T>::value, "T is not a derived class of session_element");
boost::shared_lock<boost::shared_mutex> sh_lock(mtx_);

if (auto itr = element_map_.find(element_id); itr != element_map_.end()) {
return std::dynamic_pointer_cast<T>(itr->second);
}
element_map_.emplace(element_id, std::make_shared<T>(std::forward<Args>(args)...));
{
boost::upgrade_lock<boost::shared_mutex> ex_lock(mtx_);
element_map_.emplace(element_id, std::make_shared<T>(std::forward<Args>(args)...));
}
return {};
}

Expand All @@ -102,8 +105,8 @@ class session_store {
*/
template<class T> // static_assert(std::is_base_of_v<session_element, T>)
bool remove(id_type element_id) {
static_assert(std::is_base_of<session_element, T>::value, "T is not a derived class of session_elemsnt");
std::lock_guard<std::shared_mutex> lock(mtx_);
static_assert(std::is_base_of<session_element, T>::value, "T is not a derived class of session_element");
boost::unique_lock<boost::shared_mutex> lock(mtx_);

if (auto itr = element_map_.find(element_id); itr != element_map_.end()) {
if (std::dynamic_pointer_cast<T>(itr->second)) {
Expand All @@ -120,7 +123,7 @@ class session_store {
* @throws std::runtime_error if failed to dispose this session store
*/
void dispose() {
std::lock_guard<std::shared_mutex> lock(mtx_);
boost::unique_lock<boost::shared_mutex> lock(mtx_);

for (auto&& e: element_map_) {
e.second->dispose();
Expand All @@ -129,7 +132,7 @@ class session_store {

private:
std::map<id_type, std::shared_ptr<session_element>> element_map_{};
std::shared_mutex mtx_{};
mutable boost::shared_mutex mtx_{};
};

}

0 comments on commit d7514a5

Please sign in to comment.