OmniSciDB  c1a53651b2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
heavyai_locks.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 <filesystem>
20 
21 inline bool g_multi_instance{
22  false}; // TODO(sy): set true after internal testing is complete
24 
25 namespace heavyai {
26 
27 // MutexInterface:
28 // An exclusive mutex compatible with std::unique_lock.
30  public:
31  virtual void lock() = 0;
32  virtual bool try_lock() = 0;
33  virtual void unlock() = 0;
34 
35  virtual ~MutexInterface() {}
36 };
37 
38 // SharedMutexInterface:
39 // A sharable mutex compatible with std::unique_lock and std::shared_lock.
41  public:
42  virtual void lock_shared() = 0;
43  virtual bool try_lock_shared() = 0;
44  virtual void unlock_shared() = 0;
45 
46  virtual ~SharedMutexInterface() {}
47 };
48 
50  public:
51  struct Callbacks {
52  std::function<void(bool /*write*/)> pre_lock_callback;
53  std::function<void(size_t /*version*/)> reload_cache_callback;
54  std::function<void(bool /*write*/)> post_lock_callback;
55  std::function<void(bool /*write*/)> pre_unlock_callback;
56  std::function<void(bool /*write*/)> post_unlock_callback;
57  };
58 
59  DistributedSharedMutex(std::filesystem::path lockfilename,
60  std::function<void(size_t)> reload_cache_callback = {}) {}
61 
62  DistributedSharedMutex(std::filesystem::path lockfilename, Callbacks callbacks) {}
63 
65 
66  virtual void lock() {}
67  virtual bool try_lock() { return true; }
68  virtual void unlock() {}
69  virtual void lock_shared() {}
70  virtual bool try_lock_shared() { return true; }
71  virtual void unlock_shared() {}
72  virtual void convert_lock() {}
73  virtual bool try_convert_lock() { return true; }
74  virtual void convert_lock_shared() {}
75  virtual bool try_convert_lock_shared() { return true; }
76 
77 }; // class DistributedSharedMutex
78 
79 } // namespace heavyai
bool g_multi_instance
Definition: heavyai_locks.h:21
virtual bool try_lock_shared()=0
DistributedSharedMutex(std::filesystem::path lockfilename, Callbacks callbacks)
Definition: heavyai_locks.h:62
std::function< void(bool)> pre_unlock_callback
Definition: heavyai_locks.h:55
virtual bool try_convert_lock_shared()
Definition: heavyai_locks.h:75
size_t g_lockfile_lock_extension_milliseconds
Definition: heavyai_locks.h:23
DistributedSharedMutex(std::filesystem::path lockfilename, std::function< void(size_t)> reload_cache_callback={})
Definition: heavyai_locks.h:59
std::function< void(bool)> pre_lock_callback
Definition: heavyai_locks.h:52
virtual void lock_shared()=0
std::function< void(bool)> post_unlock_callback
Definition: heavyai_locks.h:56
virtual void unlock_shared()=0
virtual void lock()=0
virtual bool try_lock()=0
virtual void unlock()=0
std::function< void(size_t)> reload_cache_callback
Definition: heavyai_locks.h:53
std::function< void(bool)> post_lock_callback
Definition: heavyai_locks.h:54