resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmFileLockPool.h
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #pragma once
4
5 #include "cmConfigure.h" // IWYU pragma: keep
6
7 #include <string>
8 #include <vector>
9
10 #include "cmFileLock.h"
11
12 class cmFileLockResult;
13
14 class cmFileLockPool
15 {
16 public:
17   cmFileLockPool();
18   ~cmFileLockPool();
19
20   cmFileLockPool(cmFileLockPool const&) = delete;
21   cmFileLockPool& operator=(cmFileLockPool const&) = delete;
22
23   //@{
24   /**
25    * @brief Function scope control.
26    */
27   void PushFunctionScope();
28   void PopFunctionScope();
29   //@}
30
31   //@{
32   /**
33    * @brief File scope control.
34    */
35   void PushFileScope();
36   void PopFileScope();
37   //@}
38
39   //@{
40   /**
41    * @brief Lock the file in given scope.
42    * @param timeoutSec Lock timeout. If -1 try until success or fatal error.
43    */
44   cmFileLockResult LockFunctionScope(const std::string& filename,
45                                      unsigned long timeoutSec);
46   cmFileLockResult LockFileScope(const std::string& filename,
47                                  unsigned long timeoutSec);
48   cmFileLockResult LockProcessScope(const std::string& filename,
49                                     unsigned long timeoutSec);
50   //@}
51
52   /**
53    * @brief Unlock the file explicitly.
54    */
55   cmFileLockResult Release(const std::string& filename);
56
57 private:
58   bool IsAlreadyLocked(const std::string& filename) const;
59
60   class ScopePool
61   {
62   public:
63     ScopePool();
64     ~ScopePool();
65
66     ScopePool(ScopePool const&) = delete;
67     ScopePool(ScopePool&&) noexcept;
68     ScopePool& operator=(ScopePool const&) = delete;
69     ScopePool& operator=(ScopePool&&) noexcept;
70
71     cmFileLockResult Lock(const std::string& filename,
72                           unsigned long timeoutSec);
73     cmFileLockResult Release(const std::string& filename);
74     bool IsAlreadyLocked(const std::string& filename) const;
75
76   private:
77     using List = std::vector<cmFileLock>;
78
79     List Locks;
80   };
81
82   using List = std::vector<ScopePool>;
83
84   List FunctionScopes;
85   List FileScopes;
86   ScopePool ProcessScope;
87 };