libretro-common/rthreads/psp_pthread.h

306 lines
7.2 KiB
C
Raw Normal View History

2017-01-22 12:58:48 +00:00
/* Copyright (C) 2010-2017 The RetroArch team
2014-10-21 01:33:20 +00:00
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (psp_pthread.h).
* ---------------------------------------------------------------------------------------
*
* 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.
*/
2015-08-29 16:40:10 +00:00
/* FIXME: unfinished on PSP, mutexes and condition variables basically a stub. */
2014-10-21 01:33:20 +00:00
#ifndef _PSP_PTHREAD_WRAP__
#define _PSP_PTHREAD_WRAP__
2015-08-29 16:40:10 +00:00
#ifdef VITA
#include <psp2/kernel/threadmgr.h>
2016-08-26 01:54:18 +00:00
#include <sys/time.h>
2015-08-29 16:40:10 +00:00
#else
2014-10-21 01:33:20 +00:00
#include <pspkernel.h>
#include <pspthreadman.h>
#include <pspthreadman_kernel.h>
2015-08-29 16:40:10 +00:00
#endif
2014-10-21 01:33:20 +00:00
#include <stdio.h>
2015-04-22 16:51:53 +00:00
#include <retro_inline.h>
2014-10-21 01:33:20 +00:00
2016-08-19 07:31:56 +00:00
#define STACKSIZE (8 * 1024)
2014-10-21 01:33:20 +00:00
typedef SceUID pthread_t;
typedef SceUID pthread_mutex_t;
typedef void* pthread_mutexattr_t;
typedef int pthread_attr_t;
2016-08-19 07:31:56 +00:00
typedef struct
{
SceUID mutex;
SceUID sema;
int waiting;
} pthread_cond_t;
2014-10-21 01:33:20 +00:00
typedef SceUID pthread_condattr_t;
/* Use pointer values to create unique names for threads/mutexes */
char name_buffer[256];
typedef void* (*sthreadEntry)(void *argp);
typedef struct
{
void* arg;
sthreadEntry start_routine;
} sthread_args_struct;
static int psp_thread_wrap(SceSize args, void *argp)
{
sthread_args_struct* sthread_args = (sthread_args_struct*)argp;
return (int)sthread_args->start_routine(sthread_args->arg);
}
2015-04-22 16:51:53 +00:00
static INLINE int pthread_create(pthread_t *thread,
2014-10-21 01:33:20 +00:00
const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
{
2016-09-25 03:19:06 +00:00
snprintf(name_buffer, sizeof(name_buffer), "0x%08X", (unsigned int) thread);
2014-10-21 01:33:20 +00:00
2016-08-19 07:31:56 +00:00
#ifdef VITA
*thread = sceKernelCreateThread(name_buffer, psp_thread_wrap,
0x10000100, 0x10000, 0, 0, NULL);
#else
2014-10-21 01:33:20 +00:00
*thread = sceKernelCreateThread(name_buffer,
psp_thread_wrap, 0x20, STACKSIZE, 0, NULL);
2016-08-19 07:31:56 +00:00
#endif
2014-10-21 01:33:20 +00:00
sthread_args_struct sthread_args;
sthread_args.arg = arg;
sthread_args.start_routine = start_routine;
return sceKernelStartThread(*thread, sizeof(sthread_args), &sthread_args);
}
2015-04-22 16:51:53 +00:00
static INLINE int pthread_mutex_init(pthread_mutex_t *mutex,
2014-10-21 01:33:20 +00:00
const pthread_mutexattr_t *attr)
{
2016-09-25 03:19:06 +00:00
snprintf(name_buffer, sizeof(name_buffer), "0x%08X", (unsigned int) mutex);
2014-10-21 01:33:20 +00:00
2015-08-29 16:40:10 +00:00
#ifdef VITA
2016-08-19 07:31:56 +00:00
*mutex = sceKernelCreateMutex(name_buffer, 0, 0, 0);
if(*mutex<0)
return *mutex;
return 0;
2015-08-29 16:40:10 +00:00
#else
2014-10-21 01:33:20 +00:00
return *mutex = sceKernelCreateSema(name_buffer, 0, 1, 1, NULL);
2015-08-29 16:40:10 +00:00
#endif
2014-10-21 01:33:20 +00:00
}
2015-04-22 16:51:53 +00:00
static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
2014-10-21 01:33:20 +00:00
{
2015-08-29 16:40:10 +00:00
#ifdef VITA
return sceKernelDeleteMutex(*mutex);
#else
2014-10-21 01:33:20 +00:00
return sceKernelDeleteSema(*mutex);
2015-08-29 16:40:10 +00:00
#endif
2014-10-21 01:33:20 +00:00
}
2015-04-22 16:51:53 +00:00
static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
2014-10-21 01:33:20 +00:00
{
2015-08-29 16:40:10 +00:00
#ifdef VITA
2016-08-19 07:31:56 +00:00
int ret = sceKernelLockMutex(*mutex, 1, 0);
return ret;
2015-08-29 16:40:10 +00:00
#else
/* FIXME: stub */
2014-10-21 01:33:20 +00:00
return 1;
2015-08-29 16:40:10 +00:00
#endif
2014-10-21 01:33:20 +00:00
}
2015-04-22 16:51:53 +00:00
static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
2014-10-21 01:33:20 +00:00
{
2015-08-29 16:40:10 +00:00
#ifdef VITA
2016-08-19 07:31:56 +00:00
int ret = sceKernelUnlockMutex(*mutex, 1);
return ret;
2015-08-29 16:40:10 +00:00
#else
/* FIXME: stub */
2014-10-21 01:33:20 +00:00
return 1;
2015-08-29 16:40:10 +00:00
#endif
2014-10-21 01:33:20 +00:00
}
2015-04-22 16:51:53 +00:00
static INLINE int pthread_join(pthread_t thread, void **retval)
2014-10-21 01:33:20 +00:00
{
2016-08-19 07:31:56 +00:00
#ifdef VITA
2016-08-29 19:26:29 +00:00
int res = sceKernelWaitThreadEnd(thread, 0, 0);
if (res < 0)
return res;
return sceKernelDeleteThread(thread);
2016-08-19 07:31:56 +00:00
#else
2016-08-29 19:26:29 +00:00
SceUInt timeout = (SceUInt)-1;
2014-10-21 01:33:20 +00:00
sceKernelWaitThreadEnd(thread, &timeout);
2015-09-01 09:43:17 +00:00
exit_status = sceKernelGetThreadExitStatus(thread);
2014-10-21 01:33:20 +00:00
sceKernelDeleteThread(thread);
return exit_status;
2016-08-19 07:31:56 +00:00
#endif
2014-10-21 01:33:20 +00:00
}
2015-04-22 16:51:53 +00:00
static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
2014-10-21 01:33:20 +00:00
{
2015-09-01 09:43:17 +00:00
#ifdef VITA
return sceKernelTryLockMutex(*mutex, 1 /* not sure about this last param */);
#else
/* FIXME: stub */
2014-10-21 01:33:20 +00:00
return 1;
2015-09-01 09:43:17 +00:00
#endif
2014-10-21 01:33:20 +00:00
}
2015-04-22 16:51:53 +00:00
static INLINE int pthread_cond_wait(pthread_cond_t *cond,
2014-10-21 01:33:20 +00:00
pthread_mutex_t *mutex)
{
2016-08-19 07:31:56 +00:00
#ifdef VITA
2016-08-29 19:26:29 +00:00
int ret = pthread_mutex_lock(&cond->mutex);
if (ret < 0)
return ret;
++cond->waiting;
pthread_mutex_unlock(mutex);
pthread_mutex_unlock(&cond->mutex);
ret = sceKernelWaitSema(cond->sema, 1, 0);
if (ret < 0)
sceClibPrintf("Premature wakeup: %08X", ret);
pthread_mutex_lock(mutex);
return ret;
2016-08-19 07:31:56 +00:00
#else
/* FIXME: stub */
2014-10-21 01:33:20 +00:00
sceKernelDelayThread(10000);
return 1;
2016-08-19 07:31:56 +00:00
#endif
2014-10-21 01:33:20 +00:00
}
2015-04-22 16:51:53 +00:00
static INLINE int pthread_cond_timedwait(pthread_cond_t *cond,
2014-10-21 01:33:20 +00:00
pthread_mutex_t *mutex, const struct timespec *abstime)
{
2016-08-19 07:31:56 +00:00
#ifdef VITA
2016-08-29 19:26:29 +00:00
int ret = pthread_mutex_lock(&cond->mutex);
if (ret < 0)
return ret;
++cond->waiting;
pthread_mutex_unlock(mutex);
pthread_mutex_unlock(&cond->mutex);
SceUInt timeout = 0;
timeout = abstime->tv_sec;
timeout += abstime->tv_nsec / 1.0e6;
ret = sceKernelWaitSema(cond->sema, 1, &timeout);
if (ret < 0)
sceClibPrintf("Premature wakeup: %08X", ret);
pthread_mutex_lock(mutex);
return ret;
2016-08-19 07:31:56 +00:00
#else
/* FIXME: stub */
2014-10-21 01:33:20 +00:00
return 1;
2016-08-19 07:31:56 +00:00
#endif
2014-10-21 01:33:20 +00:00
}
2015-04-22 16:51:53 +00:00
static INLINE int pthread_cond_init(pthread_cond_t *cond,
2014-10-21 01:33:20 +00:00
const pthread_condattr_t *attr)
{
2016-08-19 07:31:56 +00:00
#ifdef VITA
2016-09-06 21:21:29 +00:00
2016-09-25 03:19:06 +00:00
pthread_mutex_init(&cond->mutex,NULL);
2016-09-06 21:21:29 +00:00
2016-09-25 03:19:06 +00:00
if(cond->mutex<0)
return cond->mutex;
2016-08-19 07:31:56 +00:00
2016-09-25 03:19:06 +00:00
snprintf(name_buffer, sizeof(name_buffer), "0x%08X", (unsigned int) cond);
cond->sema = sceKernelCreateSema(name_buffer, 0, 0, 1, 0);
2016-09-06 21:21:29 +00:00
2016-09-25 03:19:06 +00:00
if(cond->sema < 0)
{
pthread_mutex_destroy(&cond->mutex);
return cond->sema;
}
cond->waiting = 0;
return 0;
2016-09-06 21:21:29 +00:00
2016-08-19 07:31:56 +00:00
#else
/* FIXME: stub */
2014-10-21 01:33:20 +00:00
return 1;
2016-08-19 07:31:56 +00:00
#endif
2014-10-21 01:33:20 +00:00
}
2015-04-22 16:51:53 +00:00
static INLINE int pthread_cond_signal(pthread_cond_t *cond)
2014-10-21 01:33:20 +00:00
{
2016-08-19 07:31:56 +00:00
#ifdef VITA
pthread_mutex_lock(&cond->mutex);
2016-08-29 19:26:29 +00:00
if (cond->waiting)
{
2016-08-19 07:31:56 +00:00
--cond->waiting;
2016-09-11 09:54:34 +00:00
sceKernelSignalSema(cond->sema, 1);
2016-08-19 07:31:56 +00:00
}
pthread_mutex_unlock(&cond->mutex);
return 0;
#else
/* FIXME: stub */
2014-10-21 01:33:20 +00:00
return 1;
2016-08-19 07:31:56 +00:00
#endif
2014-10-21 01:33:20 +00:00
}
2015-04-22 16:51:53 +00:00
static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
2014-10-21 01:33:20 +00:00
{
2016-08-19 07:31:56 +00:00
/* FIXME: stub */
2014-10-21 01:33:20 +00:00
return 1;
}
2015-04-22 16:51:53 +00:00
static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
2014-10-21 01:33:20 +00:00
{
2016-08-19 07:31:56 +00:00
#ifdef VITA
int ret = sceKernelDeleteSema(cond->sema);
if(ret < 0)
return ret;
return sceKernelDeleteMutex(cond->mutex);
#else
/* FIXME: stub */
return 1;
#endif
2014-10-21 01:33:20 +00:00
}
2015-04-22 16:51:53 +00:00
static INLINE int pthread_detach(pthread_t thread)
2014-10-21 01:33:20 +00:00
{
2016-08-19 07:31:56 +00:00
return 0;
2014-10-21 01:33:20 +00:00
}
2015-04-22 16:51:53 +00:00
static INLINE void pthread_exit(void *retval)
2014-10-21 01:33:20 +00:00
{
2016-08-19 07:31:56 +00:00
#ifdef VITA
sceKernelExitDeleteThread(sceKernelGetThreadId());
#endif
2014-10-21 01:33:20 +00:00
}
2016-03-20 11:00:03 +00:00
static INLINE pthread_t pthread_self(void)
{
/* zero 20-mar-2016: untested */
return sceKernelGetThreadId();
}
2016-08-19 07:31:56 +00:00
static INLINE int pthread_equal(pthread_t t1, pthread_t t2)
{
return t1 == t2;
}
2016-03-20 11:00:03 +00:00
2014-10-21 01:33:20 +00:00
#endif //_PSP_PTHREAD_WRAP__