2015-11-17 17:30:20 +00:00
|
|
|
/* 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/>.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|