OmniSciDB  dfae7c3b14
LegacyLockMgr.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 
33 
34 template <typename MutexType, typename KeyType>
35 class LockMgr {
36  public:
37  static std::shared_ptr<MutexType> getMutex(const LockType lockType, const KeyType& key);
38 
39  private:
40  static std::mutex aMutex_;
41  static std::map<std::tuple<LockType, KeyType>, std::shared_ptr<MutexType>> mutexMap_;
42 };
43 
44 template <typename MutexType, typename KeyType>
46 template <typename MutexType, typename KeyType>
47 std::map<std::tuple<LockType, KeyType>, std::shared_ptr<MutexType>>
49 
50 template <typename MutexType, typename KeyType>
51 std::shared_ptr<MutexType> LockMgr<MutexType, KeyType>::getMutex(const LockType lock_type,
52  const KeyType& key) {
53  auto lock_key = std::make_tuple(lock_type, key);
54 
55  std::unique_lock<std::mutex> lck(aMutex_);
56  auto mit = mutexMap_.find(lock_key);
57  if (mit != mutexMap_.end()) {
58  return mit->second;
59  }
60 
61  auto tMutex = std::make_shared<MutexType>();
62  mutexMap_[lock_key] = tMutex;
63  return tMutex;
64 }
65 
66 } // namespace legacylockmgr
This file contains the class specification and related data structures for Catalog.
static std::mutex aMutex_
Definition: LegacyLockMgr.h:40
static std::shared_ptr< MutexType > getMutex(const LockType lockType, const KeyType &key)
Definition: LegacyLockMgr.h:51
static std::map< std::tuple< LockType, KeyType >, std::shared_ptr< MutexType > > mutexMap_
Definition: LegacyLockMgr.h:41