PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
sleep.c
Go to the documentation of this file.
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright 2001-2005, 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 <unistd.h>
18#include <stdint.h>
19#include <errno.h>
20#include <time.h>
21#include <timer_alarm.h>
22
23#ifdef F_nanosleep
24static u64 nanosleep_wakeup_callback(s32 alarm_id, u64 scheduled_time, u64 actual_time, void *arg, void *pc_value)
25{
26 (void)alarm_id;
27 (void)scheduled_time;
28 (void)actual_time;
29 (void)pc_value;
30
31 iSignalSema((s32)arg);
32 ExitHandler();
33 return 0;
34}
35
36int nanosleep(const struct timespec *req, struct timespec *rem)
37{
38 u32 eie;
39 s32 sema_id;
40 s32 timer_alarm_id;
41 ee_sema_t sema;
42
43 __asm__ __volatile__ ("mfc0\t%0, $12" : "=r" (eie));
44 if ((eie & 0x10000) == 0)
45 {
46 return 0;
47 }
48 sema.max_count = 1;
49 sema.option = (u32)"nanosleep";
50 sema.init_count = 0;
51 sema_id = CreateSema(&sema);
52 if (sema_id < 0)
53 {
54 return 0;
55 }
56 timer_alarm_id = SetTimerAlarm(Sec2TimerBusClock(req->tv_sec) + NSec2TimerBusClock(req->tv_nsec), nanosleep_wakeup_callback, (void *)sema_id);
57 if (timer_alarm_id < 0)
58 {
59 DeleteSema(sema_id);
60 return 0;
61 }
62 WaitSema(sema_id);
63 DeleteSema(sema_id);
64
65 if (rem != NULL)
66 {
67 rem->tv_sec = 0;
68 rem->tv_nsec = 0;
69 }
70
71 return 0;
72}
73#endif