Skip to content

Commit

Permalink
Merge pull request #473 from uyjulian/eekernel_waitsema_timeout
Browse files Browse the repository at this point in the history
Refactor WaitSema with a timeout into a separate function
  • Loading branch information
fjtrujy authored Dec 10, 2023
2 parents 8188748 + b89f07b commit ec88a92
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 47 deletions.
10 changes: 9 additions & 1 deletion ee/kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ GETKERNEL_OBJS = GetSyscallHandler.o GetSyscall.o GetExceptionHandler.o GetInter

INITSYS_OBJS = _InitSys.o TerminateLibrary.o

### Kernel utility objects

KERNEL_UTIL_OBJS = WaitSemaEx.o

### SYSCALL OBJECTS

KERNEL_OBJS = ResetEE.o SetGsCrt.o $(EXEC_SYSCALLS) \
Expand Down Expand Up @@ -142,7 +146,7 @@ KERNEL_OBJS = ResetEE.o SetGsCrt.o $(EXEC_SYSCALLS) \
EE_OBJS = $(KERNEL_OBJS) $(SIFCMD_OBJS) $(SIFRPC_OBJS) $(FILEIO_OBJS) \
$(LOADFILE_OBJS) $(IOPHEAP_OBJS) $(IOPCONTROL_OBJS) $(ROM0_OBJS) $(CONFIG_OBJS) \
$(GLUE_OBJS) $(SIO_OBJS) $(TIMER_OBJS) $(TIMER_ALARM_OBJS) $(GETKERNEL_OBJS) \
$(INITSYS_OBJS) erl-support.o
$(INITSYS_OBJS) $(KERNEL_UTIL_OBJS) erl-support.o


EE_OBJS += $(THREAD_OBJS) $(LIBOSD_OBJS) $(TLBFUNC_OBJS) $(ALARM_OBJS) $(EXIT_OBJS) $(SETUP_OBJS) setup_syscalls.o debug.o
Expand Down Expand Up @@ -220,6 +224,10 @@ $(INITSYS_OBJS:%=$(EE_OBJS_DIR)%): $(EE_SRC_DIR)initsys.c
$(DIR_GUARD)
$(EE_C_COMPILE) -DF_$(*:$(EE_OBJS_DIR)%=%) $< -c -o $@

$(KERNEL_UTIL_OBJS:%=$(EE_OBJS_DIR)%): $(EE_SRC_DIR)kernel_util.c
$(DIR_GUARD)
$(EE_C_COMPILE) -DF_$(*:$(EE_OBJS_DIR)%=%) $< -c -o $@

$(EXIT_OBJS:%=$(EE_OBJS_DIR)%): $(EE_SRC_DIR)exit.c
$(DIR_GUARD)
$(EE_C_COMPILE) -DF_$(*:$(EE_OBJS_DIR)%=%) $< -c -o $@
Expand Down
23 changes: 23 additions & 0 deletions ee/kernel/include/kernel_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# Copyright ps2dev - http://www.ps2dev.org
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.
*/

/**
* @file
* Kernel utility functions
*/

#ifndef __KERNEL_UTIL_H__
#define __KERNEL_UTIL_H__

#include <tamtypes.h>

extern s32 WaitSemaEx(s32 semaid, int signal, u64 *timeout);

#endif
62 changes: 62 additions & 0 deletions ee/kernel/src/kernel_util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# Copyright ps2dev - http://www.ps2dev.org
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.
*/

#include <kernel.h>
#include <timer_alarm.h>
#include <kernel_util.h>

#ifdef F_WaitSemaEx
static void WaitSemaEx_callback(struct timer_alarm_t *alarm, void *arg)
{
iReleaseWaitThread((s32)arg);
}

/** Semaphore wait function similar to newer platforms */
s32 WaitSemaEx(s32 semaid, int signal, u64 *timeout)
{
int ret;
struct timer_alarm_t alarm;

// TODO: other values NYI
if (signal != 1)
{
return -1;
}

if (timeout != NULL && *timeout == 0)
{
ret = PollSema(semaid);
if (ret < 0)
{
return ret;
}
return semaid;
}

if (timeout != NULL)
{
InitializeTimerAlarm(&alarm);
SetTimerAlarm(&alarm, USec2TimerBusClock(*timeout), &WaitSemaEx_callback, (void *)GetThreadId());
}

ret = WaitSema(semaid);

if (timeout != NULL)
{
StopTimerAlarm(&alarm);
}

if (ret < 0)
{
return ret;
}
return semaid;
}
#endif
1 change: 1 addition & 0 deletions ee/libpthreadglue/src/include/ps2_osal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stdint.h>
#include <string.h>
#include <kernel.h>
#include <kernel_util.h>

typedef int pte_osThreadHandle;
typedef int pte_osSemaphoreHandle;
Expand Down
17 changes: 7 additions & 10 deletions ee/libpthreadglue/src/osal.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <ps2_osal.h>
#include <time.h>
#include <timer_alarm.h>

#define MAX_PS2_UID 2048 // SWAG
#define DEFAULT_STACK_SIZE_BYTES 4096
Expand Down Expand Up @@ -75,10 +74,6 @@ ps2ThreadData *__getThreadData(s32 threadHandle)
ps2ThreadData *__getThreadData(s32 threadHandle);
#endif

