Skip to content

Commit

Permalink
Merge pull request #3405 from frangarcj/master
Browse files Browse the repository at this point in the history
(VITA) Add threads support
  • Loading branch information
inactive123 committed Aug 19, 2016
2 parents bdd26ba + 6e920f1 commit 4b9ced1
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Makefile.griffin
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ else ifeq ($(platform), vita)
WANT_ZLIB := 1
INCLUDE += -I$(VITASDK)/arm-vita-eabi/include/freetype2 -Ideps/libvita2d/include
INCLUDE += -Ideps/zlib
PLATCFLAGS := -O3 -mfloat-abi=hard -ffast-math -fsingle-precision-constant
PLATCFLAGS := -mthumb -mfloat-abi=hard -ffast-math -fsingle-precision-constant -mword-relocations
LIBS += -lSceKernel_stub -lSceDisplay_stub -lSceGxm_stub -lSceNet_stub -lSceNetCtl_stub\
-lSceSysmodule_stub -lSceCtrl_stub -lSceAudio_stub \
-lScePower_stub -lSceRtc_stub -lSceCommonDialog_stub -lScePgf_stub \
Expand All @@ -239,14 +239,14 @@ else ifeq ($(platform), vita)
HAVE_RJPEG := 1
HAVE_RBMP := 1
HAVE_RTGA := 1
HAVE_IMAGEVIEWER := 1
HAVE_ZLIB := 1
HAVE_7ZIP := 1
HAVE_VITA2D := 1
HAVE_NETWORKING := 1
HAVE_NETPLAY := 1
HAVE_OVERLAY := 1
RARCH_CONSOLE = 1
HAVE_THREADS := 1
endif


Expand Down
147 changes: 132 additions & 15 deletions libretro-common/rthreads/psp_pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@
#include <stdio.h>
#include <retro_inline.h>

#define STACKSIZE (64 * 1024)
#define STACKSIZE (8 * 1024)

typedef SceUID pthread_t;
typedef SceUID pthread_mutex_t;
typedef void* pthread_mutexattr_t;
typedef int pthread_attr_t;
typedef SceUID pthread_cond_t;

typedef struct
{
SceUID mutex;
SceUID sema;
int waiting;
} pthread_cond_t;

typedef SceUID pthread_condattr_t;

