blob: 41d7fe45cb911fe425051a1a975cc5bf6085b375 [file] [log] [blame]
/*
* Copyright (C) 2007 Apple Inc. All rights reserved.
* Copyright (C) 2007 Brent Fulgham
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "Threading.h"
namespace WebCore {
Mutex::Mutex()
{
m_mutex.m_recursionCount = 0;
::InitializeCriticalSection(&m_mutex.m_internalMutex);
}
Mutex::~Mutex()
{
::DeleteCriticalSection(&m_mutex.m_internalMutex);
}
void Mutex::lock()
{
::EnterCriticalSection(&m_mutex.m_internalMutex);
++m_mutex.m_recursionCount;
}
bool Mutex::tryLock()
{
// This method is modeled after the behavior of pthread_mutex_trylock,
// which will return an error if the lock is already owned by the
// current thread. Since the primitive Win32 'TryEnterCriticalSection'
// treats this as a successful case, it changes the behavior of several
// tests in WebKit that check to see if the current thread already
// owned this mutex (see e.g., IconDatabase::getOrCreateIconRecord)
DWORD result = ::TryEnterCriticalSection(&m_mutex.m_internalMutex);
if (result != 0) { // We got the lock
// If this thread already had the lock, we must unlock and
// return false so that we mimic the behavior of POSIX's
// pthread_mutex_trylock:
if (m_mutex.m_recursionCount > 0) {
::LeaveCriticalSection(&m_mutex.m_internalMutex);
return false;
}
++m_mutex.m_recursionCount;
return true;
}
return false;
}
void Mutex::unlock()
{
--m_mutex.m_recursionCount;
::LeaveCriticalSection(&m_mutex.m_internalMutex);
}
} // namespace WebCore