Skip to content

Commit

Permalink
fix mutex usage in session_store.h
Browse files Browse the repository at this point in the history
  • Loading branch information
t-horikawa committed Apr 19, 2024
1 parent 4d78934 commit b24f148
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions include/tateyama/api/server/session_store.h
Expand Up @@ -42,7 +42,7 @@ 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_element");
boost::unique_lock<boost::shared_mutex> lock(mtx_);
boost::unique_lock<boost::upgrade_mutex> lock(mtx_);

if (auto itr = element_map_.find(element_id); itr != element_map_.end()) {
return false;
Expand All @@ -61,7 +61,7 @@ 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_element");
boost::shared_lock<boost::shared_mutex> lock(mtx_);
boost::shared_lock<boost::upgrade_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 @@ -84,13 +84,13 @@ 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_element");
boost::shared_lock<boost::shared_mutex> sh_lock(mtx_);
boost::upgrade_lock<boost::upgrade_mutex> ug_lock(mtx_);

if (auto itr = element_map_.find(element_id); itr != element_map_.end()) {
return std::dynamic_pointer_cast<T>(itr->second);
}
{
boost::upgrade_lock<boost::shared_mutex> ex_lock(mtx_);
boost::upgrade_to_unique_lock<boost::upgrade_mutex> ex_lock(ug_lock);
element_map_.emplace(element_id, std::make_shared<T>(std::forward<Args>(args)...));
}
return {};
Expand All @@ -106,7 +106,7 @@ 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_element");
boost::unique_lock<boost::shared_mutex> lock(mtx_);
boost::unique_lock<boost::upgrade_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 @@ -123,7 +123,7 @@ class session_store {
* @throws std::runtime_error if failed to dispose this session store
*/
void dispose() {
boost::unique_lock<boost::shared_mutex> lock(mtx_);
boost::unique_lock<boost::upgrade_mutex> lock(mtx_);

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

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

}

0 comments on commit b24f148

Please sign in to comment.