40 lines
1.1 KiB
C++
40 lines
1.1 KiB
C++
#pragma once
|
|
#ifndef MESSMER_CPPUTILS_LOCK_MUTEXPOOLLOCK_H
|
|
#define MESSMER_CPPUTILS_LOCK_MUTEXPOOLLOCK_H
|
|
|
|
#include "LockPool.h"
|
|
|
|
namespace cpputils {
|
|
template<class LockName>
|
|
class MutexPoolLock {
|
|
public:
|
|
MutexPoolLock(LockPool<LockName> *pool, const LockName &lockName): _pool(pool), _lockName(lockName) {
|
|
_pool->lock(_lockName);
|
|
}
|
|
|
|
MutexPoolLock(LockPool<LockName> *pool, const LockName &lockName, std::unique_lock<std::mutex> *lockToFreeWhileWaiting)
|
|
: _pool(pool), _lockName(lockName) {
|
|
_pool->lock(_lockName, lockToFreeWhileWaiting);
|
|
}
|
|
|
|
MutexPoolLock(MutexPoolLock &&rhs): _pool(rhs._pool), _lockName(rhs._lockName) {
|
|
rhs._pool = nullptr;
|
|
}
|
|
|
|
~MutexPoolLock() {
|
|
if (_pool != nullptr) {
|
|
_pool->release(_lockName);
|
|
_pool = nullptr;
|
|
}
|
|
}
|
|
|
|
private:
|
|
LockPool<LockName> *_pool;
|
|
LockName _lockName;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(MutexPoolLock);
|
|
};
|
|
}
|
|
|
|
#endif
|