PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
delaythread.c
Go to the documentation of this file.
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright ps2dev - http://www.ps2dev.org
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
16#include <kernel.h>
17#include <timer.h>
18#include <timer_alarm.h>
19#include <delaythread.h>
20
21#ifdef F_DelayThread
22static u64 DelayThreadWakeup_callback(s32 alarm_id, u64 scheduled_time, u64 actual_time, void *arg, void *pc_value)
23{
24 (void)alarm_id;
25 (void)scheduled_time;
26 (void)actual_time;
27 (void)pc_value;
28
29 iSignalSema((s32)arg);
30 ExitHandler();
31 return 0;
32}
33
34s32 DelayThread(s32 microseconds)
35{
36 u32 eie;
37 s32 sema_id;
38 s32 timer_alarm_id;
39 ee_sema_t sema;
40
41 __asm__ __volatile__ ("mfc0\t%0, $12" : "=r" (eie));
42 if ((eie & 0x10000) == 0)
43 {
44 return 0x80008008; // ECPUDI
45 }
46 sema.max_count = 1;
47 sema.option = (u32)"DelayThread";
48 sema.init_count = 0;
49 sema_id = CreateSema(&sema);
50 if (sema_id < 0)
51 {
52 return 0x80008003; // ESEMAPHORE
53 }
54 timer_alarm_id = SetTimerAlarm(TimerUSec2BusClock(0, microseconds), DelayThreadWakeup_callback, (void *)sema_id);
55 if (timer_alarm_id < 0)
56 {
57 DeleteSema(sema_id);
58 return timer_alarm_id;
59 }
60 WaitSema(sema_id);
61 DeleteSema(sema_id);
62 return 0;
63}
64#endif