Storage Engine API
d_concurrency.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <climits> // For UINT_MAX
32 
34 #include "mongo/db/operation_context.h"
35 #include "mongo/util/timer.h"
36 
37 namespace mongo {
38 
39 class StringData;
40 class NamespaceString;
41 
42 class Lock {
43 public:
47  class TempRelease {
49 
50  public:
51  explicit TempRelease(Locker* lockState);
52  ~TempRelease();
53 
54  private:
55  // Not owned
57 
58  // If _locksReleased is true, this stores the persisted lock information to be restored
59  // in the destructor. Otherwise it is empty.
61 
62  // False if locks could not be released because of recursive locking
63  const bool _locksReleased;
64  };
65 
73  class ResourceLock {
75 
76  public:
78  : _rid(rid), _locker(locker), _result(LOCK_INVALID) {}
79 
81  : _rid(rid), _locker(locker), _result(LOCK_INVALID) {
82  lock(mode);
83  }
84 
86  : _rid(otherLock._rid), _locker(otherLock._locker), _result(otherLock._result) {
87  // Mark as moved so the destructor doesn't invalidate the newly-
88  // constructed lock.
89  otherLock._result = LOCK_INVALID;
90  }
91 
93  if (isLocked()) {
94  unlock();
95  }
96  }
97 
98  void lock(LockMode mode);
99  void unlock();
100 
101  bool isLocked() const {
102  return _result == LOCK_OK;
103  }
104 
105  private:
107  Locker* const _locker;
108 
110  };
111 
112  class SharedLock;
113  class ExclusiveLock;
114 
122  public:
123  ResourceMutex(std::string resourceLabel);
124 
125  std::string getName() const {
126  return getName(_rid);
127  };
128 
129  static std::string getName(ResourceId resourceId);
130 
131  bool isExclusivelyLocked(Locker* locker);
132 
133  bool isAtLeastReadLocked(Locker* locker);
134 
135  private:
136  friend class Lock::SharedLock;
137  friend class Lock::ExclusiveLock;
138 
142  ResourceId rid() const {
143  return _rid;
144  }
145 
147  };
148 
152  class ExclusiveLock : public ResourceLock {
153  public:
155  : ResourceLock(locker, mutex.rid(), MODE_X) {}
156  };
157 
163  class SharedLock : public ResourceLock {
164  public:
166  : ResourceLock(locker, mutex.rid(), MODE_IS) {}
167  };
168 
172  enum class InterruptBehavior {
173  kThrow, // Throw the interruption exception.
174  kLeaveUnlocked // Suppress the exception, but leave unlocked such that a call to isLocked()
175  // returns false.
176  };
177 
188  class GlobalLock {
189  public:
190  class EnqueueOnly {};
191 
196  GlobalLock(OperationContext* opCtx, LockMode lockMode)
197  : GlobalLock(opCtx, lockMode, Date_t::max(), InterruptBehavior::kThrow) {}
198 
202  GlobalLock(OperationContext* opCtx,
203  LockMode lockMode,
204  Date_t deadline,
205  InterruptBehavior behavior);
206 
208 
216  GlobalLock(OperationContext* opCtx,
217  LockMode lockMode,
218  Date_t deadline,
219  InterruptBehavior behavior,
220  EnqueueOnly enqueueOnly);
221 
223  if (isLocked()) {
224  // Abandon our snapshot if destruction of the GlobalLock object results in actually
225  // unlocking the global lock. Recursive locking and the two-phase locking protocol
226  // may prevent lock release.
227  const bool willReleaseLock = _isOutermostLock &&
228  !(_opCtx->lockState() && _opCtx->lockState()->inAWriteUnitOfWork());
229  if (willReleaseLock) {
230  _opCtx->recoveryUnit()->abandonSnapshot();
231  }
232  _unlock();
233  }
234  }
235 
240  void waitForLockUntil(Date_t deadline);
241 
242  bool isLocked() const {
243  return _result == LOCK_OK;
244  }
245 
246  private:
247  void _enqueue(LockMode lockMode, Date_t deadline);
248  void _unlock();
249 
250  OperationContext* const _opCtx;
254  const bool _isOutermostLock;
255  };
256 
264  class GlobalWrite : public GlobalLock {
265  public:
266  explicit GlobalWrite(OperationContext* opCtx)
267  : GlobalWrite(opCtx, Date_t::max(), InterruptBehavior::kThrow) {}
268  explicit GlobalWrite(OperationContext* opCtx, Date_t deadline, InterruptBehavior behavior)
269  : GlobalLock(opCtx, MODE_X, deadline, behavior) {
270  if (isLocked()) {
271  opCtx->lockState()->lockMMAPV1Flush();
272  }
273  }
274  };
275 
283  class GlobalRead : public GlobalLock {
284  public:
285  explicit GlobalRead(OperationContext* opCtx)
286  : GlobalRead(opCtx, Date_t::max(), InterruptBehavior::kThrow) {}
287  explicit GlobalRead(OperationContext* opCtx, Date_t deadline, InterruptBehavior behavior)
288  : GlobalLock(opCtx, MODE_S, deadline, behavior) {
289  if (isLocked()) {
290  opCtx->lockState()->lockMMAPV1Flush();
291  }
292  }
293  };
294 
309  class DBLock {
310  public:
311  DBLock(OperationContext* opCtx,
312  StringData db,
313  LockMode mode,
314  Date_t deadline = Date_t::max());
315  DBLock(DBLock&&);
316  ~DBLock();
317 
324  void relockWithMode(LockMode newMode);
325 
326  bool isLocked() const {
327  return _result == LOCK_OK;
328  }
329 
330  private:
332  OperationContext* const _opCtx;
334 
335  // May be changed through relockWithMode. The global lock mode won't change though,
336  // because we never change from IS/S to IX/X or vice versa, just convert locks from
337  // IX -> X.
339 
340  // Acquires the global lock on our behalf.
342  };
343 
360 
361  public:
362  CollectionLock(Locker* lockState,
363  StringData ns,
364  LockMode mode,
365  Date_t deadline = Date_t::max());
367  ~CollectionLock();
368 
369  bool isLocked() const {
370  return _result == LOCK_OK;
371  }
372 
373  private:
377  };
378 
387 
388  public:
389  explicit OplogIntentWriteLock(Locker* lockState);
391  void serializeIfNeeded();
392 
393  private:
396  };
397 
406 
407  public:
408  explicit ParallelBatchWriterMode(Locker* lockState);
409 
410  private:
413  };
414 };
415 
416 } // namespace mongo
Locker *const _lockState
Definition: d_concurrency.h:394
Locker *const _lockState
Definition: d_concurrency.h:56
ResourceLock _pbwm
Definition: d_concurrency.h:411
ResourceLock(Locker *locker, ResourceId rid, LockMode mode)
Definition: d_concurrency.h:80
Global lock.
Definition: d_concurrency.h:188
Locker * _lockState
Definition: d_concurrency.h:376
Interface for acquiring locks.
Definition: locker.h:47
OperationContext *const _opCtx
Definition: d_concurrency.h:332
LockResult _result
Definition: d_concurrency.h:375
Collection *const const NamespaceString & ns
Definition: collection_info_cache_impl.cpp:53
const ResourceId _id
Definition: d_concurrency.h:331
Database lock with support for collection- and document-level locking.
Definition: d_concurrency.h:309
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
LockResult _result
Definition: d_concurrency.h:251
const ResourceId _id
Definition: d_concurrency.h:374
Definition: lock_manager_defs.h:61
Like the CollectionLock, but optimized for the local oplog.
Definition: d_concurrency.h:385
GlobalLock(OperationContext *opCtx, LockMode lockMode)
A GlobalLock without a deadline defaults to Date_t::max() and an InterruptBehavior of kThrow...
Definition: d_concurrency.h:196
Definition: d_concurrency.h:42
General purpose RAII wrapper for a resource managed by the lock manager.
Definition: d_concurrency.h:73
bool isLocked() const
Definition: d_concurrency.h:101
RAII-style class to opt out of replication&#39;s use of ParallelBatchWriterMode.
Definition: locker.h:501
ExclusiveLock(Locker *locker, ResourceMutex mutex)
Definition: d_concurrency.h:154
ResourceId rid() const
Each instantiation of this class allocates a new ResourceId.
Definition: d_concurrency.h:142
ShouldNotConflictWithSecondaryBatchApplicationBlock _shouldNotConflictBlock
Definition: d_concurrency.h:412
ResourceLock(Locker *locker, ResourceId rid)
Definition: d_concurrency.h:77
This is used as an initializer value.
Definition: lock_manager_defs.h:133
NOTE: DO NOT add any new usages of TempRelease.
Definition: d_concurrency.h:47
GlobalWrite(OperationContext *opCtx, Date_t deadline, InterruptBehavior behavior)
Definition: d_concurrency.h:268
Global shared lock.
Definition: d_concurrency.h:283
Locker::LockSnapshot _lockSnapshot
Definition: d_concurrency.h:60
InterruptBehavior _interruptBehavior
Definition: d_concurrency.h:253
Uniquely identifies a lockable resource.
Definition: lock_manager_defs.h:176
GlobalLock _globalLock
Definition: d_concurrency.h:341
~ResourceLock()
Definition: d_concurrency.h:92
GlobalWrite(OperationContext *opCtx)
Definition: d_concurrency.h:266
GlobalRead(OperationContext *opCtx, Date_t deadline, InterruptBehavior behavior)
Definition: d_concurrency.h:287
bool isLocked() const
Definition: d_concurrency.h:326
Definition: lock_manager_defs.h:63
LockResult
Return values for the locking functions of the lock manager.
Definition: lock_manager_defs.h:99
const ResourceId _rid
Definition: d_concurrency.h:146
LockMode
Lock modes.
Definition: lock_manager_defs.h:59
LockResult _result
Definition: d_concurrency.h:333
TempRelease(Locker *lockState)
Definition: d_concurrency.cpp:51
std::array< DefaultLockerImpl, kMaxPerfThreads > locker
Definition: d_concurrency_bm.cpp:66
Global exclusive lock.
Definition: d_concurrency.h:264
Obtains a ResourceMutex for shared/non-exclusive use.
Definition: d_concurrency.h:163
Collection lock with support for document-level locking.
Definition: d_concurrency.h:358
MONGO_DISALLOW_COPYING(TempRelease)
~TempRelease()
Definition: d_concurrency.cpp:56
SharedLock(Locker *locker, ResourceMutex mutex)
Definition: d_concurrency.h:165
~GlobalLock()
Definition: d_concurrency.h:222
The lock request was granted and is now on the granted list for the specified resource.
Definition: lock_manager_defs.h:104
const bool _locksReleased
Definition: d_concurrency.h:63
OperationContext Database * db
Definition: database_impl.cpp:949
OperationContext *const _opCtx
Definition: d_concurrency.h:250
bool _serialized
Definition: d_concurrency.h:395
For use as general mutex or readers/writers lock, outside the general multi-granularity model...
Definition: d_concurrency.h:121
Locker *const _locker
Definition: d_concurrency.h:107
LockMode _mode
Definition: d_concurrency.h:338
LockSnapshot captures the state of all resources that are locked, what modes they&#39;re locked in...
Definition: locker.h:342
bool isLocked() const
Definition: d_concurrency.h:369
GlobalRead(OperationContext *opCtx)
Definition: d_concurrency.h:285
ResourceLock _pbwm
Definition: d_concurrency.h:252
bool isLocked() const
Definition: d_concurrency.h:242
OperationContext * _opCtx
Definition: ephemeral_for_test_btree_impl.cpp:447
Definition: d_concurrency.h:190
Turn on "parallel batch writer mode" by locking the global ParallelBatchWriterMode resource in exclus...
Definition: d_concurrency.h:404
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
const ResourceId _rid
Definition: d_concurrency.h:106
std::string getName() const
Definition: d_concurrency.h:125
Definition: lock_manager_defs.h:64
LockResult _result
Definition: d_concurrency.h:109
const bool _isOutermostLock
Definition: d_concurrency.h:254
InterruptBehavior
The interrupt behavior is used to tell a lock how to handle an interrupted lock acquisition.
Definition: d_concurrency.h:172
Obtains a ResourceMutex for exclusive use.
Definition: d_concurrency.h:152
ResourceLock(ResourceLock &&otherLock)
Definition: d_concurrency.h:85