From 5c6cc731e3203281cd5e49df68d2d1a6021e5618 Mon Sep 17 00:00:00 2001 From: Francisco Javier Trujillo Mata Date: Sun, 17 Mar 2024 14:55:09 +0100 Subject: [PATCH 1/3] Fixing pthreadglue --- src/libpthreadglue/osal.c | 16 ++++++++++++++-- src/libpthreadglue/tls-helper.c | 4 +++- src/prof/prof.c | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/libpthreadglue/osal.c b/src/libpthreadglue/osal.c index 33b33fe4..b7ca9856 100644 --- a/src/libpthreadglue/osal.c +++ b/src/libpthreadglue/osal.c @@ -339,8 +339,9 @@ pte_osResult pte_osThreadWaitForEnd(pte_osThreadHandle threadHandle) while (1) { SceKernelThreadRunStatus info; - /* Poll task to see if it has ended */ + + /* Prepare info before be use in sceKernelReferThreadRunStatus */ memset(&info,0,sizeof(info)); info.size = sizeof(info); sceKernelReferThreadRunStatus(threadHandle, &info); @@ -355,6 +356,9 @@ pte_osResult pte_osThreadWaitForEnd(pte_osThreadHandle threadHandle) if (pThreadData != NULL) { SceUID osResult; + /* Prepare semInfo before be use in sceKernelReferSemaStatus */ + memset(&semInfo, 0, sizeof(semInfo)); + semInfo.size = sizeof(semInfo); osResult = sceKernelReferSemaStatus(pThreadData->cancelSem, &semInfo); if (osResult == SCE_KERNEL_ERROR_OK) { if (semInfo.currentCount > 0) { @@ -390,7 +394,9 @@ int pte_osThreadGetPriority(pte_osThreadHandle threadHandle) { SceKernelThreadInfo thinfo; - thinfo.size = sizeof(SceKernelThreadInfo); + /* Prepare info before be use in sceKernelReferThreadRunStatus */ + memset(&thinfo,0,sizeof(thinfo)); + thinfo.size = sizeof(thinfo); sceKernelReferThreadStatus(threadHandle, &thinfo); return thinfo.currentPriority; @@ -435,6 +441,9 @@ pte_osResult pte_osThreadCheckCancel(pte_osThreadHandle threadHandle) pThreadData = __getThreadData(threadHandle); if (pThreadData != NULL) { + /* Prepare semInfo before be use in sceKernelReferSemaStatus */ + memset(&semInfo, 0, sizeof(semInfo)); + semInfo.size = sizeof(semInfo); osResult = sceKernelReferSemaStatus(pThreadData->cancelSem, &semInfo); if (osResult == SCE_KERNEL_ERROR_OK) { @@ -687,6 +696,9 @@ pte_osResult pte_osSemaphoreCancellablePend(pte_osSemaphoreHandle semHandle, uns if (pThreadData != NULL) { SceUID osResult; + /* Prepare semInfo before be use in sceKernelReferSemaStatus */ + memset(&semInfo, 0, sizeof(semInfo)); + semInfo.size = sizeof(semInfo); osResult = sceKernelReferSemaStatus(pThreadData->cancelSem, &semInfo); if (osResult == SCE_KERNEL_ERROR_OK) { if (semInfo.currentCount > 0) { diff --git a/src/libpthreadglue/tls-helper.c b/src/libpthreadglue/tls-helper.c index 84563c92..c2ae94cf 100644 --- a/src/libpthreadglue/tls-helper.c +++ b/src/libpthreadglue/tls-helper.c @@ -158,7 +158,9 @@ void *__getTlsStructFromThread(SceUID thid) int numMatches; - thinfo.size = sizeof(SceKernelThreadInfo); + /* Prepare thinfo before be use in sceKernelReferThreadRunStatus */ + memset(&thinfo,0,sizeof(thinfo)); + thinfo.size = sizeof(thinfo); sceKernelReferThreadStatus(thid, &thinfo); numMatches = sscanf(thinfo.name,"pthread%04d__%x", &thrNum, &ptr); diff --git a/src/prof/prof.c b/src/prof/prof.c index c3cc96f3..7a53f69d 100644 --- a/src/prof/prof.c +++ b/src/prof/prof.c @@ -130,6 +130,7 @@ static void initialize() int thid = sceKernelGetThreadId(); SceKernelThreadInfo info; + memset(&info, 0, sizeof(info)); info.size = sizeof(info); int ret = sceKernelReferThreadStatus(thid, &info); From 3615791863b388ace36bbf3d9020bb9d28688079 Mon Sep 17 00:00:00 2001 From: Francisco Javier Trujillo Mata Date: Wed, 3 Apr 2024 23:56:32 +0200 Subject: [PATCH 2/3] Fix pthread priorities and timeout --- src/libpthreadglue/osal.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/libpthreadglue/osal.c b/src/libpthreadglue/osal.c index b7ca9856..3a28a94f 100644 --- a/src/libpthreadglue/osal.c +++ b/src/libpthreadglue/osal.c @@ -180,6 +180,12 @@ pte_osResult pte_osTerminate(void) { * Threads * ***************************************************************************/ + +static inline int invert_priority(int priority) +{ + return (pte_osThreadGetMinPriority() - priority) + pte_osThreadGetMaxPriority(); +} + #ifdef F_pte_osThreadCreate pte_osResult pte_osThreadCreate(pte_osThreadEntryPoint entryPoint, int stackSize, @@ -255,7 +261,7 @@ pte_osResult pte_osThreadCreate(pte_osThreadEntryPoint entryPoint, // printf("%s %p %d %d %d\n",threadName, __pspStubThreadEntry, initialPriority, stackSize, pspAttr); threadId = sceKernelCreateThread(threadName, __pspStubThreadEntry, - initialPriority, + invert_priority(initialPriority), stackSize, pspAttr, NULL); @@ -399,14 +405,14 @@ int pte_osThreadGetPriority(pte_osThreadHandle threadHandle) thinfo.size = sizeof(thinfo); sceKernelReferThreadStatus(threadHandle, &thinfo); - return thinfo.currentPriority; + return invert_priority(thinfo.currentPriority); } #endif #ifdef F_pte_osThreadSetPriority pte_osResult pte_osThreadSetPriority(pte_osThreadHandle threadHandle, int newPriority) { - sceKernelChangeThreadPriority(threadHandle, newPriority); + sceKernelChangeThreadPriority(threadHandle, invert_priority(newPriority)); return PTE_OS_OK; } #endif @@ -475,21 +481,21 @@ void pte_osThreadSleep(unsigned int msecs) #ifdef F_pte_osThreadGetMinPriority int pte_osThreadGetMinPriority() { - return 17; + return pte_osThreadGetDefaultPriority() - 32; } #endif #ifdef F_pte_osThreadGetMaxPriority int pte_osThreadGetMaxPriority() { - return 30; + return pte_osThreadGetDefaultPriority() + 32; } #endif #ifdef F_pte_osThreadGetDefaultPriority int pte_osThreadGetDefaultPriority() { - return 18; + return 60; } #endif @@ -621,7 +627,7 @@ pte_osResult pte_osSemaphorePend(pte_osSemaphoreHandle handle, unsigned int *pTi { unsigned int timeoutUsecs; unsigned int *pTimeoutUsecs; - SceUInt result; + int result; pte_osResult osResult; if (pTimeoutMsecs == NULL) { @@ -661,17 +667,14 @@ pte_osResult pte_osSemaphoreCancellablePend(pte_osSemaphoreHandle semHandle, uns clock_t start_time; pte_osResult result = PTE_OS_OK; unsigned int timeout; - unsigned char timeoutEnabled; start_time = clock(); // clock() is in microseconds, timeout as passed in was in milliseconds if (pTimeout == NULL) { timeout = 0; - timeoutEnabled = 0; } else { timeout = *pTimeout * 1000; - timeoutEnabled = 1; } while (1) { @@ -686,7 +689,7 @@ pte_osResult pte_osSemaphoreCancellablePend(pte_osSemaphoreHandle semHandle, uns /* User semaphore posted to */ result = PTE_OS_OK; break; - } else if ((timeoutEnabled) && ((clock() - start_time) > timeout)) { + } else if ((pTimeout) && ((clock() - start_time) > timeout)) { /* Timeout expired */ result = PTE_OS_TIMEOUT; break; From eef342187319ead21cfe9a6598fd79a21188c1fa Mon Sep 17 00:00:00 2001 From: Francisco Javier Trujillo Mata Date: Wed, 3 Apr 2024 23:56:44 +0200 Subject: [PATCH 3/3] flix _times implementation --- src/libcglue/glue.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcglue/glue.c b/src/libcglue/glue.c index 7cc3f4fc..c7f2ba4d 100755 --- a/src/libcglue/glue.c +++ b/src/libcglue/glue.c @@ -758,7 +758,7 @@ clock_t _times(struct tms *buffer) if (buffer != NULL) { buffer->tms_utime = clk; buffer->tms_stime = 0; - buffer->tms_cutime = clk; + buffer->tms_cutime = 0; buffer->tms_cstime = 0; }