#ifndef LIBRARIES_SDK_UTIL_AUTO_LOCK_H_
#define LIBRARIES_SDK_UTIL_AUTO_LOCK_H_
#include <pthread.h>
#include "sdk_util/macros.h"
#include "sdk_util/simple_lock.h"
namespace sdk_util {
#define AUTO_LOCK(lock) \
::sdk_util::AutoLock Lock##__LINE__(lock);
class AutoLock {
public:
AutoLock(const SimpleLock& lock) {
lock_ = lock.mutex();
pthread_mutex_lock(lock_);
}
~AutoLock() {
Unlock();
}
void Unlock() {
if (lock_) pthread_mutex_unlock(lock_);
lock_ = NULL;
}
private:
pthread_mutex_t* lock_;
DISALLOW_COPY_AND_ASSIGN(AutoLock);
};
}
#endif