Skip to content

Commit

Permalink
Add LWP_ThreadTimedSleep
Browse files Browse the repository at this point in the history
  • Loading branch information
Extrems committed May 6, 2024
1 parent 6bed976 commit 76a2f80
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
13 changes: 12 additions & 1 deletion gc/ogc/lwp.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ distribution.
*/

#include <gctypes.h>
#include <time.h>

#define LWP_CLOSED -1
#define LWP_SUCCESSFUL 0
Expand Down Expand Up @@ -176,11 +177,21 @@ void LWP_CloseQueue(lwpq_t thequeue);
\brief Pushes the current thread onto the given thread synchronization queue and sets the thread state to blocked.
\param[in] thequeue handle to the thread's synchronization queue to push the thread on
\return none
\return 0 on success, <0 on error
*/
s32 LWP_ThreadSleep(lwpq_t thequeue);


/*! \fn s32 LWP_ThreadTimedSleep(lwpq_t thequeue,const struct timespec *abstime)
\brief Pushes the current thread onto the given thread synchronization queue and sets the thread state to blocked until timeout.
\param[in] thequeue handle to the thread's synchronization queue to push the thread on
\param[in] abstime pointer to a timespec structure holding the abs time for the timeout.
\return 0 on success, <0 on error
*/
s32 LWP_ThreadTimedSleep(lwpq_t thequeue,const struct timespec *abstime);


/*! \fn void LWP_ThreadSignal(lwpq_t thequeue)
\brief Signals one thread to be revmoved from the thread synchronization queue and sets it back to running state.
\param[in] thequeue handle to the thread's synchronization queue to pop the blocked thread off
Expand Down
21 changes: 17 additions & 4 deletions libogc/lwp.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ s32 LWP_InitQueue(lwpq_t *thequeue)
tq = __lwp_tqueue_allocate();
if(!tq) return -1;

__lwp_threadqueue_init(&tq->tqueue,LWP_THREADQ_MODEFIFO,LWP_STATES_WAITING_ON_THREADQ,0);
__lwp_threadqueue_init(&tq->tqueue,LWP_THREADQ_MODEFIFO,LWP_STATES_WAITING_ON_THREADQ,ETIMEDOUT);

*thequeue = (lwpq_t)(LWP_OBJMASKTYPE(LWP_OBJTYPE_TQUEUE)|LWP_OBJMASKID(tq->object.id));
__lwp_thread_dispatchenable();
Expand All @@ -371,7 +371,7 @@ void LWP_CloseQueue(lwpq_t thequeue)
return;
}

s32 LWP_ThreadSleep(lwpq_t thequeue)
static s32 __lwp_tqueue_sleepsupp(lwpq_t thequeue,u64 timeout)
{
u32 level;
tqueue_st *tq;
Expand All @@ -389,9 +389,22 @@ s32 LWP_ThreadSleep(lwpq_t thequeue)
exec->wait.queue = &tq->tqueue;
exec->wait.id = thequeue;
_CPU_ISR_Restore(level);
__lwp_threadqueue_enqueue(&tq->tqueue,LWP_THREADQ_NOTIMEOUT);
__lwp_threadqueue_enqueue(&tq->tqueue,timeout);
__lwp_thread_dispatchenable();
return 0;
return exec->wait.ret_code;
}

s32 LWP_ThreadSleep(lwpq_t thequeue)
{
return __lwp_tqueue_sleepsupp(thequeue,LWP_THREADQ_NOTIMEOUT);
}

s32 LWP_ThreadTimedSleep(lwpq_t thequeue,const struct timespec *abstime)
{
u64 timeout = LWP_THREADQ_NOTIMEOUT;

if(abstime) timeout = __lwp_wd_calc_ticks(abstime);
return __lwp_tqueue_sleepsupp(thequeue,timeout);
}

void LWP_ThreadBroadcast(lwpq_t thequeue)
Expand Down
8 changes: 4 additions & 4 deletions libogc/semaphore.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static sema_st* __lwp_sema_allocate(void)
return NULL;
}

static s32 __lwp_sema_waitsupp(sem_t sem,u64 timeout,u8 block)
static s32 __lwp_sema_waitsupp(sem_t sem,u8 block,u64 timeout)
{
sema_st *lwp_sem;

Expand Down Expand Up @@ -130,20 +130,20 @@ s32 LWP_SemInit(sem_t *sem,u32 start,u32 max)

s32 LWP_SemWait(sem_t sem)
{
return __lwp_sema_waitsupp(sem,LWP_THREADQ_NOTIMEOUT,TRUE);
return __lwp_sema_waitsupp(sem,TRUE,LWP_THREADQ_NOTIMEOUT);
}

s32 LWP_SemTimedWait(sem_t sem,const struct timespec *abstime)
{
u64 timeout = LWP_THREADQ_NOTIMEOUT;

if(abstime) timeout = __lwp_wd_calc_ticks(abstime);
return __lwp_sema_waitsupp(sem,timeout,TRUE);
return __lwp_sema_waitsupp(sem,TRUE,timeout);
}

s32 LWP_SemTryWait(sem_t sem)
{
return __lwp_sema_waitsupp(sem,LWP_THREADQ_NOTIMEOUT,FALSE);
return __lwp_sema_waitsupp(sem,FALSE,LWP_THREADQ_NOTIMEOUT);
}

s32 LWP_SemPost(sem_t sem)
Expand Down

0 comments on commit 76a2f80

Please sign in to comment.