2011-03-25 05:06:49 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
|
|
|
*
|
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-09-01 20:31:46 +00:00
|
|
|
#include "common/RwMutex.h"
|
2011-03-25 05:06:49 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// RwMutex
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
Threading::RwMutex::RwMutex()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
pthread_rwlock_init(&m_rwlock, NULL);
|
2011-03-25 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 12:22:00 +00:00
|
|
|
Threading::RwMutex::~RwMutex()
|
2011-03-25 05:06:49 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
pthread_rwlock_destroy(&m_rwlock);
|
2011-03-25 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Threading::RwMutex::AcquireRead()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
pthread_rwlock_rdlock(&m_rwlock);
|
2011-03-25 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Threading::RwMutex::AcquireWrite()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
pthread_rwlock_wrlock(&m_rwlock);
|
2011-03-25 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Threading::RwMutex::TryAcquireRead()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
return pthread_rwlock_tryrdlock(&m_rwlock) != EBUSY;
|
2011-03-25 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Threading::RwMutex::TryAcquireWrite()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
return pthread_rwlock_trywrlock(&m_rwlock) != EBUSY;
|
2011-03-25 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Threading::RwMutex::Release()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
pthread_rwlock_unlock(&m_rwlock);
|
2011-03-25 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
2016-11-12 15:28:37 +00:00
|
|
|
//
|
2011-03-25 05:06:49 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
2017-05-06 12:22:00 +00:00
|
|
|
Threading::BaseScopedReadWriteLock::~BaseScopedReadWriteLock()
|
2011-03-25 05:06:49 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (m_IsLocked)
|
|
|
|
m_lock.Release();
|
2011-03-25 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Provides manual unlocking of a scoped lock prior to object destruction.
|
|
|
|
void Threading::BaseScopedReadWriteLock::Release()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (!m_IsLocked)
|
|
|
|
return;
|
|
|
|
m_IsLocked = false;
|
|
|
|
m_lock.Release();
|
2011-03-25 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// ScopedReadLock / ScopedWriteLock
|
|
|
|
// --------------------------------------------------------------------------------------
|
2021-09-06 18:28:26 +00:00
|
|
|
Threading::ScopedReadLock::ScopedReadLock(RwMutex& locker)
|
|
|
|
: BaseScopedReadWriteLock(locker)
|
2011-03-25 05:06:49 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
m_IsLocked = true;
|
|
|
|
m_lock.AcquireRead();
|
2011-03-25 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// provides manual locking of a scoped lock, to re-lock after a manual unlocking.
|
|
|
|
void Threading::ScopedReadLock::Acquire()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (m_IsLocked)
|
|
|
|
return;
|
|
|
|
m_lock.AcquireRead();
|
|
|
|
m_IsLocked = true;
|
2011-03-25 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
Threading::ScopedWriteLock::ScopedWriteLock(RwMutex& locker)
|
|
|
|
: BaseScopedReadWriteLock(locker)
|
2011-03-25 05:06:49 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
m_IsLocked = true;
|
|
|
|
m_lock.AcquireWrite();
|
2011-03-25 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// provides manual locking of a scoped lock, to re-lock after a manual unlocking.
|
|
|
|
void Threading::ScopedWriteLock::Acquire()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (m_IsLocked)
|
|
|
|
return;
|
|
|
|
m_lock.AcquireWrite();
|
|
|
|
m_IsLocked = true;
|
2011-03-25 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Special constructor used by ScopedTryLock
|
2021-09-06 18:28:26 +00:00
|
|
|
Threading::ScopedWriteLock::ScopedWriteLock(RwMutex& locker, bool isTryLock)
|
|
|
|
: BaseScopedReadWriteLock(locker)
|
2011-03-25 05:06:49 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
//m_IsLocked = isTryLock ? m_lock.TryAcquireWrite() : false;
|
2011-03-25 05:06:49 +00:00
|
|
|
}
|