mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2025-08-16 16:35:47 +00:00
Fixes for Moonlight compatibility on the PS Vita. (#110)
* Use pthread for vita thread managment * Add thread name to pthread and remove ipv6 from vita * Fix inclusion of pthread.h so that it is only included on specific platforms (__vita__) * Fix memory release in ThreadProc for psvita platform --------- Co-authored-by: MetalfaceScout <logan.mathis@utahtech.edu> Co-authored-by: Logan Mathis <33127568+MetalfaceScout@users.noreply.github.com>
This commit is contained in:
parent
262537959f
commit
34074c9ec0
@ -1,5 +1,8 @@
|
|||||||
#define _GNU_SOURCE
|
#define _GNU_SOURCE
|
||||||
#include "Limelight-internal.h"
|
#include "Limelight-internal.h"
|
||||||
|
#if defined(__vita__)
|
||||||
|
#include <pthread.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
// The maximum amount of time before observing an interrupt
|
// The maximum amount of time before observing an interrupt
|
||||||
// in PltSleepMsInterruptible().
|
// in PltSleepMsInterruptible().
|
||||||
@ -9,9 +12,6 @@ struct thread_context {
|
|||||||
ThreadEntry entry;
|
ThreadEntry entry;
|
||||||
void* context;
|
void* context;
|
||||||
const char* name;
|
const char* name;
|
||||||
#if defined(__vita__)
|
|
||||||
PLT_THREAD* thread;
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static int activeThreads = 0;
|
static int activeThreads = 0;
|
||||||
@ -67,9 +67,6 @@ void setThreadNameWin32(const char* name) {
|
|||||||
|
|
||||||
DWORD WINAPI ThreadProc(LPVOID lpParameter) {
|
DWORD WINAPI ThreadProc(LPVOID lpParameter) {
|
||||||
struct thread_context* ctx = (struct thread_context*)lpParameter;
|
struct thread_context* ctx = (struct thread_context*)lpParameter;
|
||||||
#elif defined(__vita__)
|
|
||||||
int ThreadProc(SceSize args, void *argp) {
|
|
||||||
struct thread_context* ctx = (struct thread_context*)argp;
|
|
||||||
#elif defined(__WIIU__)
|
#elif defined(__WIIU__)
|
||||||
int ThreadProc(int argc, const char** argv) {
|
int ThreadProc(int argc, const char** argv) {
|
||||||
struct thread_context* ctx = (struct thread_context*)argv;
|
struct thread_context* ctx = (struct thread_context*)argv;
|
||||||
@ -89,15 +86,7 @@ void* ThreadProc(void* context) {
|
|||||||
ctx->entry(ctx->context);
|
ctx->entry(ctx->context);
|
||||||
|
|
||||||
#if defined(__vita__)
|
#if defined(__vita__)
|
||||||
if (ctx->thread->detached) {
|
free(ctx);
|
||||||
free(ctx);
|
|
||||||
sceKernelExitDeleteThread(0);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
free(ctx);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
free(ctx);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(LC_WINDOWS) || defined(__vita__) || defined(__WIIU__) || defined(__3DS__)
|
#if defined(LC_WINDOWS) || defined(__vita__) || defined(__WIIU__) || defined(__3DS__)
|
||||||
@ -110,8 +99,6 @@ void* ThreadProc(void* context) {
|
|||||||
void PltSleepMs(int ms) {
|
void PltSleepMs(int ms) {
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
SleepEx(ms, FALSE);
|
SleepEx(ms, FALSE);
|
||||||
#elif defined(__vita__)
|
|
||||||
sceKernelDelayThread(ms * 1000);
|
|
||||||
#elif defined(__3DS__)
|
#elif defined(__3DS__)
|
||||||
s64 nsecs = ms * 1000000;
|
s64 nsecs = ms * 1000000;
|
||||||
svcSleepThread(nsecs);
|
svcSleepThread(nsecs);
|
||||||
@ -132,11 +119,6 @@ void PltSleepMsInterruptible(PLT_THREAD* thread, int ms) {
|
|||||||
int PltCreateMutex(PLT_MUTEX* mutex) {
|
int PltCreateMutex(PLT_MUTEX* mutex) {
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
InitializeSRWLock(mutex);
|
InitializeSRWLock(mutex);
|
||||||
#elif defined(__vita__)
|
|
||||||
*mutex = sceKernelCreateMutex("", 0, 0, NULL);
|
|
||||||
if (*mutex < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
#elif defined(__WIIU__)
|
#elif defined(__WIIU__)
|
||||||
OSFastMutex_Init(mutex, "");
|
OSFastMutex_Init(mutex, "");
|
||||||
#elif defined(__3DS__)
|
#elif defined(__3DS__)
|
||||||
@ -156,8 +138,6 @@ void PltDeleteMutex(PLT_MUTEX* mutex) {
|
|||||||
activeMutexes--;
|
activeMutexes--;
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
// No-op to destroy a SRWLOCK
|
// No-op to destroy a SRWLOCK
|
||||||
#elif defined(__vita__)
|
|
||||||
sceKernelDeleteMutex(*mutex);
|
|
||||||
#elif defined(__WIIU__) || defined(__3DS__)
|
#elif defined(__WIIU__) || defined(__3DS__)
|
||||||
|
|
||||||
#else
|
#else
|
||||||
@ -168,8 +148,6 @@ void PltDeleteMutex(PLT_MUTEX* mutex) {
|
|||||||
void PltLockMutex(PLT_MUTEX* mutex) {
|
void PltLockMutex(PLT_MUTEX* mutex) {
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
AcquireSRWLockExclusive(mutex);
|
AcquireSRWLockExclusive(mutex);
|
||||||
#elif defined(__vita__)
|
|
||||||
sceKernelLockMutex(*mutex, 1, NULL);
|
|
||||||
#elif defined(__WIIU__)
|
#elif defined(__WIIU__)
|
||||||
OSFastMutex_Lock(mutex);
|
OSFastMutex_Lock(mutex);
|
||||||
#elif defined(__3DS__)
|
#elif defined(__3DS__)
|
||||||
@ -182,8 +160,6 @@ void PltLockMutex(PLT_MUTEX* mutex) {
|
|||||||
void PltUnlockMutex(PLT_MUTEX* mutex) {
|
void PltUnlockMutex(PLT_MUTEX* mutex) {
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
ReleaseSRWLockExclusive(mutex);
|
ReleaseSRWLockExclusive(mutex);
|
||||||
#elif defined(__vita__)
|
|
||||||
sceKernelUnlockMutex(*mutex, 1);
|
|
||||||
#elif defined(__WIIU__)
|
#elif defined(__WIIU__)
|
||||||
OSFastMutex_Unlock(mutex);
|
OSFastMutex_Unlock(mutex);
|
||||||
#elif defined(__3DS__)
|
#elif defined(__3DS__)
|
||||||
@ -200,10 +176,6 @@ void PltJoinThread(PLT_THREAD* thread) {
|
|||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
WaitForSingleObjectEx(thread->handle, INFINITE, FALSE);
|
WaitForSingleObjectEx(thread->handle, INFINITE, FALSE);
|
||||||
CloseHandle(thread->handle);
|
CloseHandle(thread->handle);
|
||||||
#elif defined(__vita__)
|
|
||||||
LC_ASSERT(!thread->detached);
|
|
||||||
sceKernelWaitThreadEnd(thread->handle, NULL, NULL);
|
|
||||||
sceKernelDeleteThread(thread->handle);
|
|
||||||
#elif defined(__WIIU__)
|
#elif defined(__WIIU__)
|
||||||
OSJoinThread(&thread->thread, NULL);
|
OSJoinThread(&thread->thread, NULL);
|
||||||
#elif defined(__3DS__)
|
#elif defined(__3DS__)
|
||||||
@ -222,9 +194,6 @@ void PltDetachThread(PLT_THREAD* thread) {
|
|||||||
// According MSDN:
|
// According MSDN:
|
||||||
// "Closing a thread handle does not terminate the associated thread or remove the thread object."
|
// "Closing a thread handle does not terminate the associated thread or remove the thread object."
|
||||||
CloseHandle(thread->handle);
|
CloseHandle(thread->handle);
|
||||||
#elif defined(__vita__)
|
|
||||||
LC_ASSERT(!thread->detached);
|
|
||||||
thread->detached = true;
|
|
||||||
#elif defined(__WIIU__)
|
#elif defined(__WIIU__)
|
||||||
OSDetachThread(&thread->thread);
|
OSDetachThread(&thread->thread);
|
||||||
#elif defined(__3DS__)
|
#elif defined(__3DS__)
|
||||||
@ -270,18 +239,6 @@ int PltCreateThread(const char* name, ThreadEntry entry, void* context, PLT_THRE
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#elif defined(__vita__)
|
|
||||||
{
|
|
||||||
thread->detached = false;
|
|
||||||
thread->context = ctx;
|
|
||||||
ctx->thread = thread;
|
|
||||||
thread->handle = sceKernelCreateThread(name, ThreadProc, 0, 0x40000, 0, 0, NULL);
|
|
||||||
if (thread->handle < 0) {
|
|
||||||
free(ctx);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
sceKernelStartThread(thread->handle, sizeof(struct thread_context), ctx);
|
|
||||||
}
|
|
||||||
#elif defined(__WIIU__)
|
#elif defined(__WIIU__)
|
||||||
memset(&thread->thread, 0, sizeof(thread->thread));
|
memset(&thread->thread, 0, sizeof(thread->thread));
|
||||||
|
|
||||||
@ -326,11 +283,20 @@ int PltCreateThread(const char* name, ThreadEntry entry, void* context, PLT_THRE
|
|||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
{
|
{
|
||||||
int err = pthread_create(&thread->thread, NULL, ThreadProc, ctx);
|
pthread_attr_t attr;
|
||||||
|
|
||||||
|
pthread_attr_init(&attr);
|
||||||
|
|
||||||
|
pthread_attr_setstacksize(&attr, 0x100000);
|
||||||
|
|
||||||
|
ctx->name = name;
|
||||||
|
|
||||||
|
int err = pthread_create(&thread->thread, &attr, ThreadProc, ctx);
|
||||||
if (err != 0) {
|
if (err != 0) {
|
||||||
free(ctx);
|
free(ctx);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -404,11 +370,6 @@ void PltWaitForEvent(PLT_EVENT* event) {
|
|||||||
int PltCreateConditionVariable(PLT_COND* cond, PLT_MUTEX* mutex) {
|
int PltCreateConditionVariable(PLT_COND* cond, PLT_MUTEX* mutex) {
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
InitializeConditionVariable(cond);
|
InitializeConditionVariable(cond);
|
||||||
#elif defined(__vita__)
|
|
||||||
*cond = sceKernelCreateCond("", 0, *mutex, NULL);
|
|
||||||
if (*cond < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
#elif defined(__WIIU__)
|
#elif defined(__WIIU__)
|
||||||
OSFastCond_Init(cond, "");
|
OSFastCond_Init(cond, "");
|
||||||
#elif defined(__3DS__)
|
#elif defined(__3DS__)
|
||||||
@ -425,8 +386,6 @@ void PltDeleteConditionVariable(PLT_COND* cond) {
|
|||||||
activeCondVars--;
|
activeCondVars--;
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
// No-op to delete a CONDITION_VARIABLE
|
// No-op to delete a CONDITION_VARIABLE
|
||||||
#elif defined(__vita__)
|
|
||||||
sceKernelDeleteCond(*cond);
|
|
||||||
#elif defined(__WIIU__)
|
#elif defined(__WIIU__)
|
||||||
// No-op to delete an OSFastCondition
|
// No-op to delete an OSFastCondition
|
||||||
#elif defined(__3DS__)
|
#elif defined(__3DS__)
|
||||||
@ -439,8 +398,6 @@ void PltDeleteConditionVariable(PLT_COND* cond) {
|
|||||||
void PltSignalConditionVariable(PLT_COND* cond) {
|
void PltSignalConditionVariable(PLT_COND* cond) {
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
WakeConditionVariable(cond);
|
WakeConditionVariable(cond);
|
||||||
#elif defined(__vita__)
|
|
||||||
sceKernelSignalCond(*cond);
|
|
||||||
#elif defined(__WIIU__)
|
#elif defined(__WIIU__)
|
||||||
OSFastCond_Signal(cond);
|
OSFastCond_Signal(cond);
|
||||||
#elif defined(__3DS__)
|
#elif defined(__3DS__)
|
||||||
@ -453,8 +410,6 @@ void PltSignalConditionVariable(PLT_COND* cond) {
|
|||||||
void PltWaitForConditionVariable(PLT_COND* cond, PLT_MUTEX* mutex) {
|
void PltWaitForConditionVariable(PLT_COND* cond, PLT_MUTEX* mutex) {
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
|
SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
|
||||||
#elif defined(__vita__)
|
|
||||||
sceKernelWaitCond(*cond, NULL);
|
|
||||||
#elif defined(__WIIU__)
|
#elif defined(__WIIU__)
|
||||||
OSFastCond_Wait(cond, mutex);
|
OSFastCond_Wait(cond, mutex);
|
||||||
#elif defined(__3DS__)
|
#elif defined(__3DS__)
|
||||||
|
@ -12,6 +12,12 @@
|
|||||||
extern in_port_t n3ds_udp_port;
|
extern in_port_t n3ds_udp_port;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef __vita__
|
||||||
|
#ifdef AF_INET6
|
||||||
|
#undef AF_INET6
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
@ -12,15 +12,6 @@ typedef struct _PLT_THREAD {
|
|||||||
HANDLE handle;
|
HANDLE handle;
|
||||||
bool cancelled;
|
bool cancelled;
|
||||||
} PLT_THREAD;
|
} PLT_THREAD;
|
||||||
#elif defined(__vita__)
|
|
||||||
typedef int PLT_MUTEX;
|
|
||||||
typedef int PLT_COND;
|
|
||||||
typedef struct _PLT_THREAD {
|
|
||||||
int handle;
|
|
||||||
int cancelled;
|
|
||||||
void *context;
|
|
||||||
bool detached;
|
|
||||||
} PLT_THREAD;
|
|
||||||
#elif defined(__WIIU__)
|
#elif defined(__WIIU__)
|
||||||
typedef OSFastMutex PLT_MUTEX;
|
typedef OSFastMutex PLT_MUTEX;
|
||||||
typedef OSFastCondition PLT_COND;
|
typedef OSFastCondition PLT_COND;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user