2009-10-17 18:21:30 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
2010-05-03 14:08:02 +00:00
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
2009-10-17 18:21:30 +00:00
|
|
|
*
|
|
|
|
* 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/Threading.h"
|
|
|
|
#include "common/wxBaseTools.h"
|
|
|
|
#include "common/ThreadingInternal.h"
|
2009-10-17 18:21:30 +00:00
|
|
|
|
|
|
|
namespace Threading
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
static std::atomic<int> _attr_refcount(0);
|
|
|
|
static pthread_mutexattr_t _attr_recursive;
|
|
|
|
} // namespace Threading
|
2009-10-17 18:21:30 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
2009-10-31 19:18:29 +00:00
|
|
|
// Mutex Implementations
|
2009-10-17 18:21:30 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
|
2015-11-17 17:38:26 +00:00
|
|
|
#if defined(_WIN32) || (defined(_POSIX_TIMEOUTS) && _POSIX_TIMEOUTS >= 200112L)
|
|
|
|
// good, we have pthread_mutex_timedlock
|
|
|
|
#define xpthread_mutex_timedlock pthread_mutex_timedlock
|
|
|
|
#else
|
|
|
|
// We have to emulate pthread_mutex_timedlock(). This could be a serious
|
|
|
|
// performance drain if its used a lot.
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
#include <sys/time.h> // gettimeofday()
|
2015-11-17 17:38:26 +00:00
|
|
|
|
|
|
|
// sleep for 10ms at a time
|
|
|
|
#define TIMEDLOCK_EMU_SLEEP_NS 10000000ULL
|
|
|
|
|
|
|
|
// Original POSIX docs:
|
|
|
|
//
|
|
|
|
// The pthread_mutex_timedlock() function shall lock the mutex object
|
|
|
|
// referenced by mutex. If the mutex is already locked, the calling thread
|
|
|
|
// shall block until the mutex becomes available as in the
|
|
|
|
// pthread_mutex_lock() function. If the mutex cannot be locked without
|
|
|
|
// waiting for another thread to unlock the mutex, this wait shall be
|
|
|
|
// terminated when the specified timeout expires.
|
|
|
|
//
|
|
|
|
// This is an implementation that emulates pthread_mutex_timedlock() via
|
|
|
|
// pthread_mutex_trylock().
|
|
|
|
static int xpthread_mutex_timedlock(
|
2021-09-06 18:28:26 +00:00
|
|
|
pthread_mutex_t* mutex,
|
|
|
|
const struct timespec* abs_timeout)
|
2015-11-17 17:38:26 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
while ((err = pthread_mutex_trylock(mutex)) == EBUSY)
|
|
|
|
{
|
|
|
|
// check if the timeout has expired, gettimeofday() is implemented
|
|
|
|
// efficiently (in userspace) on OSX
|
|
|
|
struct timeval now;
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
if (now.tv_sec > abs_timeout->tv_sec || (now.tv_sec == abs_timeout->tv_sec && (u64)now.tv_usec * 1000ULL > (u64)abs_timeout->tv_nsec))
|
|
|
|
{
|
|
|
|
return ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// acquiring lock failed, sleep some
|
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = TIMEDLOCK_EMU_SLEEP_NS;
|
|
|
|
while (nanosleep(&ts, &ts) == -1)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2015-11-17 17:38:26 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-10-31 19:18:29 +00:00
|
|
|
Threading::Mutex::Mutex()
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
pthread_mutex_init(&m_mutex, NULL);
|
2009-10-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
static wxTimeSpan def_detach_timeout(0, 0, 6, 0);
|
2010-04-27 13:12:03 +00:00
|
|
|
|
2009-10-31 19:18:29 +00:00
|
|
|
void Threading::Mutex::Detach()
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (EBUSY != pthread_mutex_destroy(&m_mutex))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (IsRecursive())
|
|
|
|
{
|
|
|
|
// Sanity check: Recursive locks could be held by our own thread, which would
|
|
|
|
// be considered an assertion failure, but can also be handled gracefully.
|
|
|
|
// (note: if the mutex is locked recursively more than twice then this assert won't
|
|
|
|
// detect it)
|
|
|
|
|
|
|
|
Release();
|
|
|
|
Release(); // in case of double recursion.
|
|
|
|
int result = pthread_mutex_destroy(&m_mutex);
|
|
|
|
if (pxAssertDev(result != EBUSY, "Detachment of a recursively-locked mutex (self-locked!)."))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Wait(def_detach_timeout))
|
|
|
|
pthread_mutex_destroy(&m_mutex);
|
|
|
|
else
|
|
|
|
Console.Error("(Thread Log) Mutex cleanup failed due to possible deadlock.");
|
2009-10-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 12:22:00 +00:00
|
|
|
Threading::Mutex::~Mutex()
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Mutex::Detach();
|
|
|
|
}
|
|
|
|
DESTRUCTOR_CATCHALL;
|
2009-10-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
Threading::MutexRecursive::MutexRecursive()
|
2021-09-06 18:28:26 +00:00
|
|
|
: Mutex(false)
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (++_attr_refcount == 1)
|
|
|
|
{
|
|
|
|
if (0 != pthread_mutexattr_init(&_attr_recursive))
|
|
|
|
throw Exception::OutOfMemory(L"Recursive mutexing attributes");
|
2009-10-17 18:21:30 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
pthread_mutexattr_settype(&_attr_recursive, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
}
|
2009-10-17 18:21:30 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
if (pthread_mutex_init(&m_mutex, &_attr_recursive))
|
|
|
|
Console.Error("(Thread Log) Failed to initialize mutex.");
|
2009-10-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 12:22:00 +00:00
|
|
|
Threading::MutexRecursive::~MutexRecursive()
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (--_attr_refcount == 0)
|
|
|
|
pthread_mutexattr_destroy(&_attr_recursive);
|
2009-10-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is a bit of a hackish function, which is technically unsafe, but can be useful for allowing
|
|
|
|
// the application to survive unexpected or inconvenient failures, where a mutex is deadlocked by
|
|
|
|
// a rogue thread. This function allows us to Recreate the mutex and let the deadlocked one ponder
|
|
|
|
// the deeper meanings of the universe for eternity.
|
2009-10-31 19:18:29 +00:00
|
|
|
void Threading::Mutex::Recreate()
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
Detach();
|
|
|
|
pthread_mutex_init(&m_mutex, NULL);
|
2009-10-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns:
|
|
|
|
// true if the mutex had to be recreated due to lock contention, or false if the mutex is safely
|
|
|
|
// unlocked.
|
2009-10-31 19:18:29 +00:00
|
|
|
bool Threading::Mutex::RecreateIfLocked()
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (!Wait(def_detach_timeout))
|
|
|
|
{
|
|
|
|
Recreate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2009-10-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-31 19:18:29 +00:00
|
|
|
// This is a direct blocking action -- very fast, very efficient, and generally very dangerous
|
|
|
|
// if used from the main GUI thread, since it typically results in an unresponsive program.
|
|
|
|
// Call this method directly only if you know the code in question will be run from threads
|
2010-04-25 00:31:27 +00:00
|
|
|
// other than the main thread.
|
2009-11-01 09:27:16 +00:00
|
|
|
void Threading::Mutex::AcquireWithoutYield()
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
pxAssertMsg(!wxThread::IsMain(), "Unyielding mutex acquire issued from the main/gui thread. Please use Acquire() instead.");
|
|
|
|
pthread_mutex_lock(&m_mutex);
|
2009-10-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
bool Threading::Mutex::AcquireWithoutYield(const wxTimeSpan& timeout)
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
wxDateTime megafail(wxDateTime::UNow() + timeout);
|
|
|
|
const timespec fail = {megafail.GetTicks(), megafail.GetMillisecond() * 1000000};
|
|
|
|
return xpthread_mutex_timedlock(&m_mutex, &fail) == 0;
|
2009-10-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
2009-10-31 19:18:29 +00:00
|
|
|
void Threading::Mutex::Release()
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
pthread_mutex_unlock(&m_mutex);
|
2009-10-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
2009-10-31 19:25:46 +00:00
|
|
|
bool Threading::Mutex::TryAcquire()
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
return EBUSY != pthread_mutex_trylock(&m_mutex);
|
2009-10-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
2009-11-01 09:27:16 +00:00
|
|
|
// This is a wxApp-safe rendition of AcquireWithoutYield, which makes sure to execute pending app events
|
2009-10-17 18:21:30 +00:00
|
|
|
// and messages *if* the lock is performed from the main GUI thread.
|
|
|
|
//
|
2009-10-31 19:25:46 +00:00
|
|
|
void Threading::Mutex::Acquire()
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
|
|
|
#if wxUSE_GUI
|
2021-09-06 18:28:26 +00:00
|
|
|
if (!wxThread::IsMain() || (wxTheApp == NULL))
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&m_mutex);
|
|
|
|
}
|
|
|
|
else if (_WaitGui_RecursionGuard(L"Mutex::Acquire"))
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&m_mutex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//ScopedBusyCursor hourglass( Cursor_KindaBusy );
|
|
|
|
while (!AcquireWithoutYield(def_yieldgui_interval))
|
|
|
|
YieldToMain();
|
|
|
|
}
|
2009-10-17 18:21:30 +00:00
|
|
|
#else
|
2021-09-06 18:28:26 +00:00
|
|
|
pthread_mutex_lock(&m_mutex);
|
2009-10-17 18:21:30 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
bool Threading::Mutex::Acquire(const wxTimeSpan& timeout)
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
|
|
|
#if wxUSE_GUI
|
2021-09-06 18:28:26 +00:00
|
|
|
if (!wxThread::IsMain() || (wxTheApp == NULL))
|
|
|
|
{
|
|
|
|
return AcquireWithoutYield(timeout);
|
|
|
|
}
|
|
|
|
else if (_WaitGui_RecursionGuard(L"Mutex::TimedAcquire"))
|
|
|
|
{
|
|
|
|
return AcquireWithoutYield(timeout);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//ScopedBusyCursor hourglass( Cursor_KindaBusy );
|
|
|
|
wxTimeSpan countdown((timeout));
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (AcquireWithoutYield(def_yieldgui_interval))
|
|
|
|
break;
|
|
|
|
YieldToMain();
|
|
|
|
countdown -= def_yieldgui_interval;
|
|
|
|
} while (countdown.GetMilliseconds() > 0);
|
|
|
|
|
|
|
|
return countdown.GetMilliseconds() > 0;
|
|
|
|
}
|
2009-10-17 18:21:30 +00:00
|
|
|
|
|
|
|
#else
|
2021-10-01 05:59:33 +00:00
|
|
|
return AcquireWithoutYield(timeout);
|
2009-10-17 18:21:30 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Performs a wait on a locked mutex, or returns instantly if the mutex is unlocked.
|
|
|
|
// Typically this action is used to determine if a thread is currently performing some
|
|
|
|
// specific task, and to block until the task is finished (PersistentThread uses it to
|
|
|
|
// determine if the thread is running or completed, for example).
|
|
|
|
//
|
2009-10-31 19:25:46 +00:00
|
|
|
// Implemented internally as a simple Acquire/Release pair.
|
2009-10-17 18:21:30 +00:00
|
|
|
//
|
2009-10-31 19:18:29 +00:00
|
|
|
void Threading::Mutex::Wait()
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
Acquire();
|
|
|
|
Release();
|
2009-10-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
2010-04-27 13:12:03 +00:00
|
|
|
void Threading::Mutex::WaitWithoutYield()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
AcquireWithoutYield();
|
|
|
|
Release();
|
2010-04-27 13:12:03 +00:00
|
|
|
}
|
|
|
|
|
2009-10-17 18:21:30 +00:00
|
|
|
// Performs a wait on a locked mutex, or returns instantly if the mutex is unlocked.
|
2009-10-31 19:25:46 +00:00
|
|
|
// (Implemented internally as a simple Acquire/Release pair.)
|
2009-10-17 18:21:30 +00:00
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the mutex was freed and is in an unlocked state; or false if the wait timed out
|
|
|
|
// and the mutex is still locked by another thread.
|
|
|
|
//
|
2021-09-06 18:28:26 +00:00
|
|
|
bool Threading::Mutex::Wait(const wxTimeSpan& timeout)
|
2009-10-17 18:21:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (Acquire(timeout))
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2009-10-17 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
bool Threading::Mutex::WaitWithoutYield(const wxTimeSpan& timeout)
|
2010-04-27 13:12:03 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (AcquireWithoutYield(timeout))
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-04-27 13:12:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// ScopedLock Implementations
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
|
2017-05-06 12:22:00 +00:00
|
|
|
Threading::ScopedLock::~ScopedLock()
|
2010-04-27 13:12:03 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (m_IsLocked && m_lock)
|
|
|
|
m_lock->Release();
|
2010-04-27 13:12:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
Threading::ScopedLock::ScopedLock(const Mutex* locker)
|
2010-04-27 13:12:03 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
m_IsLocked = false;
|
|
|
|
AssignAndLock(locker);
|
2010-04-27 13:12:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
Threading::ScopedLock::ScopedLock(const Mutex& locker)
|
2010-04-27 13:12:03 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
m_IsLocked = false;
|
|
|
|
AssignAndLock(locker);
|
2010-04-27 13:12:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
void Threading::ScopedLock::AssignAndLock(const Mutex& locker)
|
2010-04-27 13:12:03 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
AssignAndLock(&locker);
|
2010-04-27 13:12:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
void Threading::ScopedLock::AssignAndLock(const Mutex* locker)
|
2010-04-27 13:12:03 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
pxAssert(!m_IsLocked); // if we're already locked, changing the lock is bad mojo.
|
2010-08-31 16:40:25 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
m_lock = const_cast<Mutex*>(locker);
|
|
|
|
if (!m_lock)
|
|
|
|
return;
|
2010-04-27 13:12:03 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
m_IsLocked = true;
|
|
|
|
m_lock->Acquire();
|
2010-04-27 13:12:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
void Threading::ScopedLock::Assign(const Mutex& locker)
|
2010-05-03 13:51:46 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
m_lock = const_cast<Mutex*>(&locker);
|
2010-05-03 13:51:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
void Threading::ScopedLock::Assign(const Mutex* locker)
|
2010-05-03 13:51:46 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
m_lock = const_cast<Mutex*>(locker);
|
2010-05-03 13:51:46 +00:00
|
|
|
}
|
|
|
|
|
2010-04-27 13:12:03 +00:00
|
|
|
// Provides manual unlocking of a scoped lock prior to object destruction.
|
|
|
|
void Threading::ScopedLock::Release()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (!m_IsLocked)
|
|
|
|
return;
|
|
|
|
m_IsLocked = false;
|
|
|
|
if (m_lock)
|
|
|
|
m_lock->Release();
|
2010-04-27 13:12:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// provides manual locking of a scoped lock, to re-lock after a manual unlocking.
|
|
|
|
void Threading::ScopedLock::Acquire()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (m_IsLocked || !m_lock)
|
|
|
|
return;
|
|
|
|
m_lock->Acquire();
|
|
|
|
m_IsLocked = true;
|
2010-04-27 13:12:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
Threading::ScopedLock::ScopedLock(const Mutex& locker, bool isTryLock)
|
2010-04-27 13:12:03 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
m_lock = const_cast<Mutex*>(&locker);
|
|
|
|
if (!m_lock)
|
|
|
|
return;
|
|
|
|
m_IsLocked = isTryLock ? m_lock->TryAcquire() : false;
|
2010-04-27 13:12:03 +00:00
|
|
|
}
|