Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / libraries / sdk_util / auto_lock.h
blobb969416ad78dee5ddf01f6a3730843cb974d2908
1 // Copyright (c) 2012 The Chromium 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.
5 #ifndef LIBRARIES_SDK_UTIL_AUTO_LOCK_H_
6 #define LIBRARIES_SDK_UTIL_AUTO_LOCK_H_
8 #include <pthread.h>
9 #include "sdk_util/macros.h"
10 #include "sdk_util/simple_lock.h"
12 namespace sdk_util {
14 // This macro is provided to allow us to quickly instrument locking for
15 // debugging purposes.
16 #define AUTO_LOCK(lock) \
17 ::sdk_util::AutoLock Lock##__LINE__(lock);
19 class AutoLock {
20 public:
21 AutoLock(const SimpleLock& lock) {
22 lock_ = lock.mutex();
23 pthread_mutex_lock(lock_);
26 ~AutoLock() {
27 Unlock();
30 void Unlock() {
31 if (lock_) pthread_mutex_unlock(lock_);
32 lock_ = NULL;
35 private:
36 pthread_mutex_t* lock_;
38 DISALLOW_COPY_AND_ASSIGN(AutoLock);
41 } // namespace sdk_util
43 #endif // LIBRARIES_SDK_UTIL_AUTO_LOCK_H_