chrome中的加锁基类,去掉不用的部分
#pragma once
#define DISALLOW_COPY_AND_ASSIGN(TypeName) /
TypeName(const TypeName&); /
void operator=(const TypeName&)
class LockImpl
{
typedef CRITICAL_SECTION OSLockType;
public:
LockImpl(void);
virtual ~LockImpl(void);
// If the lock is not held, take it and return true. If the lock is already
// held by something else, immediately return false.
bool Try();
// Take the lock, blocking until it is available if necessary.
void Lock();
// Release the lock. This must only be called by the lock's holder: after
// a successful call to Try, or a call to Lock.
void Unlock();
// Debug-only method that will DCHECK() if the lock is not acquired by the
// current thread. In non-debug builds, no check is performed.
// Because linux and mac condition variables modify the underlyning lock
// through the os_lock() method, runtime assertions can not be done on those
// builds.
#ifdef DEBUG
void AssertAcquired() const;
#endif
private:
OSLockType os_lock_;
DISALLOW_COPY_AND_ASSIGN(LockImpl);
};
#include "stdafx.h"
#include "LockImpl.h"
LockImpl::LockImpl(void)
{
::InitializeCriticalSectionAndSpinCount(&os_lock_, 2000);
}
LockImpl::~LockImpl(void)
{
::DeleteCriticalSection(&os_lock_);
}
bool LockImpl::Try() {
if (::TryEnterCriticalSection(&os_lock_) != FALSE) {
return true;
}
return false;
}
void LockImpl::Lock() {
::EnterCriticalSection(&os_lock_);
}
void LockImpl::Unlock() {
::LeaveCriticalSection(&os_lock_);
}
LockImpl是一个用于Chrome的加锁基类,它定义了尝试获取锁、锁定、解锁以及在调试模式下检查锁是否已被当前线程持有的方法。该类基于CRITICAL_SECTION实现,并使用InitializeCriticalSectionAndSpinCount进行初始化,DeleteCriticalSection进行销毁。
2277

被折叠的 条评论
为什么被折叠?



