CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fence.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_FENCE_H_
6 #define CVMFS_FENCE_H_
7 
8 #include "atomic.h"
9 #include "gtest/gtest_prod.h"
10 #include "util/posix.h"
11 #include "util/single_copy.h"
12 
13 #ifdef CVMFS_NAMESPACE_GUARD
14 namespace CVMFS_NAMESPACE_GUARD {
15 #endif
16 
25 class Fence : public SingleCopy {
26  FRIEND_TEST(T_Fence, Basics);
27 
28  public:
29  Fence() {
30  atomic_init64(&counter_);
31  atomic_init32(&blocking_);
32  }
33 
34  void Enter() {
35  while (atomic_read32(&blocking_)) {
36  SafeSleepMs(kBusyWaitBackoffMs);
37  }
38  atomic_inc64(&counter_);
39  }
40 
41  void Leave() {
42  atomic_dec64(&counter_);
43  }
44 
45  void Close() {
46  atomic_cas32(&blocking_, 0, 1);
47  }
48 
52  void Drain() {
53  Close();
54  while (atomic_read64(&counter_) > 0) {
55  SafeSleepMs(kBusyWaitBackoffMs);
56  }
57  }
58 
59  void Open() {
60  atomic_cas32(&blocking_, 1, 0);
61  }
62 
63  private:
64  static const unsigned kBusyWaitBackoffMs = 100;
65 
70 
75 };
76 
77 
82 class FenceGuard {
83  public:
84  explicit FenceGuard(Fence *fence) : fence_(fence) {
85  fence_->Enter();
86  }
88  fence_->Leave();
89  }
90  private:
92 };
93 
94 #ifdef CVMFS_NAMESPACE_GUARD
95 } // namespace CVMFS_NAMESPACE_GUARD
96 #endif
97 
98 #endif // CVMFS_FENCE_H_
int64_t atomic_int64
Definition: atomic.h:18
void Enter()
Definition: fence.h:34
atomic_int64 counter_
Definition: fence.h:69
~FenceGuard()
Definition: fence.h:87
int32_t atomic_int32
Definition: atomic.h:17
atomic_int32 blocking_
Definition: fence.h:74
void Drain()
Definition: fence.h:52
Fence()
Definition: fence.h:29
void Leave()
Definition: fence.h:41
FenceGuard(Fence *fence)
Definition: fence.h:84
Definition: fence.h:25
void SafeSleepMs(const unsigned ms)
Definition: posix.cc:1908
void Close()
Definition: fence.h:45
Fence * fence_
Definition: fence.h:91
void Open()
Definition: fence.h:59