Storage Engine API
mongo::LockManager Class Reference

Entry point for the lock manager scheduling functionality. More...

#include <lock_manager.h>

Detailed Description

Entry point for the lock manager scheduling functionality.

Don't use it directly, but instead go through the Locker interface.

Classes

struct  LockBucket
 
struct  Partition
 

Public Member Functions

 LockManager ()
 
 ~LockManager ()
 
LockResult lock (ResourceId resId, LockRequest *request, LockMode mode)
 Acquires lock on the specified resource in the specified mode and returns the outcome of the operation. More...
 
LockResult convert (ResourceId resId, LockRequest *request, LockMode newMode)
 
bool unlock (LockRequest *request)
 Decrements the reference count of a previously locked request and if the reference count becomes zero, removes the request and proceeds to granting any conflicts. More...
 
void downgrade (LockRequest *request, LockMode newMode)
 Downgrades the mode in which an already granted request is held, without changing the reference count of the lock request. More...
 
void cleanupUnusedLocks ()
 Iterates through all buckets and deletes all locks, which have no requests on them. More...
 
void dump () const
 Dumps the contents of all locks to the log. More...
 
void getLockInfoBSON (const std::map< LockerId, BSONObj > &lockToClientMap, BSONObjBuilder *result)
 Dumps the contents of all locks into a BSON object to be used in lockInfo command in the shell. More...
 

Private Member Functions

 MONGO_DISALLOW_COPYING (LockManager)
 
LockBucket_getBucket (ResourceId resId) const
 Retrieves the bucket in which the particular resource must reside. More...
 
Partition_getPartition (LockRequest *request) const
 Retrieves the Partition that a particular LockRequest should use for intent locking. More...
 
void _dumpBucket (const LockBucket *bucket) const
 Prints the contents of a bucket to the log. More...
 
void _dumpBucketToBSON (const std::map< LockerId, BSONObj > &lockToClientMap, const LockBucket *bucket, BSONObjBuilder *result)
 Dump the contents of a bucket to the BSON. More...
 
void _buildBucketBSON (const LockRequest *iter, const std::map< LockerId, BSONObj > &lockToClientMap, const LockBucket *bucket, BSONArrayBuilder *locks)
 Build the BSON object containing the lock info for a particular bucket. More...
 
void _onLockModeChanged (LockHead *lock, bool checkConflictQueue)
 Should be invoked when the state of a lock changes in a way, which could potentially allow other blocked requests to proceed. More...
 
void _cleanupUnusedLocksInBucket (LockBucket *bucket)
 Helper function to delete all locks that have no request on them on a single bucket. More...
 

Private Attributes

LockBucket_lockBuckets
 
Partition_partitions
 

Static Private Attributes

static const unsigned _numLockBuckets
 
static const unsigned _numPartitions = 32
 

Friends

class DeadlockDetector
 
struct LockHead
 

Constructor & Destructor Documentation

◆ LockManager()

mongo::LockManager::LockManager ( )

◆ ~LockManager()

mongo::LockManager::~LockManager ( )

Member Function Documentation

◆ _buildBucketBSON()

void mongo::LockManager::_buildBucketBSON ( const LockRequest iter,
const std::map< LockerId, BSONObj > &  lockToClientMap,
const LockBucket bucket,
BSONArrayBuilder *  locks 
)
private

Build the BSON object containing the lock info for a particular bucket.

The lockToClientMap is used to map the lockerId to more useful client information.

◆ _cleanupUnusedLocksInBucket()

void mongo::LockManager::_cleanupUnusedLocksInBucket ( LockBucket bucket)
private

Helper function to delete all locks that have no request on them on a single bucket.

Called by cleanupUnusedLocks()

◆ _dumpBucket()

void mongo::LockManager::_dumpBucket ( const LockBucket bucket) const
private

Prints the contents of a bucket to the log.

◆ _dumpBucketToBSON()

void mongo::LockManager::_dumpBucketToBSON ( const std::map< LockerId, BSONObj > &  lockToClientMap,
const LockBucket bucket,
BSONObjBuilder *  result 
)
private

Dump the contents of a bucket to the BSON.

◆ _getBucket()

LockManager::LockBucket * mongo::LockManager::_getBucket ( ResourceId  resId) const
private

