nim_duilib/base/synchronization/lock_win.cpp
jiajia_deng 4933d1f2bc Remove dependency on shared
Signed-off-by: jiajia_deng <2894220@gmail.com>
2019-09-20 16:27:58 +08:00

49 lines
737 B
C++

// Copyright (c) 2011, NetEase Inc. All rights reserved.
//
// Author: rl
// Date: 2011/6/9
//
// Lock implemetation on windows
#include "lock.h"
#if defined(OS_WIN)
namespace nbase
{
NLock::NLock()
{
// The second parameter is the spin count, for short-held locks it avoid the
// contending thread from going to sleep which helps performance greatly.
::InitializeCriticalSectionAndSpinCount(&os_lock_, 2000);
}
NLock::~NLock()
{
::DeleteCriticalSection(&os_lock_);
}
bool NLock::Try()
{
if (::TryEnterCriticalSection(&os_lock_))
{
return true;
}
return false;
}
void NLock::Lock()
{
::EnterCriticalSection(&os_lock_);
}
void NLock::Unlock()
{
::LeaveCriticalSection(&os_lock_);
}
} // namespace
#endif // OS_WIN