/* Use pointer values to create unique names for threads/mutexes */
Expand All @@ -67,8 +74,13 @@ static INLINE int pthread_create(pthread_t *thread,
{
sprintf(name_buffer, "0x%08X", (uint32_t) thread);

#ifdef VITA
*thread = sceKernelCreateThread(name_buffer, psp_thread_wrap,
0x10000100, 0x10000, 0, 0, NULL);
#else
*thread = sceKernelCreateThread(name_buffer,
psp_thread_wrap, 0x20, STACKSIZE, 0, NULL);
#endif

sthread_args_struct sthread_args;
sthread_args.arg = arg;
Expand All @@ -83,7 +95,10 @@ static INLINE int pthread_mutex_init(pthread_mutex_t *mutex,
sprintf(name_buffer, "0x%08X", (uint32_t) mutex);

#ifdef VITA
return *mutex = sceKernelCreateMutex(name_buffer, 0, 0, 0);
*mutex = sceKernelCreateMutex(name_buffer, 0, 0, 0);
if(*mutex<0)
return *mutex;
return 0;
#else
return *mutex = sceKernelCreateSema(name_buffer, 0, 1, 1, NULL);
#endif
Expand All @@ -101,7 +116,10 @@ static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
{
#ifdef VITA
return sceKernelLockMutex(*mutex, 1, 0);
int ret = sceKernelLockMutex(*mutex, 1, 0);
//sceClibPrintf("pthread_mutex_lock: %x\n",ret);
return ret;

#else
/* FIXME: stub */
return 1;
Expand All @@ -111,7 +129,9 @@ static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
#ifdef VITA
return sceKernelUnlockMutex(*mutex, 1);
int ret = sceKernelUnlockMutex(*mutex, 1);
//sceClibPrintf("pthread_mutex_unlock: %x\n",ret);
return ret;
#else
/* FIXME: stub */
return 1;
Expand All @@ -121,13 +141,20 @@ static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)

static INLINE int pthread_join(pthread_t thread, void **retval)
{
int exit_status;
SceUInt timeout = (SceUInt)-1;

#ifdef VITA
int res = sceKernelWaitThreadEnd(thread, 0, 0);
if (res < 0) {
return res;
}
return sceKernelDeleteThread(thread);
#else
SceUInt timeout = (SceUInt)-1;
sceKernelWaitThreadEnd(thread, &timeout);
exit_status = sceKernelGetThreadExitStatus(thread);
sceKernelDeleteThread(thread);
return exit_status;
#endif
}

static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
Expand All @@ -143,51 +170,137 @@ static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
static INLINE int pthread_cond_wait(pthread_cond_t *cond,
pthread_mutex_t *mutex)
{
#ifdef VITA
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;
#else
/* FIXME: stub */
sceKernelDelayThread(10000);
return 1;
#endif
}

static INLINE int pthread_cond_timedwait(pthread_cond_t *cond,
pthread_mutex_t *mutex, const struct timespec *abstime)
{
//FIXME: stub
#ifdef VITA
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;

#else
/* FIXME: stub */
return 1;
#endif
}

static INLINE int pthread_cond_init(pthread_cond_t *cond,
const pthread_condattr_t *attr)
{
//FIXME: stub
#ifdef VITA

pthread_mutex_init(&cond->mutex,NULL);
sceClibPrintf("pthread_cond_init: mutex %x\n",cond->mutex);
if(cond->mutex<0){
return cond->mutex;
}
sprintf(name_buffer, "0x%08X", (uint32_t) cond);
//cond->sema = sceKernelCreateCond(name_buffer, 0, cond->mutex, 0);
cond->sema = sceKernelCreateSema(name_buffer, 0, 0, 1, 0);
sceClibPrintf("pthread_cond_init: sema %x\n",cond->sema);
if(cond->sema<0){
pthread_mutex_destroy(&cond->mutex);
return cond->sema;
}

cond->waiting = 0;


return 0;


#else
/* FIXME: stub */
return 1;
#endif
}

static INLINE int pthread_cond_signal(pthread_cond_t *cond)
{
//FIXME: stub
#ifdef VITA
pthread_mutex_lock(&cond->mutex);
if (cond->waiting) {
--cond->waiting;
int ret = sceKernelSignalSema(cond->sema, 1);
sceClibPrintf("pthread_cond_signal: %x\n",ret);
}
pthread_mutex_unlock(&cond->mutex);
return 0;
#else
/* FIXME: stub */
return 1;
#endif
}

static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
{
//FIXME: stub
/* FIXME: stub */
return 1;
}

static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
{
//FIXME: stub
return 1;
#ifdef VITA
int ret = sceKernelDeleteSema(cond->sema);
if(ret < 0)
return ret;

return sceKernelDeleteMutex(cond->mutex);
#else
/* FIXME: stub */
return 1;
#endif
}


static INLINE int pthread_detach(pthread_t thread)
{
return 1;
return 0;
}

static INLINE void pthread_exit(void *retval)
{
(void)retval;
#ifdef VITA
sceKernelExitDeleteThread(sceKernelGetThreadId());
#endif
}

static INLINE pthread_t pthread_self(void)
Expand All @@ -196,5 +309,9 @@ static INLINE pthread_t pthread_self(void)
return sceKernelGetThreadId();
}

static INLINE int pthread_equal(pthread_t t1, pthread_t t2)
{
return t1 == t2;
}

#endif //_PSP_PTHREAD_WRAP__
7 changes: 5 additions & 2 deletions libretro-common/rthreads/rthreads.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#endif
#elif defined(GEKKO)
#include "gx_pthread.h"
#elif defined(PSP)
#elif defined(PSP) || defined(VITA)
#include "psp_pthread.h"
#elif defined(__CELLOS_LV2__)
#include <pthread.h>
Expand All @@ -51,6 +51,9 @@
#include <time.h>
#endif

#if defined(VITA)
#include <sys/time.h>
#endif

#ifdef __MACH__
#include <mach/clock.h>
Expand Down Expand Up @@ -446,7 +449,7 @@ bool scond_wait_timeout(scond_t *cond, slock_t *lock, int64_t timeout_us)
sys_time_get_current_time(&s, &n);
now.tv_sec = s;
now.tv_nsec = n;
#elif defined(__mips__)
#elif defined(__mips__) || defined(VITA)
struct timeval tm;

gettimeofday(&tm, NULL);
Expand Down

0 comments on commit 4b9ced1

Please sign in to comment.