PS2SDK
PS2 Homebrew Libraries
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 #include <mipscopaccess.h>
21 
22 #ifdef F_DelayThread
23 static u64 DelayThreadWakeup_callback(s32 alarm_id, u64 scheduled_time, u64 actual_time, void *arg, void *pc_value)
24 {
25  (void)alarm_id;
26  (void)scheduled_time;
27  (void)actual_time;
28  (void)pc_value;
29 
30  iSignalSema((s32)arg);
31  ExitHandler();
32  return 0;
33 }
34 
35 s32 DelayThread(s32 microseconds)
36 {
37  u32 eie;
38  s32 sema_id;
39  s32 timer_alarm_id;
40  ee_sema_t sema;
41 
42  eie = get_mips_cop_reg(0, COP0_REG_Status);
43  if ((eie & 0x10000) == 0)
44  {
45  return 0x80008008; // ECPUDI
46  }
47  sema.max_count = 1;
48  sema.option = (u32)"DelayThread";
49  sema.init_count = 0;
50  sema_id = CreateSema(&sema);
51  if (sema_id < 0)
52  {
53  return 0x80008003; // ESEMAPHORE
54  }
55  timer_alarm_id = SetTimerAlarm(TimerUSec2BusClock(0, microseconds), DelayThreadWakeup_callback, (void *)sema_id);
56  if (timer_alarm_id < 0)
57  {
58  DeleteSema(sema_id);
59  return timer_alarm_id;
60  }
61  WaitSema(sema_id);
62  DeleteSema(sema_id);
63  return 0;
64 }
65 #endif
kernel.h
COP0_REG_Status
@ COP0_REG_Status
Definition: mipscopaccess.h:36
mipscopaccess.h
t_ee_sema
Definition: kernel.h:193
timer.h