winss
path_mutex.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2017 Morgan Stanley
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 #ifndef LIB_WINSS_PATH_MUTEX_HPP_
18 #define LIB_WINSS_PATH_MUTEX_HPP_
19 
20 #include <windows.h>
21 #include <filesystem>
22 #include <string>
23 
24 namespace fs = std::experimental::filesystem;
25 
26 namespace winss {
30 class PathMutex {
31  protected:
32  std::string mutex_name;
33  HANDLE lock = nullptr;
35  PathMutex() {}
37  public:
44  PathMutex(fs::path path, std::string name);
45 
46  PathMutex(const PathMutex&) = delete;
47  PathMutex(PathMutex&&) = delete;
54  virtual bool Lock();
55 
61  virtual bool CanLock() const;
62 
68  virtual bool HasLock() const;
69 
75  virtual const std::string& GetName() const;
76 
77  PathMutex& operator=(const PathMutex&) = delete;
78  PathMutex& operator=(PathMutex&&) = delete;
80  virtual ~PathMutex();
81 };
82 } // namespace winss
83 
84 #endif // LIB_WINSS_PATH_MUTEX_HPP_
PathMutex()
Hide the default constructor.
Definition: path_mutex.hpp:35
A global mutex where the key is derived from a file path and service name.
Definition: path_mutex.hpp:30
virtual bool CanLock() const
Checks if the lock will fail or not.
Definition: path_mutex.cpp:62
virtual bool HasLock() const
Checks if this instance owns the lock.
Definition: path_mutex.cpp:82
Definition: case_ignore.hpp:23
virtual bool Lock()
Attempts to get the mutex.
Definition: path_mutex.cpp:37
HANDLE lock
A handle to the Windows mutex.
Definition: path_mutex.hpp:33
virtual ~PathMutex()
Default destructor.
Definition: path_mutex.cpp:90
PathMutex & operator=(const PathMutex &)=delete
No copy.
virtual const std::string & GetName() const
Gets the name of the service.
Definition: path_mutex.cpp:86
std::string mutex_name
The service name.
Definition: path_mutex.hpp:32