2023-12-22 11:57:49 +00:00
|
|
|
// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0+
|
2015-11-17 17:30:20 +00:00
|
|
|
|
2021-09-03 10:43:33 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
2015-11-17 17:30:20 +00:00
|
|
|
#include <cstdio>
|
2016-11-12 15:28:37 +00:00
|
|
|
#include <cassert> // assert
|
|
|
|
#include <pthread.h> // pthread_setcancelstate()
|
|
|
|
#include <sys/time.h> // gettimeofday()
|
2015-11-17 17:30:20 +00:00
|
|
|
#include <mach/mach.h>
|
2021-09-06 18:28:26 +00:00
|
|
|
#include <mach/task.h> // semaphore_create() and semaphore_destroy()
|
|
|
|
#include <mach/semaphore.h> // semaphore_*()
|
2016-11-12 15:28:37 +00:00
|
|
|
#include <mach/mach_error.h> // mach_error_string()
|
2021-09-06 18:28:26 +00:00
|
|
|
#include <mach/mach_time.h> // mach_absolute_time()
|
2015-11-17 17:30:20 +00:00
|
|
|
|
2021-09-03 10:43:33 +00:00
|
|
|
#include "common/Threading.h"
|
2015-11-17 17:30:20 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// Semaphore Implementation for Darwin/OSX
|
|
|
|
//
|
|
|
|
// Sadly, Darwin/OSX needs its own implementation of Semaphores instead of
|
|
|
|
// relying on phtreads, because OSX unnamed semaphore (the best kind)
|
|
|
|
// support is very poor.
|
|
|
|
//
|
|
|
|
// This implementation makes use of Mach primitives instead. These are also
|
|
|
|
// what Grand Central Dispatch (GCD) is based on, as far as I understand:
|
|
|
|
// http://newosxbook.com/articles/GCD.html.
|
|
|
|
//
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
|
2020-05-24 06:19:47 +00:00
|
|
|
static void MACH_CHECK(kern_return_t mach_retval)
|
|
|
|
{
|
2022-11-27 21:13:57 +00:00
|
|
|
if (mach_retval != KERN_SUCCESS)
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2022-11-27 21:13:57 +00:00
|
|
|
fprintf(stderr, "mach error: %s", mach_error_string(mach_retval));
|
|
|
|
assert(mach_retval == KERN_SUCCESS);
|
2021-09-06 18:28:26 +00:00
|
|
|
}
|
2020-05-24 06:19:47 +00:00
|
|
|
}
|
2015-11-17 17:30:20 +00:00
|
|
|
|
2022-03-25 10:12:52 +00:00
|
|
|
Threading::KernelSemaphore::KernelSemaphore()
|
|
|
|
{
|
|
|
|
MACH_CHECK(semaphore_create(mach_task_self(), &m_sema, SYNC_POLICY_FIFO, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
Threading::KernelSemaphore::~KernelSemaphore()
|
|
|
|
{
|
|
|
|
MACH_CHECK(semaphore_destroy(mach_task_self(), m_sema));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Threading::KernelSemaphore::Post()
|
|
|
|
{
|
|
|
|
MACH_CHECK(semaphore_signal(m_sema));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Threading::KernelSemaphore::Wait()
|
|
|
|
{
|
|
|
|
MACH_CHECK(semaphore_wait(m_sema));
|
|
|
|
}
|
|
|
|
|
2022-05-05 13:01:12 +00:00
|
|
|
bool Threading::KernelSemaphore::TryWait()
|
|
|
|
{
|
|
|
|
mach_timespec_t time = {};
|
|
|
|
kern_return_t res = semaphore_timedwait(m_sema, time);
|
|
|
|
if (res == KERN_OPERATION_TIMED_OUT)
|
|
|
|
return false;
|
|
|
|
MACH_CHECK(res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-17 17:30:20 +00:00
|
|
|
#endif
|
2021-09-06 18:28:26 +00:00
|
|
|
|