2012-04-29 18:52:25 +00:00
|
|
|
//
|
|
|
|
// Mutex_WIN32.cpp
|
|
|
|
//
|
|
|
|
// $Id: //poco/1.4/Foundation/src/Mutex_WIN32.cpp#1 $
|
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: Threading
|
|
|
|
// Module: Mutex
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
2014-05-04 21:02:42 +02:00
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
2012-04-29 18:52:25 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include "Poco/Mutex_WIN32.h"
|
2014-10-19 10:59:08 +01:00
|
|
|
#include "Poco/Thread.h"
|
2012-04-29 18:52:25 +00:00
|
|
|
#include "Poco/Timestamp.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
|
|
|
2015-01-14 10:48:22 +00:00
|
|
|
MutexImpl::MutexImpl(MutexTypeImpl type)
|
2014-10-19 10:59:08 +01:00
|
|
|
: _lockCount(0)
|
2015-01-14 10:48:22 +00:00
|
|
|
, _recursive(type == MUTEX_RECURSIVE_IMPL)
|
2012-04-29 18:52:25 +00:00
|
|
|
{
|
|
|
|
// the fct has a boolean return value under WInnNt/2000/XP but not on Win98
|
|
|
|
// the return only checks if the input address of &_cs was valid, so it is safe to omit it
|
|
|
|
InitializeCriticalSectionAndSpinCount(&_cs, 4000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MutexImpl::~MutexImpl()
|
|
|
|
{
|
|
|
|
DeleteCriticalSection(&_cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-30 11:10:39 +00:00
|
|
|
void MutexImpl::lockImpl()
|
2014-10-19 10:59:08 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&_cs);
|
|
|
|
++_lockCount;
|
|
|
|
|
2014-10-30 11:10:39 +00:00
|
|
|
if (!_recursive && _lockCount > 1)
|
2014-10-19 10:59:08 +01:00
|
|
|
{
|
|
|
|
// We're trying to go recursive so self-deadlock
|
|
|
|
Thread::current()->join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
throw SystemException("cannot lock mutex");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-30 11:10:39 +00:00
|
|
|
bool MutexImpl::tryLockImpl()
|
2014-10-19 10:59:08 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (TryEnterCriticalSection(&_cs) == 0)
|
|
|
|
return false;
|
|
|
|
|
2014-10-30 11:10:39 +00:00
|
|
|
if (!_recursive && _lockCount > 0)
|
2014-10-19 10:59:08 +01:00
|
|
|
{
|
|
|
|
LeaveCriticalSection(&_cs);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
++_lockCount;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
throw SystemException("cannot lock mutex");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-29 18:52:25 +00:00
|
|
|
bool MutexImpl::tryLockImpl(long milliseconds)
|
2014-10-19 10:59:08 +01:00
|
|
|
{
|
|
|
|
const int sleepMillis = 5;
|
|
|
|
Timestamp now;
|
|
|
|
Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-10-19 18:11:43 +01:00
|
|
|
if (tryLockImpl())
|
|
|
|
return true;
|
2014-10-19 10:59:08 +01:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
throw SystemException("cannot lock mutex");
|
|
|
|
}
|
|
|
|
Sleep(sleepMillis);
|
|
|
|
} while (!now.isElapsed(diff));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-30 11:10:39 +00:00
|
|
|
FastMutexImpl::FastMutexImpl()
|
|
|
|
{
|
|
|
|
// the fct has a boolean return value under WInnNt/2000/XP but not on Win98
|
|
|
|
// the return only checks if the input address of &_cs was valid, so it is safe to omit it
|
|
|
|
InitializeCriticalSectionAndSpinCount(&_cs, 4000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FastMutexImpl::~FastMutexImpl()
|
|
|
|
{
|
|
|
|
DeleteCriticalSection(&_cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FastMutexImpl::tryLockImpl(long milliseconds)
|
2012-04-29 18:52:25 +00:00
|
|
|
{
|
|
|
|
const int sleepMillis = 5;
|
|
|
|
Timestamp now;
|
|
|
|
Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (TryEnterCriticalSection(&_cs) == TRUE)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
throw SystemException("cannot lock mutex");
|
|
|
|
}
|
|
|
|
Sleep(sleepMillis);
|
|
|
|
}
|
|
|
|
while (!now.isElapsed(diff));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|