mirror of
https://github.com/libretro/parallel-ps2.git
synced 2026-11-13 04:41:40 -10:00
Three things, and the middle one is the reason for the other two. libretro-common is brought level with RetroArch master. Five files differed and one of them stays as it is -- include/compat/ifaddrs.h keeps the extern "C" guard ps2 added for DEV9, which is not upstream and without which the core fails to link; the other four are RetroArch being ahead, checked line by line. Three files are new: retro_procbarrier.c, the process-wide barrier, retro_eventcount.c, and retro_asym_eventcount.c, which is built on both. Makefile.common builds all three. WorkSema is rewritten on two of those. It was a hand-rolled state machine whose NotifyOfWork -- what the EE pays per GS packet with the GS thread awake, and what MTVU's own comment already named as the fetch_add on its hot path -- measured 6.4 ns. It is now retro_asym_eventcount's notify, a release store and a relaxed load with no lock prefix, measured 1.2 ns in the same binary and the same run. The symmetric retro_eventcount was measured too, at 9.6 ns, and would have been a regression: it pays an RMW and a seq_cst load to serve many producers, and each WorkSema here has one. The ordering the hot side drops is paid where WaitForWork parks, by retro_procbarrier; where no barrier exists the object runs the symmetric protocol for its whole life, decided at init, and MTGS never needs to know. WaitForEmpty's direction -- worker to producer, once a frame -- is the plain retro_eventcount, whose prepare/commit contract is what STATE_FLAG_WAITING_EMPTY was reinventing. The seven operations keep their contract exactly, and tests/spsc/ worksema.cpp pins each: CheckForWork consumes a burst of notifies as one; WaitForWork returns at once with work pending and parks without; a timed wait is false only on a timeout with nothing pending; Kill unblocks every waiter and makes WaitForEmpty false; Reset revives. One subtlety is pinned because it bit the test first: "empty" means the worker went idle after consuming, not merely consumed, and a WaitForEmpty issued between the two blocks -- the old state machine did the same, checked. The producer/consumer pair runs 50000 items with WaitForEmpty interleaved 2858 times and never sees it return with work outstanding. Plain, TSan, ASan and UBSan are all clean; TSan sees the eventcounts' atomics, not the barrier, so the lost-wake half is only load-bearing on more than one core. The last part is what the rewrite made dead, plus what an audit of every common/ object against every importer in the finished build found alongside it: KernelSemaphore::WaitFor, whose only caller was the old timed wait; Mutex::TryLock; both StringUtil::ReplaceAll overloads; and DecompressBlockBC4 and BC5 with their private helper and the two Int8ToFloat conversions that served only them -- the replacement loader dispatches BC1, BC2, BC3 and BC7 and nothing else. Every header in common/ is still included by something, so nothing is removed at file level; the audit was at symbol level and discounted vtable dispatch, which is why SettingsInterface's methods, which nm cannot see a caller for, are untouched. The core links, all twelve suites pass, and the patch harness gains the three objects its Semaphore.cpp link now needs.
441 lines
10 KiB
C++
441 lines
10 KiB
C++
/* PCSX2 - PS2 Emulator for PCs
|
|
* Copyright (C) 2002-2014 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/>.
|
|
*/
|
|
|
|
#if !defined(_WIN32) && !defined(__APPLE__)
|
|
|
|
#ifndef _GNU_SOURCE
|
|
#define _GNU_SOURCE
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#include "Threading.h"
|
|
#include <utility>
|
|
|
|
#ifdef _WIN32
|
|
#include "RedtapeWindows.h"
|
|
#include <direct.h>
|
|
#else
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
#include <memory>
|
|
|
|
#ifdef _WIN32
|
|
#include <mmsystem.h>
|
|
#include <process.h>
|
|
#include <timeapi.h>
|
|
#else
|
|
#include <sched.h>
|
|
#include <pthread.h>
|
|
|
|
#if defined(__linux__)
|
|
#include <sys/prctl.h>
|
|
#include <sys/types.h>
|
|
#include <sched.h>
|
|
|
|
// glibc < v2.30 doesn't define gettid...
|
|
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
|
|
#include <sys/syscall.h>
|
|
#define gettid() syscall(SYS_gettid)
|
|
#endif
|
|
|
|
#elif defined(__APPLE__)
|
|
#include <mach/mach_init.h>
|
|
#include <mach/thread_act.h>
|
|
#include <mach/mach_port.h>
|
|
#elif defined(__unix__)
|
|
#include <pthread_np.h>
|
|
#endif
|
|
#endif
|
|
|
|
Threading::ThreadHandle::ThreadHandle() = default;
|
|
|
|
#ifdef _WIN32
|
|
Threading::ThreadHandle::ThreadHandle(const ThreadHandle& handle)
|
|
{
|
|
if (handle.m_native_handle)
|
|
{
|
|
HANDLE new_handle;
|
|
if (DuplicateHandle(GetCurrentProcess(),
|
|
(HANDLE)handle.m_native_handle,
|
|
GetCurrentProcess(),
|
|
&new_handle,
|
|
THREAD_QUERY_INFORMATION
|
|
| THREAD_SET_LIMITED_INFORMATION,
|
|
FALSE, 0))
|
|
m_native_handle = (void*)new_handle;
|
|
}
|
|
}
|
|
#else
|
|
Threading::ThreadHandle::ThreadHandle(const ThreadHandle& handle)
|
|
: m_native_handle(handle.m_native_handle)
|
|
#ifdef __linux__
|
|
, m_native_id(handle.m_native_id)
|
|
#endif
|
|
{
|
|
}
|
|
#endif
|
|
|
|
Threading::ThreadHandle::ThreadHandle(ThreadHandle&& handle)
|
|
: m_native_handle(handle.m_native_handle)
|
|
#ifdef __linux__
|
|
, m_native_id(handle.m_native_id)
|
|
#endif
|
|
{
|
|
handle.m_native_handle = nullptr;
|
|
#ifdef __linux__
|
|
handle.m_native_id = 0;
|
|
#endif
|
|
}
|
|
|
|
Threading::ThreadHandle::~ThreadHandle()
|
|
{
|
|
#ifdef _WIN32
|
|
if (m_native_handle)
|
|
CloseHandle(m_native_handle);
|
|
#endif
|
|
}
|
|
|
|
Threading::ThreadHandle Threading::ThreadHandle::GetForCallingThread()
|
|
{
|
|
ThreadHandle ret;
|
|
#ifdef _WIN32
|
|
ret.m_native_handle = (void*)OpenThread(THREAD_QUERY_INFORMATION | THREAD_SET_LIMITED_INFORMATION, FALSE, GetCurrentThreadId());
|
|
#else
|
|
ret.m_native_handle = (void*)pthread_self();
|
|
#ifdef __linux__
|
|
ret.m_native_id = gettid();
|
|
#endif
|
|
#endif
|
|
return ret;
|
|
}
|
|
|
|
Threading::Thread& Threading::Thread::operator=(Thread&& thread)
|
|
{
|
|
ThreadHandle::operator=(std::move(thread));
|
|
m_stack_size = thread.m_stack_size;
|
|
thread.m_stack_size = 0;
|
|
return *this;
|
|
}
|
|
|
|
Threading::ThreadHandle& Threading::ThreadHandle::operator=(ThreadHandle&& handle)
|
|
{
|
|
#ifdef _WIN32
|
|
if (m_native_handle)
|
|
CloseHandle((HANDLE)m_native_handle);
|
|
#endif
|
|
m_native_handle = handle.m_native_handle;
|
|
handle.m_native_handle = nullptr;
|
|
#ifdef __linux__
|
|
m_native_id = handle.m_native_id;
|
|
handle.m_native_id = 0;
|
|
#endif
|
|
return *this;
|
|
}
|
|
|
|
Threading::ThreadHandle& Threading::ThreadHandle::operator=(const ThreadHandle& handle)
|
|
{
|
|
#ifdef _WIN32
|
|
HANDLE new_handle;
|
|
if (m_native_handle)
|
|
{
|
|
CloseHandle((HANDLE)m_native_handle);
|
|
m_native_handle = nullptr;
|
|
}
|
|
|
|
if (DuplicateHandle(GetCurrentProcess(),
|
|
(HANDLE)handle.m_native_handle,
|
|
GetCurrentProcess(),
|
|
&new_handle,
|
|
THREAD_QUERY_INFORMATION
|
|
| THREAD_SET_LIMITED_INFORMATION,
|
|
FALSE, 0))
|
|
m_native_handle = (void*)new_handle;
|
|
#else
|
|
m_native_handle = handle.m_native_handle;
|
|
#ifdef __linux__
|
|
m_native_id = handle.m_native_id;
|
|
#endif
|
|
#endif
|
|
return *this;
|
|
}
|
|
|
|
bool Threading::ThreadHandle::SetAffinity(u64 processor_mask) const
|
|
{
|
|
#if defined(_WIN32)
|
|
if (processor_mask == 0)
|
|
processor_mask = ~processor_mask;
|
|
|
|
return (SetThreadAffinityMask(GetCurrentThread(),
|
|
(DWORD_PTR)processor_mask) != 0);
|
|
#elif defined(__linux__)
|
|
cpu_set_t set;
|
|
CPU_ZERO(&set);
|
|
|
|
if (processor_mask != 0)
|
|
{
|
|
for (u32 i = 0; i < 64; i++)
|
|
{
|
|
if (processor_mask & (static_cast<u64>(1) << i))
|
|
{
|
|
CPU_SET(i, &set);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
long num_processors = sysconf(_SC_NPROCESSORS_CONF);
|
|
for (long i = 0; i < num_processors; i++)
|
|
{
|
|
CPU_SET(i, &set);
|
|
}
|
|
}
|
|
|
|
return sched_setaffinity((pid_t)m_native_id, sizeof(set), &set) >= 0;
|
|
#else
|
|
// Doesn't appear to be possible to set affinity.
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
bool Threading::ThreadHandle::SetHighestPriority() const
|
|
{
|
|
#if defined(_WIN32)
|
|
return SetThreadPriority((HANDLE)m_native_handle, THREAD_PRIORITY_HIGHEST) != 0;
|
|
#else
|
|
int policy = 0;
|
|
sched_param param;
|
|
|
|
if (pthread_getschedparam((pthread_t)m_native_handle, &policy, ¶m) != 0)
|
|
return false;
|
|
param.sched_priority = sched_get_priority_max(policy);
|
|
return pthread_setschedparam((pthread_t)m_native_handle, policy, ¶m) == 0;
|
|
#endif
|
|
}
|
|
|
|
Threading::Thread::Thread() = default;
|
|
|
|
Threading::Thread::Thread(Thread&& thread)
|
|
: ThreadHandle(std::move(thread))
|
|
, m_stack_size(thread.m_stack_size)
|
|
{
|
|
thread.m_stack_size = 0;
|
|
}
|
|
|
|
Threading::Thread::Thread(EntryPoint func)
|
|
: ThreadHandle()
|
|
{
|
|
Start(std::move(func));
|
|
}
|
|
|
|
Threading::Thread::~Thread() { }
|
|
|
|
#if defined(_WIN32)
|
|
unsigned Threading::Thread::ThreadProc(void* param)
|
|
{
|
|
std::unique_ptr<EntryPoint> entry(static_cast<EntryPoint*>(param));
|
|
(*entry.get())();
|
|
return 0;
|
|
}
|
|
|
|
bool Threading::Thread::Start(EntryPoint func)
|
|
{
|
|
std::unique_ptr<EntryPoint> func_clone(std::make_unique<EntryPoint>(std::move(func)));
|
|
unsigned thread_id;
|
|
m_native_handle = reinterpret_cast<void*>(_beginthreadex(nullptr, m_stack_size, ThreadProc, func_clone.get(), 0, &thread_id));
|
|
if (!m_native_handle)
|
|
return false;
|
|
|
|
// thread started, it'll release the memory
|
|
func_clone.release();
|
|
return true;
|
|
}
|
|
|
|
#elif defined(__linux__)
|
|
// For Linux, we have to do a bit of trickery here to get
|
|
// the thread's ID back from the thread itself, because
|
|
// it's not part of pthreads. We use a semaphore to signal
|
|
// when the thread has started, and filled in thread_id_ptr.
|
|
struct ThreadProcParameters
|
|
{
|
|
Threading::Thread::EntryPoint func;
|
|
Threading::KernelSemaphore* start_semaphore;
|
|
unsigned int* thread_id_ptr;
|
|
};
|
|
|
|
void* Threading::Thread::ThreadProc(void* param)
|
|
{
|
|
std::unique_ptr<ThreadProcParameters> entry(static_cast<ThreadProcParameters*>(param));
|
|
*entry->thread_id_ptr = gettid();
|
|
entry->start_semaphore->Post();
|
|
entry->func();
|
|
return nullptr;
|
|
}
|
|
|
|
bool Threading::Thread::Start(EntryPoint func)
|
|
{
|
|
pthread_attr_t attrs;
|
|
KernelSemaphore start_semaphore;
|
|
std::unique_ptr<ThreadProcParameters> params(std::make_unique<ThreadProcParameters>());
|
|
params->func = std::move(func);
|
|
params->start_semaphore = &start_semaphore;
|
|
params->thread_id_ptr = &m_native_id;
|
|
|
|
bool has_attributes = false;
|
|
|
|
if (m_stack_size != 0)
|
|
{
|
|
has_attributes = true;
|
|
pthread_attr_init(&attrs);
|
|
}
|
|
if (m_stack_size != 0)
|
|
pthread_attr_setstacksize(&attrs, m_stack_size);
|
|
|
|
pthread_t handle;
|
|
const int res = pthread_create(&handle, has_attributes ? &attrs : nullptr, ThreadProc, params.get());
|
|
if (res != 0)
|
|
return false;
|
|
|
|
// wait until it sets our native id
|
|
start_semaphore.Wait();
|
|
|
|
// thread started, it'll release the memory
|
|
m_native_handle = (void*)handle;
|
|
params.release();
|
|
return true;
|
|
}
|
|
#else
|
|
void* Threading::Thread::ThreadProc(void* param)
|
|
{
|
|
std::unique_ptr<EntryPoint> entry(static_cast<EntryPoint*>(param));
|
|
(*entry.get())();
|
|
return nullptr;
|
|
}
|
|
|
|
bool Threading::Thread::Start(EntryPoint func)
|
|
{
|
|
std::unique_ptr<EntryPoint> func_clone(std::make_unique<EntryPoint>(std::move(func)));
|
|
|
|
pthread_attr_t attrs;
|
|
bool has_attributes = false;
|
|
|
|
if (m_stack_size != 0)
|
|
{
|
|
has_attributes = true;
|
|
pthread_attr_init(&attrs);
|
|
}
|
|
if (m_stack_size != 0)
|
|
pthread_attr_setstacksize(&attrs, m_stack_size);
|
|
|
|
pthread_t handle;
|
|
const int res = pthread_create(&handle, has_attributes ? &attrs : nullptr, ThreadProc, func_clone.get());
|
|
if (res != 0)
|
|
return false;
|
|
|
|
// thread started, it'll release the memory
|
|
m_native_handle = (void*)handle;
|
|
func_clone.release();
|
|
return true;
|
|
}
|
|
#endif
|
|
|
|
void Threading::Thread::Detach()
|
|
{
|
|
if (!m_native_handle)
|
|
return;
|
|
#ifdef _WIN32
|
|
CloseHandle((HANDLE)m_native_handle);
|
|
#else
|
|
pthread_detach((pthread_t)m_native_handle);
|
|
#ifdef __linux__
|
|
m_native_id = 0;
|
|
#endif
|
|
#endif
|
|
m_native_handle = nullptr;
|
|
}
|
|
|
|
void Threading::Thread::Join()
|
|
{
|
|
if (!m_native_handle)
|
|
return;
|
|
#ifdef _WIN32
|
|
WaitForSingleObject((HANDLE)m_native_handle, INFINITE);
|
|
CloseHandle((HANDLE)m_native_handle);
|
|
#else
|
|
void* retval;
|
|
pthread_join((pthread_t)m_native_handle, &retval);
|
|
#ifdef __linux__
|
|
m_native_id = 0;
|
|
#endif
|
|
#endif
|
|
m_native_handle = nullptr;
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
void Threading::Timeslice()
|
|
{
|
|
::SwitchToThread();
|
|
}
|
|
#else
|
|
void Threading::Timeslice()
|
|
{
|
|
sched_yield();
|
|
}
|
|
#endif
|
|
|
|
#include <rthreads/rthreads.h>
|
|
|
|
Threading::Mutex::Mutex() { m_lock = slock_new(); }
|
|
Threading::Mutex::~Mutex() { slock_free(m_lock); }
|
|
void Threading::Mutex::Lock() { slock_lock(m_lock); }
|
|
void Threading::Mutex::Unlock() { slock_unlock(m_lock); }
|
|
Threading::RecursiveMutex::RecursiveMutex()
|
|
: m_owner(0)
|
|
, m_depth(0)
|
|
{
|
|
m_lock = slock_new();
|
|
}
|
|
Threading::RecursiveMutex::~RecursiveMutex()
|
|
{
|
|
slock_free(m_lock);
|
|
}
|
|
void Threading::RecursiveMutex::Lock()
|
|
{
|
|
const uintptr_t self = sthread_get_current_thread_id();
|
|
if (m_owner == self)
|
|
{
|
|
m_depth++;
|
|
return;
|
|
}
|
|
slock_lock(m_lock);
|
|
m_owner = self;
|
|
m_depth = 1;
|
|
}
|
|
void Threading::RecursiveMutex::Unlock()
|
|
{
|
|
if (--m_depth == 0)
|
|
{
|
|
m_owner = 0;
|
|
slock_unlock(m_lock);
|
|
}
|
|
}
|
|
|
|
Threading::CondVar::CondVar() { m_cond = scond_new(); }
|
|
Threading::CondVar::~CondVar() { scond_free(m_cond); }
|
|
void Threading::CondVar::Wait(Mutex& m) { scond_wait(m_cond, m.Native()); }
|
|
void Threading::CondVar::Signal() { scond_signal(m_cond); }
|
|
void Threading::CondVar::Broadcast() { scond_broadcast(m_cond); }
|