Export provisioning sdk
Change-Id: I4d47d80444c9507f84896767dc676112ca11e901
This commit is contained in:
91
base/mutex.h
Normal file
91
base/mutex.h
Normal file
@@ -0,0 +1,91 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2016 Google Inc.
|
||||
//
|
||||
// This software is licensed under the terms defined in the Widevine Master
|
||||
// License Agreement. For a copy of this agreement, please contact
|
||||
// widevine-licensing@google.com.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef BASE_MUTEX_H_
|
||||
#define BASE_MUTEX_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "glog/logging.h"
|
||||
#include "base/macros.h"
|
||||
#include "base/thread_annotations.h"
|
||||
|
||||
// Basic mutex wrapper around a pthread RW lock.
|
||||
class LOCKABLE Mutex {
|
||||
public:
|
||||
inline Mutex() { CHECK_EQ(pthread_rwlock_init(&lock_, nullptr), 0); }
|
||||
inline Mutex(base::LinkerInitialized) { // NOLINT
|
||||
CHECK_EQ(pthread_rwlock_init(&lock_, nullptr), 0);
|
||||
}
|
||||
inline ~Mutex() { CHECK_EQ(pthread_rwlock_destroy(&lock_), 0); }
|
||||
inline void Lock() EXCLUSIVE_LOCK_FUNCTION() { WriterLock(); }
|
||||
inline void Unlock() UNLOCK_FUNCTION() { WriterUnlock(); }
|
||||
inline void ReaderLock() SHARED_LOCK_FUNCTION() {
|
||||
CHECK_EQ(pthread_rwlock_rdlock(&lock_), 0);
|
||||
}
|
||||
inline void ReaderUnlock() UNLOCK_FUNCTION() {
|
||||
CHECK_EQ(pthread_rwlock_unlock(&lock_), 0);
|
||||
}
|
||||
inline void WriterLock() EXCLUSIVE_LOCK_FUNCTION() {
|
||||
CHECK_EQ(pthread_rwlock_wrlock(&lock_), 0);
|
||||
}
|
||||
inline void WriterUnlock() UNLOCK_FUNCTION() {
|
||||
CHECK_EQ(pthread_rwlock_unlock(&lock_), 0);
|
||||
}
|
||||
|
||||
private:
|
||||
pthread_rwlock_t lock_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Mutex);
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// MutexLock(mu) acquires mu when constructed and releases it when destroyed.
|
||||
class SCOPED_LOCKABLE MutexLock {
|
||||
public:
|
||||
explicit MutexLock(Mutex* mu) EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) {
|
||||
this->mu_->Lock();
|
||||
}
|
||||
~MutexLock() UNLOCK_FUNCTION() { this->mu_->Unlock(); }
|
||||
|
||||
private:
|
||||
Mutex* const mu_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(MutexLock);
|
||||
};
|
||||
|
||||
// The ReaderMutexLock and WriterMutexLock classes work like MutexLock
|
||||
// to acquire/release read and write locks on reader/writer locks.
|
||||
class SCOPED_LOCKABLE ReaderMutexLock {
|
||||
public:
|
||||
explicit ReaderMutexLock(Mutex* mu) SHARED_LOCK_FUNCTION(mu) : mu_(mu) {
|
||||
mu->ReaderLock();
|
||||
}
|
||||
~ReaderMutexLock() UNLOCK_FUNCTION() { this->mu_->ReaderUnlock(); }
|
||||
|
||||
private:
|
||||
Mutex* const mu_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock);
|
||||
};
|
||||
|
||||
class SCOPED_LOCKABLE WriterMutexLock {
|
||||
public:
|
||||
explicit WriterMutexLock(Mutex* mu) EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) {
|
||||
mu->WriterLock();
|
||||
}
|
||||
~WriterMutexLock() UNLOCK_FUNCTION() { this->mu_->WriterUnlock(); }
|
||||
|
||||
private:
|
||||
Mutex* const mu_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(WriterMutexLock);
|
||||
};
|
||||
|
||||
#endif // BASE_MUTEX_H_
|
||||
Reference in New Issue
Block a user