Phosphor
chunk_lock.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright 2017 Couchbase, Inc
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
22 #pragma once
23 
24 #include <atomic>
25 
26 #include "platform/core.h"
27 
28 namespace phosphor {
29 
34 };
35 
36 constexpr non_trivial_constructor_t non_trivial_constructor {};
37 
38 class SlaveChunkLock;
39 class MasterChunkLock;
40 class TraceChunk;
41 
62 class PHOSPHOR_API ChunkLock {
63 public:
64 
71  ChunkLock() = default;
72 
77 
78  /*
79  * Explicit copy-constructor / copy-assignment
80  * as they are deleted by std::atomic
81  */
82  ChunkLock(const ChunkLock& other);
83  ChunkLock& operator=(const ChunkLock& other);
84 
93  void lockSlave();
94 
102  bool tryLockSlave();
103 
110  void unlockSlave();
111 
115  SlaveChunkLock& slave();
116 
120  void lockMaster();
121 
128  void unlockMaster();
129 
133  MasterChunkLock& master();
134 
135 protected:
136  enum class State {
137  // State is explicitly defined as 0 to allow for zero-initialization
138  Unlocked = 0x00,
139  SlaveLocked,
140  MasterLocked
141  };
142 
143  std::atomic<State> state;
144 
145  // Increase size to at least that of a cacheline
146  char cacheline_pad[64 - sizeof(std::atomic<State>)];
147 };
148 
152 class PHOSPHOR_API SlaveChunkLock : public ChunkLock {
153 public:
154  void lock();
155  bool try_lock();
156  void unlock();
157 };
158 
162 class PHOSPHOR_API MasterChunkLock : public ChunkLock {
163 public:
164  void lock();
165  void unlock();
166 };
167 
168 struct PHOSPHOR_API ChunkTenant {
175  ChunkTenant() = default;
176 
181 
182  void lock() {
183  lck.slave().lock();
184  }
185 
186  bool try_lock() {
187  return lck.slave().try_lock();
188  }
189 
190  void unlock() {
191  lck.slave().unlock();
192  }
193 
194  ChunkLock lck;
195  TraceChunk* chunk;
196 
203 };
204 
205 // @todo Remove requirement when moving to XCode 8 (10.11.5 / 10.12 or later)
206 #if defined(__APPLE__)
207 static_assert(std::is_trivially_constructible<ChunkTenant>::value,
208  "ChunkTenant must be trivially constructible as MacOS"
209  "only supports trivially constructible thread_locals");
210 #endif
211 }
Definition: chunk_lock.h:152
Definition: chunk_lock.h:33
Definition: chunk_lock.h:168
bool initialised
Definition: chunk_lock.h:202
Definition: chunk_lock.h:162
Definition: chunk_lock.h:62
Definition: trace_buffer.h:49
Definition: category_registry.h:32