1 /* Copyright  (C) 2010-2020 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (retro_timers.h).
5  * ---------------------------------------------------------------------------------------
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __LIBRETRO_COMMON_TIMERS_H
24 #define __LIBRETRO_COMMON_TIMERS_H
25 
26 #include <stdint.h>
27 
28 #if defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)
29 #include <sys/timer.h>
30 #elif defined(XENON)
31 #include <time/time.h>
32 #elif defined(GEKKO) || defined(__PSL1GHT__) || defined(__QNX__)
33 #include <unistd.h>
34 #elif defined(WIIU)
35 #include <wiiu/os/thread.h>
36 #elif defined(PSP)
37 #include <pspthreadman.h>
38 #elif defined(VITA)
39 #include <psp2/kernel/threadmgr.h>
40 #elif defined(_3DS)
41 #include <3ds.h>
42 #else
43 #include <time.h>
44 #endif
45 
46 #if defined(_WIN32) && !defined(_XBOX)
47 #define WIN32_LEAN_AND_MEAN
48 #include <windows.h>
49 #elif defined(_WIN32) && defined(_XBOX)
50 #include <Xtl.h>
51 #endif
52 
53 #include <limits.h>
54 
55 #ifdef _MSC_VER
56 #include <compat/msvc.h>
57 #endif
58 #include <retro_inline.h>
59 
60 #ifdef DJGPP
61 #define timespec timeval
62 #define tv_nsec tv_usec
63 #include <unistd.h>
64 
65 extern int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
66 
nanosleepDOS(const struct timespec * rqtp,struct timespec * rmtp)67 static int nanosleepDOS(const struct timespec *rqtp, struct timespec *rmtp)
68 {
69    usleep(1000000L * rqtp->tv_sec + rqtp->tv_nsec / 1000);
70 
71    if (rmtp)
72       rmtp->tv_sec = rmtp->tv_nsec=0;
73 
74    return 0;
75 }
76 
77 #define nanosleep nanosleepDOS
78 #endif
79 
80 /**
81  * retro_sleep:
82  * @msec         : amount in milliseconds to sleep
83  *
84  * Sleeps for a specified amount of milliseconds (@msec).
85  **/
86 #if defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)
87 #define retro_sleep(msec) (sys_timer_usleep(1000 * (msec)))
88 #elif defined(PSP) || defined(VITA)
89 #define retro_sleep(msec) (sceKernelDelayThread(1000 * (msec)))
90 #elif defined(_3DS)
91 #define retro_sleep(msec) (svcSleepThread(1000000 * (s64)(msec)))
92 #elif defined(__WINRT__) || defined(WINAPI_FAMILY) && WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
93 #define retro_sleep(msec) (SleepEx((msec), FALSE))
94 #elif defined(_WIN32)
95 #define retro_sleep(msec) (Sleep((msec)))
96 #elif defined(XENON)
97 #define retro_sleep(msec) (udelay(1000 * (msec)))
98 #elif defined(GEKKO) || defined(__PSL1GHT__) || defined(__QNX__)
99 #define retro_sleep(msec) (usleep(1000 * (msec)))
100 #elif defined(WIIU)
101 #define retro_sleep(msec) (OSSleepTicks(ms_to_ticks((msec))))
102 #else
retro_sleep(unsigned msec)103 static INLINE void retro_sleep(unsigned msec)
104 {
105    struct timespec tv = {0};
106    tv.tv_sec          = msec / 1000;
107    tv.tv_nsec         = (msec % 1000) * 1000000;
108    nanosleep(&tv, NULL);
109 }
110 #endif
111 
112 #endif
113