OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LegacyLockMgr.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <rapidjson/document.h>
20 #include <boost/variant.hpp>
21 #include <map>
22 #include <mutex>
23 #include <tuple>
24 
25 #include "Catalog/Catalog.h"
27 #include "Shared/types.h"
29 
30 namespace legacylockmgr {
31 
32 // NOTE(sy): Currently the legacylockmgr is only used (or "abused") as a global lock.
33 
34 enum LockType { ExecutorOuterLock /*, LockMax*/ };
35 
36 template <typename MutexType>
37 class WrapperType final {
38  public:
40  : dmutex_(std::filesystem::path(g_base_path) / shared::kLockfilesDirectoryName /
41  "global.lockfile") {}
42 
43  void lock() {
44  if (!g_multi_instance) {
45  mutex_.lock();
46  } else {
47  dmutex_.lock();
48  }
49  }
50  bool try_lock() {
51  if (!g_multi_instance) {
52  return mutex_.try_lock();
53  } else {
54  return dmutex_.try_lock();
55  }
56  }
57  void unlock() {
58  if (!g_multi_instance) {
59  mutex_.unlock();
60  } else {
61  dmutex_.unlock();
62  }
63  }
64 
65  void lock_shared() {
66  if (!g_multi_instance) {
67  mutex_.lock_shared();
68  } else {
70  }
71  }
72  bool try_lock_shared() {
73  if (!g_multi_instance) {
74  return mutex_.try_lock_shared();
75  } else {
76  return dmutex_.try_lock_shared();
77  }
78  }
79  void unlock_shared() {
80  if (!g_multi_instance) {
81  mutex_.unlock_shared();
82  } else {
84  }
85  }
86 
87  private:
88  MutexType mutex_;
90 };
91 
92 template <typename MutexType, typename KeyType>
93 class LockMgr {
94  public:
95  static std::shared_ptr<WrapperType<MutexType>> getMutex(const LockType lockType,
96  const KeyType& key);
97 
98  private:
99  static std::mutex aMutex_;
100  static std::map<std::tuple<LockType, KeyType>, std::shared_ptr<WrapperType<MutexType>>>
102 };
103 
104 template <typename MutexType, typename KeyType>
106 template <typename MutexType, typename KeyType>
107 std::map<std::tuple<LockType, KeyType>, std::shared_ptr<WrapperType<MutexType>>>
109 
110 template <typename MutexType, typename KeyType>
111 std::shared_ptr<WrapperType<MutexType>> LockMgr<MutexType, KeyType>::getMutex(
112  const LockType lock_type,
113  const KeyType& key) {
114  auto lock_key = std::make_tuple(lock_type, key);
115 
116  std::unique_lock<std::mutex> lck(aMutex_);
117  auto mit = mutexMap_.find(lock_key);
118  if (mit != mutexMap_.end()) {
119  return mit->second;
120  }
121 
122  auto tMutex = std::make_shared<WrapperType<MutexType>>();
123  mutexMap_[lock_key] = tMutex;
124  return tMutex;
125 }
126 
127 using ExecutorWriteLock = std::unique_lock<WrapperType<std::shared_mutex>>;
128 using ExecutorReadLock = std::shared_lock<WrapperType<std::shared_mutex>>;
129 
130 inline auto getExecuteWriteLock() {
131  return ExecutorWriteLock(
133 }
134 
135 inline auto getExecuteReadLock() {
136  return ExecutorReadLock(
138 }
139 
140 } // namespace legacylockmgr
static std::shared_ptr< WrapperType< MutexType > > getMutex(const LockType lockType, const KeyType &key)
bool g_multi_instance
Definition: heavyai_locks.h:22
auto getExecuteReadLock()
std::unique_lock< WrapperType< std::shared_mutex >> ExecutorWriteLock
static std::map< std::tuple< LockType, KeyType >, std::shared_ptr< WrapperType< MutexType > > > mutexMap_
This file contains the class specification and related data structures for Catalog.
heavyai::DistributedSharedMutex dmutex_
Definition: LegacyLockMgr.h:89
std::shared_lock< WrapperType< std::shared_mutex >> ExecutorReadLock
std::string g_base_path
Definition: SysCatalog.cpp:62
static std::mutex aMutex_
Definition: LegacyLockMgr.h:99
auto getExecuteWriteLock()
const std::string kLockfilesDirectoryName