1 /* Copyright  (C) 2010-2018 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(PS2)
41 #include <SDL/SDL_timer.h>
42 #elif defined(_3DS)
43 #include <3ds.h>
44 #else
45 #include <time.h>
46 #endif
47 
48 #if defined(_WIN32) && !defined(_XBOX)
49 #define WIN32_LEAN_AND_MEAN
50 #include <windows.h>
51 #elif defined(_WIN32) && defined(_XBOX)
52 #include <Xtl.h>
53 #endif
54 
55 #include <limits.h>
56 
57 #ifdef _MSC_VER
58 #include <compat/msvc.h>
59 #endif
60 #include <retro_inline.h>
61 
62 #ifdef DJGPP
63 #define timespec timeval
64 #define tv_nsec tv_usec
65 #include <unistd.h>
66 
67 extern int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
68 
nanosleepDOS(const struct timespec * rqtp,struct timespec * rmtp)69 static int nanosleepDOS(const struct timespec *rqtp, struct timespec *rmtp)
70 {
71    usleep(1000000 * rqtp->tv_sec + rqtp->tv_nsec / 1000);
72 
73    if (rmtp)
74       rmtp->tv_sec = rmtp->tv_nsec=0;
75 
76    return 0;
77 }
78 
79 #define nanosleep nanosleepDOS
80 #endif
81 
82 /**
83  * retro_sleep:
84  * @msec         : amount in milliseconds to sleep
85  *
86  * Sleeps for a specified amount of milliseconds (@msec).
87  **/
retro_sleep(unsigned msec)88 static INLINE void retro_sleep(unsigned msec)
89 {
90 #if defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)
91    sys_timer_usleep(1000 * msec);
92 #elif defined(PSP) || defined(VITA)
93    sceKernelDelayThread(1000 * msec);
94 #elif defined(PS2)
95    SDL_Delay(msec);
96 #elif defined(_3DS)
97    svcSleepThread(1000000 * (s64)msec);
98 #elif defined(__WINRT__) || defined(WINAPI_FAMILY) && WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
99    SleepEx(msec, FALSE);
100 #elif defined(_WIN32)
101    Sleep(msec);
102 #elif defined(XENON)
103    udelay(1000 * msec);
104 #elif defined(GEKKO) || defined(__PSL1GHT__) || defined(__QNX__)
105    usleep(1000 * msec);
106 #elif defined(WIIU)
107    OSSleepTicks(ms_to_ticks(msec));
108 #else
109    struct timespec tv = {0};
110    tv.tv_sec = msec / 1000;
111    tv.tv_nsec = (msec % 1000) * 1000000;
112    nanosleep(&tv, NULL);
113 #endif
114 }
115 
116 #endif
117