beetle-psx-libretro/libretro-common/rthreads/rthreads.c

961 lines
26 KiB
C
Raw Normal View History

2020-06-29 22:23:58 +00:00
/* Copyright (C) 2010-2020 The RetroArch team
2015-10-03 06:04:28 +00:00
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (rthreads.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2016-04-08 21:59:49 +00:00
#ifdef __unix__
2018-04-12 23:09:29 +00:00
#ifndef __sun__
2016-04-08 21:59:49 +00:00
#define _POSIX_C_SOURCE 199309
#endif
2018-04-12 23:09:29 +00:00
#endif
2016-04-08 21:59:49 +00:00
2015-10-03 06:04:28 +00:00
#include <stdlib.h>
2019-07-08 06:21:45 +00:00
#include <string.h>
2015-10-03 06:04:28 +00:00
#include <boolean.h>
2015-10-03 06:04:28 +00:00
#include <rthreads/rthreads.h>
2016-04-08 21:59:49 +00:00
/* with RETRO_WIN32_USE_PTHREADS, pthreads can be used even on win32. Maybe only supported in MSVC>=2005 */
#if defined(_WIN32) && !defined(RETRO_WIN32_USE_PTHREADS)
#define USE_WIN32_THREADS
2015-10-03 06:04:28 +00:00
#ifdef _XBOX
#include <xtl.h>
#else
#define WIN32_LEAN_AND_MEAN
2017-07-26 01:01:05 +00:00
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500 /*_WIN32_WINNT_WIN2K */
#endif
2015-10-03 06:04:28 +00:00
#include <windows.h>
2017-07-26 01:01:05 +00:00
#include <mmsystem.h>
2015-10-03 06:04:28 +00:00
#endif
#elif defined(GEKKO)
#include "gx_pthread.h"
2018-04-12 23:09:29 +00:00
#elif defined(_3DS)
#include "ctr_pthread.h"
2016-08-19 10:44:14 +00:00
#elif defined(__CELLOS_LV2__)
#include <pthread.h>
#include <sys/sys_time.h>
2015-10-03 06:04:28 +00:00
#else
#include <pthread.h>
#include <time.h>
#endif
2019-07-08 06:21:45 +00:00
#if defined(VITA) || defined(BSD) || defined(ORBIS)
2016-08-19 10:44:14 +00:00
#include <sys/time.h>
#endif
2015-10-03 06:04:28 +00:00
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
struct thread_data
{
void (*func)(void*);
void *userdata;
};
struct sthread
{
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2015-10-03 06:04:28 +00:00
HANDLE thread;
2018-04-12 23:09:29 +00:00
DWORD id;
2015-10-03 06:04:28 +00:00
#else
pthread_t id;
#endif
};
struct slock
{
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2017-07-26 01:01:05 +00:00
CRITICAL_SECTION lock;
2015-10-03 06:04:28 +00:00
#else
pthread_mutex_t lock;
#endif
};
2017-07-26 01:01:05 +00:00
#ifdef USE_WIN32_THREADS
/* The syntax we'll use is mind-bending unless we use a struct. Plus, we might want to store more info later */
/* This will be used as a linked list immplementing a queue of waiting threads */
struct QueueEntry
{
struct QueueEntry *next;
};
#endif
2015-10-03 06:04:28 +00:00
struct scond
{
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2018-01-05 12:22:00 +00:00
/* With this implementation of scond, we don't have any way of waking
2017-07-26 01:01:05 +00:00
* (or even identifying) specific threads
* But we need to wake them in the order indicated by the queue.
2018-01-05 12:22:00 +00:00
* This potato token will get get passed around every waiter.
2017-07-26 01:01:05 +00:00
* The bearer can test whether he's next, and hold onto the potato if he is.
2018-01-05 12:22:00 +00:00
* When he's done he can then put it back into play to progress
2017-07-26 01:01:05 +00:00
* the queue further */
HANDLE hot_potato;
/* The primary signalled event. Hot potatoes are passed until this is set. */
2015-10-03 06:04:28 +00:00
HANDLE event;
2017-07-26 01:01:05 +00:00
/* the head of the queue; NULL if queue is empty */
struct QueueEntry *head;
/* equivalent to the queue length */
int waiters;
2018-01-05 12:22:00 +00:00
/* how many waiters in the queue have been conceptually wakened by signals
2017-07-26 01:01:05 +00:00
* (even if we haven't managed to actually wake them yet) */
int wakens;
/* used to control access to this scond, in case the user fails */
CRITICAL_SECTION cs;
2015-10-03 06:04:28 +00:00
#else
pthread_cond_t cond;
#endif
};
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2015-10-03 06:04:28 +00:00
static DWORD CALLBACK thread_wrap(void *data_)
#else
static void *thread_wrap(void *data_)
#endif
{
struct thread_data *data = (struct thread_data*)data_;
if (!data)
2017-07-26 01:01:05 +00:00
return 0;
2015-10-03 06:04:28 +00:00
data->func(data->userdata);
free(data);
return 0;
}
2015-10-03 06:04:28 +00:00
/**
* sthread_create:
* @start_routine : thread entry callback function
* @userdata : pointer to userdata that will be made
* available in thread entry callback function
*
* Create a new thread.
*
* Returns: pointer to new thread if successful, otherwise NULL.
*/
sthread_t *sthread_create(void (*thread_func)(void*), void *userdata)
{
2019-07-08 06:21:45 +00:00
return sthread_create_with_priority(thread_func, userdata, 0);
}
/* TODO/FIXME - this needs to be implemented for Switch/3DS */
#if !defined(SWITCH) && !defined(USE_WIN32_THREADS) && !defined(_3DS) && !defined(GEKKO) && !defined(__HAIKU__)
#define HAVE_THREAD_ATTR
#endif
/**
* sthread_create_with_priority:
* @start_routine : thread entry callback function
* @userdata : pointer to userdata that will be made
* available in thread entry callback function
* @thread_priority : thread priority hint value from [1-100]
*
* Create a new thread. It is possible for the caller to give a hint
* for the thread's priority from [1-100]. Any passed in @thread_priority
* values that are outside of this range will cause sthread_create() to
* create a new thread using the operating system's default thread
* priority.
*
* Returns: pointer to new thread if successful, otherwise NULL.
*/
sthread_t *sthread_create_with_priority(void (*thread_func)(void*), void *userdata, int thread_priority)
{
#ifdef HAVE_THREAD_ATTR
pthread_attr_t thread_attr;
bool thread_attr_needed = false;
#endif
bool thread_created = false;
struct thread_data *data = NULL;
2020-06-29 22:23:58 +00:00
sthread_t *thread = (sthread_t*)malloc(sizeof(*thread));
2018-04-12 23:09:29 +00:00
2015-10-03 06:04:28 +00:00
if (!thread)
return NULL;
2020-06-29 22:23:58 +00:00
data = (struct thread_data*)malloc(sizeof(*data));
2015-10-03 06:04:28 +00:00
if (!data)
goto error;
2015-10-03 06:04:28 +00:00
2019-07-08 06:21:45 +00:00
data->func = thread_func;
data->userdata = userdata;
2015-10-03 06:04:28 +00:00
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2020-06-29 22:23:58 +00:00
thread->id = 0;
thread->thread = CreateThread(NULL, 0, thread_wrap,
data, 0, &thread->id);
2019-07-08 06:21:45 +00:00
thread_created = !!thread->thread;
2017-07-26 01:01:05 +00:00
#else
2020-06-29 22:23:58 +00:00
thread->id = 0;
2019-07-08 06:21:45 +00:00
#ifdef HAVE_THREAD_ATTR
2017-07-26 01:01:05 +00:00
pthread_attr_init(&thread_attr);
2019-07-08 06:21:45 +00:00
2020-06-29 22:23:58 +00:00
if ((thread_priority >= 1) && (thread_priority <= 100))
2019-07-08 06:21:45 +00:00
{
struct sched_param sp;
memset(&sp, 0, sizeof(struct sched_param));
sp.sched_priority = thread_priority;
pthread_attr_setschedpolicy(&thread_attr, SCHED_RR);
pthread_attr_setschedparam(&thread_attr, &sp);
thread_attr_needed = true;
}
#endif
#if defined(VITA)
2017-07-26 01:01:05 +00:00
pthread_attr_setstacksize(&thread_attr , 0x10000 );
2019-07-08 06:21:45 +00:00
thread_attr_needed = true;
2017-07-26 01:01:05 +00:00
#endif
2019-07-08 06:21:45 +00:00
#ifdef HAVE_THREAD_ATTR
if (thread_attr_needed)
thread_created = pthread_create(&thread->id, &thread_attr, thread_wrap, data) == 0;
else
2015-10-03 06:04:28 +00:00
#endif
2019-07-08 06:21:45 +00:00
thread_created = pthread_create(&thread->id, NULL, thread_wrap, data) == 0;
2019-07-08 06:21:45 +00:00
#ifdef HAVE_THREAD_ATTR
pthread_attr_destroy(&thread_attr);
#endif
#endif
2015-10-03 06:04:28 +00:00
2019-07-08 06:21:45 +00:00
if (thread_created)
return thread;
error:
if (data)
free(data);
free(thread);
return NULL;
2015-10-03 06:04:28 +00:00
}
/**
* sthread_detach:
2017-07-26 01:01:05 +00:00
* @thread : pointer to thread object
2015-10-03 06:04:28 +00:00
*
* Detach a thread. When a detached thread terminates, its
2017-07-26 01:01:05 +00:00
* resources are automatically released back to the system
* without the need for another thread to join with the
2015-10-03 06:04:28 +00:00
* terminated thread.
*
* Returns: 0 on success, otherwise it returns a non-zero error number.
*/
int sthread_detach(sthread_t *thread)
{
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2015-10-03 06:04:28 +00:00
CloseHandle(thread->thread);
free(thread);
return 0;
#else
2020-06-29 22:23:58 +00:00
int ret = pthread_detach(thread->id);
free(thread);
return ret;
2015-10-03 06:04:28 +00:00
#endif
}
/**
* sthread_join:
2017-07-26 01:01:05 +00:00
* @thread : pointer to thread object
2015-10-03 06:04:28 +00:00
*
* Join with a terminated thread. Waits for the thread specified by
* @thread to terminate. If that thread has already terminated, then
* it will return immediately. The thread specified by @thread must
* be joinable.
2017-07-26 01:01:05 +00:00
*
2015-10-03 06:04:28 +00:00
* Returns: 0 on success, otherwise it returns a non-zero error number.
*/
void sthread_join(sthread_t *thread)
{
2018-01-05 12:22:00 +00:00
if (!thread)
return;
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2015-10-03 06:04:28 +00:00
WaitForSingleObject(thread->thread, INFINITE);
CloseHandle(thread->thread);
#else
pthread_join(thread->id, NULL);
#endif
free(thread);
}
2016-04-08 21:59:49 +00:00
/**
* sthread_isself:
2017-07-26 01:01:05 +00:00
* @thread : pointer to thread object
2016-04-08 21:59:49 +00:00
*
* Returns: true (1) if calling thread is the specified thread
*/
bool sthread_isself(sthread_t *thread)
{
2017-07-26 01:01:05 +00:00
/* This thread can't possibly be a null thread */
2018-04-12 23:09:29 +00:00
if (!thread)
return false;
2017-07-26 01:01:05 +00:00
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2018-04-12 23:09:29 +00:00
return GetCurrentThreadId() == thread->id;
2016-04-08 21:59:49 +00:00
#else
return pthread_equal(pthread_self(),thread->id);
#endif
}
2015-10-03 06:04:28 +00:00
/**
* slock_new:
*
* Create and initialize a new mutex. Must be manually
* freed.
*
* Returns: pointer to a new mutex if successful, otherwise NULL.
**/
slock_t *slock_new(void)
{
2017-07-26 01:01:05 +00:00
bool mutex_created = false;
slock_t *lock = (slock_t*)calloc(1, sizeof(*lock));
2015-10-03 06:04:28 +00:00
if (!lock)
return NULL;
2020-06-29 22:23:58 +00:00
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2017-07-26 01:01:05 +00:00
InitializeCriticalSection(&lock->lock);
2020-06-29 22:23:58 +00:00
mutex_created = true;
2015-10-03 06:04:28 +00:00
#else
2020-06-29 22:23:58 +00:00
mutex_created = (pthread_mutex_init(&lock->lock, NULL) == 0);
2016-08-19 10:44:14 +00:00
#endif
2015-10-03 06:04:28 +00:00
2017-07-26 01:01:05 +00:00
if (!mutex_created)
goto error;
2015-10-03 06:04:28 +00:00
return lock;
error:
2017-07-26 01:01:05 +00:00
free(lock);
return NULL;
2015-10-03 06:04:28 +00:00
}
/**
* slock_free:
2017-07-26 01:01:05 +00:00
* @lock : pointer to mutex object
2015-10-03 06:04:28 +00:00
*
* Frees a mutex.
**/
void slock_free(slock_t *lock)
{
if (!lock)
return;
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2017-07-26 01:01:05 +00:00
DeleteCriticalSection(&lock->lock);
2015-10-03 06:04:28 +00:00
#else
pthread_mutex_destroy(&lock->lock);
#endif
free(lock);
}
/**
* slock_lock:
2017-07-26 01:01:05 +00:00
* @lock : pointer to mutex object
2015-10-03 06:04:28 +00:00
*
* Locks a mutex. If a mutex is already locked by
* another thread, the calling thread shall block until
* the mutex becomes available.
**/
void slock_lock(slock_t *lock)
{
2017-07-26 01:01:05 +00:00
if (!lock)
return;
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2017-07-26 01:01:05 +00:00
EnterCriticalSection(&lock->lock);
2015-10-03 06:04:28 +00:00
#else
pthread_mutex_lock(&lock->lock);
#endif
}
2020-06-29 22:23:58 +00:00
/**
* slock_try_lock:
* @lock : pointer to mutex object
*
* Attempts to lock a mutex. If a mutex is already locked by
* another thread, return false. If the lock is acquired, return true.
**/
bool slock_try_lock(slock_t *lock)
{
if (!lock)
return false;
#ifdef USE_WIN32_THREADS
return TryEnterCriticalSection(&lock->lock);
#else
return pthread_mutex_trylock(&lock->lock)==0;
#endif
}
2015-10-03 06:04:28 +00:00
/**
* slock_unlock:
2017-07-26 01:01:05 +00:00
* @lock : pointer to mutex object
2015-10-03 06:04:28 +00:00
*
* Unlocks a mutex.
**/
void slock_unlock(slock_t *lock)
{
2017-07-26 01:01:05 +00:00
if (!lock)
return;
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2017-07-26 01:01:05 +00:00
LeaveCriticalSection(&lock->lock);
2015-10-03 06:04:28 +00:00
#else
pthread_mutex_unlock(&lock->lock);
#endif
}
/**
* scond_new:
*
* Creates and initializes a condition variable. Must
* be manually freed.
*
* Returns: pointer to new condition variable on success,
* otherwise NULL.
**/
scond_t *scond_new(void)
{
scond_t *cond = (scond_t*)calloc(1, sizeof(*cond));
2015-10-03 06:04:28 +00:00
if (!cond)
return NULL;
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2018-01-05 12:22:00 +00:00
/* This is very complex because recreating condition variable semantics
2017-07-26 01:01:05 +00:00
* with Win32 parts is not easy.
*
2018-01-05 12:22:00 +00:00
* The main problem is that a condition variable can't be used to
2017-07-26 01:01:05 +00:00
* "pre-wake" a thread (it will get wakened only after it's waited).
*
2018-01-05 12:22:00 +00:00
* Whereas a win32 event can pre-wake a thread (the event will be set
* in advance, so a 'waiter' won't even have to wait on it).
2017-07-26 01:01:05 +00:00
*
2018-01-05 12:22:00 +00:00
* Keep in mind a condition variable can apparently pre-wake a thread,
2017-07-26 01:01:05 +00:00
* insofar as spurious wakeups are always possible,
* but nobody will be expecting this and it does not need to be simulated.
*
2018-01-05 12:22:00 +00:00
* Moreover, we won't be doing this, because it counts as a spurious wakeup
2017-07-26 01:01:05 +00:00
* -- someone else with a genuine claim must get wakened, in any case.
*
* Therefore we choose to wake only one of the correct waiting threads.
2018-01-05 12:22:00 +00:00
* So at the very least, we need to do something clever. But there's
2017-07-26 01:01:05 +00:00
* bigger problems.
2018-01-05 12:22:00 +00:00
* We don't even have a straightforward way in win32 to satisfy
* pthread_cond_wait's atomicity requirement. The bulk of this
2017-07-26 01:01:05 +00:00
* algorithm is solving that.
*
2018-01-05 12:22:00 +00:00
* Note: We might could simplify this using vista+ condition variables,
2017-07-26 01:01:05 +00:00
* but we wanted an XP compatible solution. */
2020-06-29 22:23:58 +00:00
cond->event = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!cond->event)
goto error;
cond->hot_potato = CreateEvent(NULL, FALSE, FALSE, NULL);
2017-07-26 01:01:05 +00:00
if (!cond->hot_potato)
{
CloseHandle(cond->event);
goto error;
}
2018-01-05 12:22:00 +00:00
2017-07-26 01:01:05 +00:00
InitializeCriticalSection(&cond->cs);
#else
if (pthread_cond_init(&cond->cond, NULL) != 0)
goto error;
2017-07-26 01:01:05 +00:00
#endif
2015-10-03 06:04:28 +00:00
return cond;
error:
free(cond);
return NULL;
2015-10-03 06:04:28 +00:00
}
/**
* scond_free:
2017-07-26 01:01:05 +00:00
* @cond : pointer to condition variable object
2015-10-03 06:04:28 +00:00
*
* Frees a condition variable.
**/
void scond_free(scond_t *cond)
{
if (!cond)
return;
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2015-10-03 06:04:28 +00:00
CloseHandle(cond->event);
2017-07-26 01:01:05 +00:00
CloseHandle(cond->hot_potato);
DeleteCriticalSection(&cond->cs);
2015-10-03 06:04:28 +00:00
#else
pthread_cond_destroy(&cond->cond);
#endif
free(cond);
}
2017-07-26 01:01:05 +00:00
#ifdef USE_WIN32_THREADS
static bool _scond_wait_win32(scond_t *cond, slock_t *lock, DWORD dwMilliseconds)
{
struct QueueEntry myentry;
struct QueueEntry **ptr;
2018-01-05 12:22:00 +00:00
#if _WIN32_WINNT >= 0x0500 || defined(_XBOX)
static LARGE_INTEGER performanceCounterFrequency;
LARGE_INTEGER tsBegin;
2018-01-05 12:22:00 +00:00
static bool first_init = true;
#else
static bool beginPeriod = false;
DWORD tsBegin;
#endif
2017-07-26 01:01:05 +00:00
DWORD waitResult;
2018-01-05 12:22:00 +00:00
DWORD dwFinalTimeout = dwMilliseconds; /* Careful! in case we begin in the head,
we don't do the hot potato stuff,
2017-07-26 01:01:05 +00:00
so this timeout needs presetting. */
/* Reminder: `lock` is held before this is called. */
/* however, someone else may have called scond_signal without the lock. soo... */
EnterCriticalSection(&cond->cs);
2018-01-05 12:22:00 +00:00
/* since this library is meant for realtime game software
2017-07-26 01:01:05 +00:00
* I have no problem setting this to 1 and forgetting about it. */
2018-01-05 12:22:00 +00:00
#if _WIN32_WINNT >= 0x0500 || defined(_XBOX)
if (first_init)
{
performanceCounterFrequency.QuadPart = 0;
first_init = false;
}
if (performanceCounterFrequency.QuadPart == 0)
2017-07-26 01:01:05 +00:00
{
QueryPerformanceFrequency(&performanceCounterFrequency);
2017-07-26 01:01:05 +00:00
}
#else
if (!beginPeriod)
{
beginPeriod = true;
timeBeginPeriod(1);
}
#endif
2017-07-26 01:01:05 +00:00
/* Now we can take a good timestamp for use in faking the timeout ourselves. */
/* But don't bother unless we need to (to save a little time) */
if (dwMilliseconds != INFINITE)
2018-01-05 12:22:00 +00:00
#if _WIN32_WINNT >= 0x0500 || defined(_XBOX)
QueryPerformanceCounter(&tsBegin);
#else
tsBegin = timeGetTime();
#endif
2017-07-26 01:01:05 +00:00
/* add ourselves to a queue of waiting threads */
ptr = &cond->head;
/* walk to the end of the linked list */
2018-01-05 12:22:00 +00:00
while (*ptr)
2017-07-26 01:01:05 +00:00
ptr = &((*ptr)->next);
*ptr = &myentry;
myentry.next = NULL;
cond->waiters++;
/* now the conceptual lock release and condition block are supposed to be atomic.
2018-01-05 12:22:00 +00:00
* we can't do that in Windows, but we can simulate the effects by using
2017-07-26 01:01:05 +00:00
* the queue, by the following analysis:
* What happens if they aren't atomic?
*
2018-01-05 12:22:00 +00:00
* 1. a signaller can rush in and signal, expecting a waiter to get it;
2017-07-26 01:01:05 +00:00
* but the waiter wouldn't, because he isn't blocked yet.
* Solution: Win32 events make this easy. The event will sit there enabled
*
* 2. a signaller can rush in and signal, and then turn right around and wait.
2018-01-05 12:22:00 +00:00
* Solution: the signaller will get queued behind the waiter, who's
2017-07-26 01:01:05 +00:00
* enqueued before he releases the mutex. */
2018-01-05 12:22:00 +00:00
/* It's my turn if I'm the head of the queue.
2017-07-26 01:01:05 +00:00
* Check to see if it's my turn. */
while (cond->head != &myentry)
{
/* It isn't my turn: */
DWORD timeout = INFINITE;
2018-01-05 12:22:00 +00:00
/* As long as someone is even going to be able to wake up
2017-07-26 01:01:05 +00:00
* when they receive the potato, keep it going round. */
if (cond->wakens > 0)
SetEvent(cond->hot_potato);
/* Assess the remaining timeout time */
if (dwMilliseconds != INFINITE)
{
2018-01-05 12:22:00 +00:00
#if _WIN32_WINNT >= 0x0500 || defined(_XBOX)
LARGE_INTEGER now;
2018-01-05 12:22:00 +00:00
LONGLONG elapsed;
QueryPerformanceCounter(&now);
2018-01-05 12:22:00 +00:00
elapsed = now.QuadPart - tsBegin.QuadPart;
elapsed *= 1000;
elapsed /= performanceCounterFrequency.QuadPart;
#else
2018-01-05 12:22:00 +00:00
DWORD now = timeGetTime();
DWORD elapsed = now - tsBegin;
#endif
2017-07-26 01:01:05 +00:00
/* Try one last time with a zero timeout (keeps the code simpler) */
if (elapsed > dwMilliseconds)
elapsed = dwMilliseconds;
timeout = dwMilliseconds - elapsed;
}
/* Let someone else go */
LeaveCriticalSection(&lock->lock);
LeaveCriticalSection(&cond->cs);
2018-01-05 12:22:00 +00:00
/* Wait a while to catch the hot potato..
2017-07-26 01:01:05 +00:00
* someone else should get a chance to go */
/* After all, it isn't my turn (and it must be someone else's) */
Sleep(0);
waitResult = WaitForSingleObject(cond->hot_potato, timeout);
/* I should come out of here with the main lock taken */
EnterCriticalSection(&lock->lock);
EnterCriticalSection(&cond->cs);
if (waitResult == WAIT_TIMEOUT)
{
/* Out of time! Now, let's think about this. I do have the potato now--
* maybe it's my turn, and I have the event?
2018-01-05 12:22:00 +00:00
* If that's the case, I could proceed right now without aborting
2017-07-26 01:01:05 +00:00
* due to timeout.
*
2018-01-05 12:22:00 +00:00
* However.. I DID wait a real long time. The caller was willing
2017-07-26 01:01:05 +00:00
* to wait that long.
*
2018-01-05 12:22:00 +00:00
* I choose to give him one last chance with a zero timeout
2017-07-26 01:01:05 +00:00
* in the next step
*/
if (cond->head == &myentry)
{
dwFinalTimeout = 0;
break;
}
else
{
/* It's not our turn and we're out of time. Give up.
* Remove ourself from the queue and bail. */
struct QueueEntry* curr = cond->head;
while (curr->next != &myentry)
curr = curr->next;
curr->next = myentry.next;
cond->waiters--;
LeaveCriticalSection(&cond->cs);
return false;
}
}
}
2018-01-05 12:22:00 +00:00
2017-07-26 01:01:05 +00:00
/* It's my turn now -- and I hold the potato */
/* I still have the main lock, in any case */
/* I need to release it so that someone can set the event */
LeaveCriticalSection(&lock->lock);
LeaveCriticalSection(&cond->cs);
/* Wait for someone to actually signal this condition */
2018-01-05 12:22:00 +00:00
/* We're the only waiter waiting on the event right now -- everyone else
2017-07-26 01:01:05 +00:00
* is waiting on something different */
waitResult = WaitForSingleObject(cond->event, dwFinalTimeout);
2018-01-05 12:22:00 +00:00
/* Take the main lock so we can do work. Nobody else waits on this lock
2017-07-26 01:01:05 +00:00
* for very long, so even though it's GO TIME we won't have to wait long */
EnterCriticalSection(&lock->lock);
EnterCriticalSection(&cond->cs);
/* Remove ourselves from the queue */
cond->head = myentry.next;
cond->waiters--;
if (waitResult == WAIT_TIMEOUT)
{
/* Oops! ran out of time in the final wait. Just bail. */
LeaveCriticalSection(&cond->cs);
return false;
}
/* If any other wakenings are pending, go ahead and set it up */
2018-01-05 12:22:00 +00:00
/* There may actually be no waiters. That's OK. The first waiter will come in,
2017-07-26 01:01:05 +00:00
* find it's his turn, and immediately get the signaled event */
cond->wakens--;
if (cond->wakens > 0)
{
SetEvent(cond->event);
2018-01-05 12:22:00 +00:00
/* Progress the queue: Put the hot potato back into play. It'll be
2017-07-26 01:01:05 +00:00
* tossed around until next in line gets it */
2018-01-05 12:22:00 +00:00
SetEvent(cond->hot_potato);
2017-07-26 01:01:05 +00:00
}
LeaveCriticalSection(&cond->cs);
return true;
}
#endif
2015-10-03 06:04:28 +00:00
/**
* scond_wait:
2017-07-26 01:01:05 +00:00
* @cond : pointer to condition variable object
* @lock : pointer to mutex object
2015-10-03 06:04:28 +00:00
*
2017-07-26 01:01:05 +00:00
* Block on a condition variable (i.e. wait on a condition).
2015-10-03 06:04:28 +00:00
**/
void scond_wait(scond_t *cond, slock_t *lock)
{
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2017-07-26 01:01:05 +00:00
_scond_wait_win32(cond, lock, INFINITE);
2015-10-03 06:04:28 +00:00
#else
pthread_cond_wait(&cond->cond, &lock->lock);
#endif
}
/**
* scond_broadcast:
2017-07-26 01:01:05 +00:00
* @cond : pointer to condition variable object
2015-10-03 06:04:28 +00:00
*
* Broadcast a condition. Unblocks all threads currently blocked
2017-07-26 01:01:05 +00:00
* on the specified condition variable @cond.
2015-10-03 06:04:28 +00:00
**/
int scond_broadcast(scond_t *cond)
{
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2017-07-26 01:01:05 +00:00
/* remember: we currently have mutex */
if (cond->waiters == 0)
return 0;
2018-01-05 12:22:00 +00:00
2017-07-26 01:01:05 +00:00
/* awaken everything which is currently queued up */
if (cond->wakens == 0)
SetEvent(cond->event);
cond->wakens = cond->waiters;
/* Since there is now at least one pending waken, the potato must be in play */
2018-01-05 12:22:00 +00:00
SetEvent(cond->hot_potato);
2017-07-26 01:01:05 +00:00
2015-10-03 06:04:28 +00:00
return 0;
#else
return pthread_cond_broadcast(&cond->cond);
#endif
}
/**
* scond_signal:
2017-07-26 01:01:05 +00:00
* @cond : pointer to condition variable object
2015-10-03 06:04:28 +00:00
*
* Signal a condition. Unblocks at least one of the threads currently blocked
2017-07-26 01:01:05 +00:00
* on the specified condition variable @cond.
2015-10-03 06:04:28 +00:00
**/
void scond_signal(scond_t *cond)
{
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2017-07-26 01:01:05 +00:00
2018-01-05 12:22:00 +00:00
/* Unfortunately, pthread_cond_signal does not require that the
2017-07-26 01:01:05 +00:00
* lock be held in advance */
2018-01-05 12:22:00 +00:00
/* To avoid stomping on the condvar from other threads, we need
2017-07-26 01:01:05 +00:00
* to control access to it with this */
EnterCriticalSection(&cond->cs);
/* remember: we currently have mutex */
if (cond->waiters == 0)
{
LeaveCriticalSection(&cond->cs);
return;
}
2018-01-05 12:22:00 +00:00
2017-07-26 01:01:05 +00:00
/* wake up the next thing in the queue */
if (cond->wakens == 0)
SetEvent(cond->event);
cond->wakens++;
/* The data structure is done being modified.. I think we can leave the CS now.
2018-01-05 12:22:00 +00:00
* This would prevent some other thread from receiving the hot potato and then
2017-07-26 01:01:05 +00:00
* immediately stalling for the critical section.
2018-01-05 12:22:00 +00:00
* But remember, we were trying to replicate a semantic where this entire
2017-07-26 01:01:05 +00:00
* scond_signal call was controlled (by the user) by a lock.
* So in case there's trouble with this, we can move it after SetEvent() */
LeaveCriticalSection(&cond->cs);
/* Since there is now at least one pending waken, the potato must be in play */
2018-01-05 12:22:00 +00:00
SetEvent(cond->hot_potato);
2017-07-26 01:01:05 +00:00
2015-10-03 06:04:28 +00:00
#else
pthread_cond_signal(&cond->cond);
#endif
}
/**
* scond_wait_timeout:
2017-07-26 01:01:05 +00:00
* @cond : pointer to condition variable object
* @lock : pointer to mutex object
2015-10-03 06:04:28 +00:00
* @timeout_us : timeout (in microseconds)
*
* Try to block on a condition variable (i.e. wait on a condition) until
* @timeout_us elapses.
*
* Returns: false (0) if timeout elapses before condition variable is
* signaled or broadcast, otherwise true (1).
**/
bool scond_wait_timeout(scond_t *cond, slock_t *lock, int64_t timeout_us)
{
2016-04-08 21:59:49 +00:00
#ifdef USE_WIN32_THREADS
2017-07-26 01:01:05 +00:00
/* How to convert a microsecond (us) timeout to millisecond (ms)?
*
* Someone asking for a 0 timeout clearly wants immediate timeout.
2018-01-05 12:22:00 +00:00
* Someone asking for a 1 timeout clearly wants an actual timeout
2017-07-26 01:01:05 +00:00
* of the minimum length */
2018-01-05 12:22:00 +00:00
/* Someone asking for 1000 or 1001 timeout shouldn't
2017-07-26 01:01:05 +00:00
* accidentally get 2ms. */
DWORD dwMilliseconds = timeout_us/1000;
/* The implementation of a 0 timeout here with pthreads is sketchy.
* It isn't clear what happens if pthread_cond_timedwait is called with NOW.
2018-01-05 12:22:00 +00:00
* Moreover, it is possible that this thread gets pre-empted after the
2017-07-26 01:01:05 +00:00
* clock_gettime but before the pthread_cond_timedwait.
2018-01-05 12:22:00 +00:00
* In order to help smoke out problems caused by this strange usage,
2017-07-26 01:01:05 +00:00
* let's treat a 0 timeout as always timing out.
*/
if (timeout_us == 0)
return false;
else if (timeout_us < 1000)
dwMilliseconds = 1;
return _scond_wait_win32(cond,lock,dwMilliseconds);
2015-10-03 06:04:28 +00:00
#else
int ret;
int64_t seconds, remainder;
struct timespec now = {0};
#ifdef __MACH__
/* OSX doesn't have clock_gettime. */
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
now.tv_sec = mts.tv_sec;
now.tv_nsec = mts.tv_nsec;
#elif defined(__CELLOS_LV2__)
sys_time_sec_t s;
sys_time_nsec_t n;
sys_time_get_current_time(&s, &n);
now.tv_sec = s;
now.tv_nsec = n;
2019-07-08 06:21:45 +00:00
#elif defined(PS2)
2020-06-29 22:23:58 +00:00
int tickms = ps2_clock();
2019-07-08 06:21:45 +00:00
now.tv_sec = tickms/1000;
now.tv_nsec = tickms * 1000;
2018-04-12 23:09:29 +00:00
#elif defined(__mips__) || defined(VITA) || defined(_3DS)
2015-10-03 06:04:28 +00:00
struct timeval tm;
gettimeofday(&tm, NULL);
now.tv_sec = tm.tv_sec;
now.tv_nsec = tm.tv_usec * 1000;
2016-04-08 21:59:49 +00:00
#elif defined(RETRO_WIN32_USE_PTHREADS)
_ftime64_s(&now);
2015-10-03 06:04:28 +00:00
#elif !defined(GEKKO)
/* timeout on libogc is duration, not end time. */
clock_gettime(CLOCK_REALTIME, &now);
#endif
seconds = timeout_us / INT64_C(1000000);
remainder = timeout_us % INT64_C(1000000);
now.tv_sec += seconds;
now.tv_nsec += remainder * INT64_C(1000);
2018-01-05 12:22:00 +00:00
if (now.tv_nsec > 1000000000)
{
2018-01-05 12:22:00 +00:00
now.tv_nsec -= 1000000000;
now.tv_sec += 1;
}
2015-10-03 06:04:28 +00:00
ret = pthread_cond_timedwait(&cond->cond, &lock->lock, &now);
return (ret == 0);
#endif
}
2017-07-26 01:01:05 +00:00
#ifdef HAVE_THREAD_STORAGE
bool sthread_tls_create(sthread_tls_t *tls)
{
#ifdef USE_WIN32_THREADS
return (*tls = TlsAlloc()) != TLS_OUT_OF_INDEXES;
#else
return pthread_key_create((pthread_key_t*)tls, NULL) == 0;
#endif
}
bool sthread_tls_delete(sthread_tls_t *tls)
{
#ifdef USE_WIN32_THREADS
return TlsFree(*tls) != 0;
#else
return pthread_key_delete(*tls) == 0;
#endif
}
void *sthread_tls_get(sthread_tls_t *tls)
{
#ifdef USE_WIN32_THREADS
return TlsGetValue(*tls);
#else
return pthread_getspecific(*tls);
#endif
}
bool sthread_tls_set(sthread_tls_t *tls, const void *data)
{
#ifdef USE_WIN32_THREADS
return TlsSetValue(*tls, (void*)data) != 0;
#else
return pthread_setspecific(*tls, data) == 0;
#endif
}
#endif
2020-06-29 22:23:58 +00:00
uintptr_t sthread_get_thread_id(sthread_t *thread)
{
if (!thread)
return 0;
return (uintptr_t)thread->id;
}
uintptr_t sthread_get_current_thread_id(void)
{
#ifdef USE_WIN32_THREADS
return (uintptr_t)GetCurrentThreadId();
#else
return (uintptr_t)pthread_self();
#endif
}