static void usercb(struct timer_alarm_t *alarm, void *arg) {
iReleaseWaitThread((int)arg);
}

static inline void DelayThread(uint32_t usecs) {
struct timespec tv = {0};
tv.tv_sec = usecs / 1000000;
Expand All @@ -89,8 +84,8 @@ static inline void DelayThread(uint32_t usecs) {
static inline int SemWaitTimeout(s32 semHandle, uint32_t timeout)
{
int ret;
struct timer_alarm_t alarm;
InitializeTimerAlarm(&alarm);
u64 timeoutUsec;
u64 *timeoutPtr;

if (timeout == 0) {
if (PollSema(semHandle) < 0) {
Expand All @@ -99,12 +94,14 @@ static inline int SemWaitTimeout(s32 semHandle, uint32_t timeout)
return 0;
}

timeoutPtr = NULL;

if (timeout > 0 && timeout != UINT32_MAX) {
SetTimerAlarm(&alarm, MSec2TimerBusClock(timeout), &usercb, (void *)GetThreadId());
timeoutUsec = timeout * 1000;
timeoutPtr = &timeoutUsec;
}

ret = WaitSema(semHandle);
StopTimerAlarm(&alarm);
ret = WaitSemaEx(semHandle, 1, timeoutPtr);

if (ret < 0)
return -2;
Expand Down
1 change: 1 addition & 0 deletions ee/network/tcpip/include/ps2ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void ps2ipDeinit(void);
/**
* Use to specify the number of H-sync ticks per milisecond (Default: 16).
* Use this function to keep timings accurate, if a mode like 480P (~31KHz H-sync) is used instead of NTSC/PAL (~16KHz H-sync).
* This function is obsolete, so it is stubbed for compatibility purposes.
*/
void ps2ipSetHsyncTicksPerMSec(unsigned char ticks);

Expand Down
5 changes: 2 additions & 3 deletions ee/network/tcpip/src/ps2ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,5 @@ void ps2ipDeinit(void){
NetManUnregisterNetworkStack();
}

void ps2ipSetHsyncTicksPerMSec(unsigned char ticks){
hsyncTicksPerMSec = ticks;
}
// Stubbed for compatibility purposes
void ps2ipSetHsyncTicksPerMSec(unsigned char ticks) {}
52 changes: 19 additions & 33 deletions ee/network/tcpip/src/sys_arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <stdio.h>
#include <string.h>
#include <kernel.h>
#include <kernel_util.h>
#include <time.h>
#include <limits.h>

Expand Down Expand Up @@ -79,14 +80,6 @@ static void free_msg(arch_message *msg)
SignalSema(MsgCountSema);
}

static void TimeoutHandler(s32 alarm_id, u16 time, void *pvArg){
(void)alarm_id;
(void)time;

iReleaseWaitThread((int)pvArg);
ExitHandler();
}

static inline u32_t ComputeTimeDiff(u32 start, u32 end)
{
u32 NumTicksElasped=(end<start)?UINT_MAX-start+end:start-end;
Expand Down Expand Up @@ -195,13 +188,6 @@ void sys_mbox_set_invalid(sys_mbox_t *mbox)
*mbox=SYS_MBOX_NULL;
}

extern unsigned short int hsyncTicksPerMSec;

static inline unsigned int mSec2HSyncTicks(unsigned int msec)
{
return msec*hsyncTicksPerMSec;
}

static void RetrieveMbxInternal(sys_mbox_t mBox, arch_message **message)
{
arch_message *NextMessage;
Expand All @@ -224,29 +210,29 @@ static void RetrieveMbxInternal(sys_mbox_t mBox, arch_message **message)

static int WaitSemaTimeout(int sema, unsigned int msec)
{
unsigned int ticks;
int threadID;
int ret;
u64 timeoutUsec;
u64 *timeoutPtr;

ticks = mSec2HSyncTicks(msec);
threadID = GetThreadId();
while(ticks > 0)
{
unsigned short int ticksToWait;
int alarmID;

ticksToWait = ticks > USHRT_MAX ? USHRT_MAX : ticks;
alarmID = SetAlarm(ticksToWait, &TimeoutHandler, (void*)threadID);
if (WaitSema(sema) == sema)
{
ReleaseAlarm(alarmID);
return sema; //Wait condition satisfied.
if (msec == 0) {
if (PollSema(sema) < 0) {
return -1;
}
return sema;
}

timeoutPtr = NULL;

//Otherwise, continue waiting.
ticks -= ticksToWait;
if (msec > 0 && msec != UINT32_MAX) {
timeoutUsec = msec * 1000;
timeoutPtr = &timeoutUsec;
}

return -1; //Timed out.
ret = WaitSemaEx(sema, 1, timeoutPtr);

if (ret < 0)
return -1; //Timed out.
return sema; //Wait condition satisfied.
}

static int ReceiveMbx(arch_message **message, sys_mbox_t mBox, u32_t timeout)
Expand Down

0 comments on commit ec88a92

Please sign in to comment.