OmniSciDB  f17484ade4
 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 namespace heavyai {
22 
23 // MutexInterface:
24 // An exclusive mutex compatible with std::unique_lock.
26  public:
27  virtual void lock() = 0;
28  virtual bool try_lock() = 0;
29  virtual void unlock() = 0;
30 
31  virtual ~MutexInterface() {}
32 };
33 
34 // SharedMutexInterface:
35 // A sharable mutex compatible with std::unique_lock and std::shared_lock.
37  public:
38  virtual void lock_shared() = 0;
39  virtual bool try_lock_shared() = 0;
40  virtual void unlock_shared() = 0;
41 
42  virtual ~SharedMutexInterface() {}
43 };
44 
46  public:
47  struct Callbacks {
48  std::function<void(bool /*write*/)> pre_lock_callback;
49  std::function<void(size_t /*version*/)> reload_cache_callback;
50  std::function<void(bool /*write*/)> post_lock_callback;
51  std::function<void(bool /*write*/)> pre_unlock_callback;
52  std::function<void(bool /*write*/)> post_unlock_callback;
53  };
54 
55  DistributedSharedMutex(std::filesystem::path lockfilename,
56  std::function<void(size_t)> reload_cache_callback = {}) {}
57 
58  DistributedSharedMutex(std::filesystem::path lockfilename, Callbacks callbacks) {}
59 
61 
62  virtual void lock() {}
63  virtual bool try_lock() { return true; }
64  virtual void unlock() {}
65  virtual void lock_shared() {}
66  virtual bool try_lock_shared() { return true; }
67  virtual void unlock_shared() {}
68  virtual void convert_lock() {}
69  virtual bool try_convert_lock() { return true; }
70  virtual void convert_lock_shared() {}
71  virtual bool try_convert_lock_shared() { return true; }
72 
73 }; // class DistributedSharedMutex
74 
75 } // namespace heavyai
virtual bool try_lock_shared()=0
DistributedSharedMutex(std::filesystem::path lockfilename, Callbacks callbacks)
Definition: heavyai_locks.h:58
std::function< void(bool)> pre_unlock_callback
Definition: heavyai_locks.h:51
virtual bool try_convert_lock_shared()
Definition: heavyai_locks.h:71
DistributedSharedMutex(std::filesystem::path lockfilename, std::function< void(size_t)> reload_cache_callback={})
Definition: heavyai_locks.h:55
std::function< void(bool)> pre_lock_callback
Definition: heavyai_locks.h:48
virtual void lock_shared()=0
std::function< void(bool)> post_unlock_callback
Definition: heavyai_locks.h:52
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:49
std::function< void(bool)> post_lock_callback
Definition: heavyai_locks.h:50