1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
5 #ifndef STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_
6 #define STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_
9 #include "port/thread_annotations.h"
13 // Helper class that locks a mutex on construction and unlocks the mutex when
14 // the destructor of the MutexLock object is invoked.
18 // void MyClass::MyMethod() {
19 // MutexLock l(&mu_); // mu_ is an instance variable
20 // ... some complex code, possibly with multiple return paths ...
23 class SCOPED_LOCKABLE MutexLock {
25 explicit MutexLock(port::Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu)
29 ~MutexLock() UNLOCK_FUNCTION() { this->mu_->Unlock(); }
32 port::Mutex *const mu_;
34 MutexLock(const MutexLock&);
35 void operator=(const MutexLock&);
38 } // namespace leveldb
41 #endif // STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_