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 errno = ENOSYS; // Functionality not available
47 return -1;
48 }
49 sema.max_count = 1;
50 sema.option = (u32)"nanosleep";
51 sema.init_count = 0;
52 sema_id = CreateSema(&sema);
53 if (sema_id < 0)
54 {
55 errno = EAGAIN; // Resource temporarily unavailable
56 return -1;
57 }
58 timer_alarm_id = SetTimerAlarm(Sec2TimerBusClock(req->tv_sec) + NSec2TimerBusClock(req->tv_nsec), nanosleep_wakeup_callback, (void *)sema_id);
59 if (timer_alarm_id < 0)
60 {
61 DeleteSema(sema_id);
62 errno = EAGAIN; // Resource temporarily unavailable
63 return -1;
64 }
65 WaitSema(sema_id);
66 DeleteSema(sema_id);
67
68 if (rem != NULL)
69 {
70 rem->tv_sec = 0;
71 rem->tv_nsec = 0;
72 }
73
74 return 0;
75}
76#endif
#define ENOSYS
Definition errno.h:185
#define EAGAIN
Definition errno.h:41