小组成员:谢瑞阳、徐翔宇
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB

  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.
  4. #ifndef STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_
  5. #define STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_
  6. #include "port/port.h"
  7. #include "port/thread_annotations.h"
  8. namespace leveldb {
  9. // Helper class that locks a mutex on construction and unlocks the mutex when
  10. // the destructor of the MutexLock object is invoked.
  11. //
  12. // Typical usage:
  13. //
  14. // void MyClass::MyMethod() {
  15. // MutexLock l(&mu_); // mu_ is an instance variable
  16. // ... some complex code, possibly with multiple return paths ...
  17. // }
  18. class SCOPED_LOCKABLE MutexLock {
  19. public:
  20. explicit MutexLock(port::Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu)
  21. : mu_(mu) {
  22. this->mu_->Lock();
  23. }
  24. ~MutexLock() UNLOCK_FUNCTION() { this->mu_->Unlock(); }
  25. private:
  26. port::Mutex *const mu_;
  27. // No copying allowed
  28. MutexLock(const MutexLock&);
  29. void operator=(const MutexLock&);
  30. };
  31. } // namespace leveldb
  32. #endif // STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_