Retrieves the bucket in which the particular resource must reside.

There is no need to hold a lock when calling this function.

◆ _getPartition()

LockManager::Partition * mongo::LockManager::_getPartition ( LockRequest request) const
private

Retrieves the Partition that a particular LockRequest should use for intent locking.

◆ _onLockModeChanged()

void mongo::LockManager::_onLockModeChanged ( LockHead lock,
bool  checkConflictQueue 
)
private

Should be invoked when the state of a lock changes in a way, which could potentially allow other blocked requests to proceed.

MUST be called under the lock bucket's mutex.

Parameters
lockLock whose grant state should be recalculated.
checkConflictQueueWhether to go through the conflict queue. This is an optimisation in that we only need to check the conflict queue if one of the granted modes, which was conflicting before became zero.

◆ cleanupUnusedLocks()

void mongo::LockManager::cleanupUnusedLocks ( )

Iterates through all buckets and deletes all locks, which have no requests on them.

This call is kind of expensive and should only be used for reducing the memory footprint of the lock manager.

◆ convert()

LockResult mongo::LockManager::convert ( ResourceId  resId,
LockRequest request,
LockMode  newMode 
)

◆ downgrade()

void mongo::LockManager::downgrade ( LockRequest request,
LockMode  newMode 
)

Downgrades the mode in which an already granted request is held, without changing the reference count of the lock request.

This call never blocks, will always succeed and may potentially allow other blocked lock requests to proceed.

Parameters
requestRequest, already in granted mode through a previous call to lock.
newModeMode, which is less-restrictive than the mode in which the request is already held. I.e., the conflict set of newMode must be a sub-set of the conflict set of the request's current mode.

◆ dump()

void mongo::LockManager::dump ( ) const

Dumps the contents of all locks to the log.

◆ getLockInfoBSON()

void mongo::LockManager::getLockInfoBSON ( const std::map< LockerId, BSONObj > &  lockToClientMap,
BSONObjBuilder *  result 
)

Dumps the contents of all locks into a BSON object to be used in lockInfo command in the shell.

◆ lock()

LockResult mongo::LockManager::lock ( ResourceId  resId,
LockRequest request,
LockMode  mode 
)

Acquires lock on the specified resource in the specified mode and returns the outcome of the operation.

See the details for LockResult for more information on what the different results mean.

Locking the same resource twice increments the reference count of the lock so each call to lock must be matched with a call to unlock with the same resource.

Parameters
resIdId of the resource to be locked.
requestLockRequest structure on which the state of the request will be tracked. This value cannot be NULL and the notify value must be set. If the return value is not LOCK_WAITING, this pointer can be freed and will not be used any more.

If the return value is LOCK_WAITING, the notification method will be called at some point into the future, when the lock either becomes granted or a deadlock is discovered. If unlock is called before the lock becomes granted, the notification will not be invoked.

If the return value is LOCK_WAITING, the notification object must live at least until the notify method has been invoked or unlock has been called for the resource it was assigned to. Failure to do so will cause the lock manager to call into an invalid memory location.

Parameters
modeMode in which the resource should be locked. Lock upgrades are allowed.
Returns
See comments for LockResult.

◆ MONGO_DISALLOW_COPYING()

mongo::LockManager::MONGO_DISALLOW_COPYING ( LockManager  )
private

◆ unlock()

bool mongo::LockManager::unlock ( LockRequest request)

Decrements the reference count of a previously locked request and if the reference count becomes zero, removes the request and proceeds to granting any conflicts.

This method always succeeds and never blocks.

Parameters
requestA previously locked request. Calling unlock more times than lock was called for the same LockRequest is an error.
Returns
true if this is the last reference for the request; false otherwise

Friends And Related Function Documentation

◆ DeadlockDetector

friend class DeadlockDetector
friend

◆ LockHead

friend struct LockHead
friend

Member Data Documentation

◆ _lockBuckets

LockBucket* mongo::LockManager::_lockBuckets
private

◆ _numLockBuckets

const unsigned mongo::LockManager::_numLockBuckets
staticprivate

◆ _numPartitions

const unsigned mongo::LockManager::_numPartitions = 32
staticprivate

◆ _partitions

Partition* mongo::LockManager::_partitions
private

The documentation for this class was generated from the following files: