trunk/src/osd/modules/lib/osdlib_unix.c
| r242809 | r242810 | |
| 4 | 4 | #include <sys/mman.h> |
| 5 | 5 | #include <sys/types.h> |
| 6 | 6 | #include <signal.h> |
| 7 | #include <time.h> |
| 8 | #include <sys/time.h> |
| 9 | #ifdef SDLMAME_EMSCRIPTEN |
| 10 | #include <emscripten.h> |
| 11 | #endif |
| 7 | 12 | |
| 13 | // MAME headers |
| 14 | #include "osdcore.h" |
| 8 | 15 | #include "osdlib.h" |
| 9 | 16 | |
| 10 | 17 | //============================================================ |
| r242809 | r242810 | |
| 88 | 95 | #error "MALLOC_DEBUG not yet supported" |
| 89 | 96 | #endif |
| 90 | 97 | } |
| 98 | |
| 99 | //============================================================ |
| 100 | // osd_cycles |
| 101 | //============================================================ |
| 102 | |
| 103 | osd_ticks_t osd_ticks(void) |
| 104 | { |
| 105 | #ifdef SDLMAME_EMSCRIPTEN |
| 106 | return (osd_ticks_t)(emscripten_get_now() * 1000.0); |
| 107 | #else |
| 108 | struct timeval tp; |
| 109 | static osd_ticks_t start_sec = 0; |
| 110 | |
| 111 | gettimeofday(&tp, NULL); |
| 112 | if (start_sec==0) |
| 113 | start_sec = tp.tv_sec; |
| 114 | return (tp.tv_sec - start_sec) * (osd_ticks_t) 1000000 + tp.tv_usec; |
| 115 | #endif |
| 116 | } |
| 117 | |
| 118 | osd_ticks_t osd_ticks_per_second(void) |
| 119 | { |
| 120 | return (osd_ticks_t) 1000000; |
| 121 | } |
| 122 | |
| 123 | //============================================================ |
| 124 | // osd_sleep |
| 125 | //============================================================ |
| 126 | |
| 127 | void osd_sleep(osd_ticks_t duration) |
| 128 | { |
| 129 | UINT32 msec; |
| 130 | |
| 131 | // convert to milliseconds, rounding down |
| 132 | msec = (UINT32)(duration * 1000 / osd_ticks_per_second()); |
| 133 | |
| 134 | // only sleep if at least 2 full milliseconds |
| 135 | if (msec >= 2) |
| 136 | { |
| 137 | // take a couple of msecs off the top for good measure |
| 138 | msec -= 2; |
| 139 | usleep(msec*1000); |
| 140 | } |
| 141 | } |
trunk/src/osd/modules/lib/osdlib_win32.c
| r242809 | r242810 | |
| 6 | 6 | |
| 7 | 7 | #define WIN32_LEAN_AND_MEAN |
| 8 | 8 | #include <windows.h> |
| 9 | #include <mmsystem.h> |
| 10 | |
| 9 | 11 | #include <stdlib.h> |
| 10 | 12 | #ifndef _MSC_VER |
| 11 | 13 | #include <unistd.h> |
| r242809 | r242810 | |
| 164 | 166 | } |
| 165 | 167 | #endif |
| 166 | 168 | } |
| 169 | |
| 170 | |
| 171 | //============================================================ |
| 172 | // GLOBAL VARIABLES |
| 173 | //============================================================ |
| 174 | |
| 175 | static osd_ticks_t ticks_per_second = 0; |
| 176 | static osd_ticks_t suspend_ticks = 0; |
| 177 | static BOOL using_qpc = TRUE; |
| 178 | |
| 179 | |
| 180 | |
| 181 | //============================================================ |
| 182 | // osd_ticks |
| 183 | //============================================================ |
| 184 | |
| 185 | osd_ticks_t osd_ticks(void) |
| 186 | { |
| 187 | LARGE_INTEGER performance_count; |
| 188 | |
| 189 | // if we're suspended, just return that |
| 190 | if (suspend_ticks != 0) |
| 191 | return suspend_ticks; |
| 192 | |
| 193 | // if we have a per second count, just go for it |
| 194 | if (ticks_per_second != 0) |
| 195 | { |
| 196 | // QueryPerformanceCounter if we can |
| 197 | if (using_qpc) |
| 198 | { |
| 199 | QueryPerformanceCounter(&performance_count); |
| 200 | return (osd_ticks_t)performance_count.QuadPart - suspend_ticks; |
| 201 | } |
| 202 | |
| 203 | // otherwise, fall back to timeGetTime |
| 204 | else |
| 205 | return (osd_ticks_t)timeGetTime() - suspend_ticks; |
| 206 | } |
| 207 | |
| 208 | // if not, we have to determine it |
| 209 | using_qpc = QueryPerformanceFrequency(&performance_count) && (performance_count.QuadPart != 0); |
| 210 | if (using_qpc) |
| 211 | ticks_per_second = (osd_ticks_t)performance_count.QuadPart; |
| 212 | else |
| 213 | ticks_per_second = 1000; |
| 214 | |
| 215 | // call ourselves to get the first value |
| 216 | return osd_ticks(); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | //============================================================ |
| 221 | // osd_ticks_per_second |
| 222 | //============================================================ |
| 223 | |
| 224 | osd_ticks_t osd_ticks_per_second(void) |
| 225 | { |
| 226 | if (ticks_per_second == 0) |
| 227 | osd_ticks(); |
| 228 | return ticks_per_second; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | //============================================================ |
| 233 | // osd_sleep |
| 234 | //============================================================ |
| 235 | |
| 236 | void osd_sleep(osd_ticks_t duration) |
| 237 | { |
| 238 | DWORD msec; |
| 239 | |
| 240 | // make sure we've computed ticks_per_second |
| 241 | if (ticks_per_second == 0) |
| 242 | (void)osd_ticks(); |
| 243 | |
| 244 | // convert to milliseconds, rounding down |
| 245 | msec = (DWORD)(duration * 1000 / ticks_per_second); |
| 246 | |
| 247 | // only sleep if at least 2 full milliseconds |
| 248 | if (msec >= 2) |
| 249 | { |
| 250 | HANDLE current_thread = GetCurrentThread(); |
| 251 | int old_priority = GetThreadPriority(current_thread); |
| 252 | |
| 253 | // take a couple of msecs off the top for good measure |
| 254 | msec -= 2; |
| 255 | |
| 256 | // bump our thread priority super high so that we get |
| 257 | // priority when we need it |
| 258 | SetThreadPriority(current_thread, THREAD_PRIORITY_TIME_CRITICAL); |
| 259 | Sleep(msec); |
| 260 | SetThreadPriority(current_thread, old_priority); |
| 261 | } |
| 262 | } |
trunk/src/osd/sdl/sdlos_unix.c
| r242809 | r242810 | |
| 26 | 26 | |
| 27 | 27 | |
| 28 | 28 | |
| 29 | | //============================================================ |
| 30 | | // osd_cycles |
| 31 | | //============================================================ |
| 32 | 29 | |
| 33 | | osd_ticks_t osd_ticks(void) |
| 34 | | { |
| 35 | | #ifdef SDLMAME_EMSCRIPTEN |
| 36 | | return (osd_ticks_t)(emscripten_get_now() * 1000.0); |
| 37 | | #else |
| 38 | | struct timeval tp; |
| 39 | | static osd_ticks_t start_sec = 0; |
| 40 | | |
| 41 | | gettimeofday(&tp, NULL); |
| 42 | | if (start_sec==0) |
| 43 | | start_sec = tp.tv_sec; |
| 44 | | return (tp.tv_sec - start_sec) * (osd_ticks_t) 1000000 + tp.tv_usec; |
| 45 | | #endif |
| 46 | | } |
| 47 | | |
| 48 | | osd_ticks_t osd_ticks_per_second(void) |
| 49 | | { |
| 50 | | return (osd_ticks_t) 1000000; |
| 51 | | } |
| 52 | | |
| 53 | | //============================================================ |
| 54 | | // osd_sleep |
| 55 | | //============================================================ |
| 56 | | |
| 57 | | void osd_sleep(osd_ticks_t duration) |
| 58 | | { |
| 59 | | UINT32 msec; |
| 60 | | |
| 61 | | // convert to milliseconds, rounding down |
| 62 | | msec = (UINT32)(duration * 1000 / osd_ticks_per_second()); |
| 63 | | |
| 64 | | // only sleep if at least 2 full milliseconds |
| 65 | | if (msec >= 2) |
| 66 | | { |
| 67 | | // take a couple of msecs off the top for good measure |
| 68 | | msec -= 2; |
| 69 | | usleep(msec*1000); |
| 70 | | } |
| 71 | | } |
| 72 | | |
| 73 | 30 | #if (SDLMAME_SDL2) |
| 74 | 31 | |
| 75 | 32 | //============================================================ |
trunk/src/osd/sdl/sdlos_win32.c
| r242809 | r242810 | |
| 20 | 20 | #include "osdcore.h" |
| 21 | 21 | #include "strconv.h" |
| 22 | 22 | |
| 23 | | //============================================================ |
| 24 | | // PROTOTYPES |
| 25 | | //============================================================ |
| 26 | 23 | |
| 27 | | static osd_ticks_t init_cycle_counter(void); |
| 28 | | static osd_ticks_t performance_cycle_counter(void); |
| 29 | | |
| 30 | 24 | //============================================================ |
| 31 | | // STATIC VARIABLES |
| 32 | | //============================================================ |
| 33 | | |
| 34 | | // global cycle_counter function and divider |
| 35 | | static osd_ticks_t (*cycle_counter)(void) = init_cycle_counter; |
| 36 | | static osd_ticks_t (*ticks_counter)(void) = init_cycle_counter; |
| 37 | | static osd_ticks_t ticks_per_second; |
| 38 | | |
| 39 | | //============================================================ |
| 40 | | // init_cycle_counter |
| 41 | | // |
| 42 | | // to avoid total grossness, this function is split by subarch |
| 43 | | //============================================================ |
| 44 | | |
| 45 | | static osd_ticks_t init_cycle_counter(void) |
| 46 | | { |
| 47 | | osd_ticks_t start, end; |
| 48 | | osd_ticks_t a, b; |
| 49 | | int priority = GetThreadPriority(GetCurrentThread()); |
| 50 | | LARGE_INTEGER frequency; |
| 51 | | |
| 52 | | if (QueryPerformanceFrequency( &frequency )) |
| 53 | | { |
| 54 | | // use performance counter if available as it is constant |
| 55 | | cycle_counter = performance_cycle_counter; |
| 56 | | ticks_counter = performance_cycle_counter; |
| 57 | | |
| 58 | | ticks_per_second = frequency.QuadPart; |
| 59 | | |
| 60 | | // return the current cycle count |
| 61 | | return (*cycle_counter)(); |
| 62 | | } |
| 63 | | else |
| 64 | | { |
| 65 | | fprintf(stderr, "Error! Unable to QueryPerformanceFrequency!\n"); |
| 66 | | exit(-1); |
| 67 | | } |
| 68 | | |
| 69 | | // temporarily set our priority higher |
| 70 | | SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); |
| 71 | | |
| 72 | | // wait for an edge on the timeGetTime call |
| 73 | | a = SDL_GetTicks(); |
| 74 | | do |
| 75 | | { |
| 76 | | b = SDL_GetTicks(); |
| 77 | | } while (a == b); |
| 78 | | |
| 79 | | // get the starting cycle count |
| 80 | | start = (*cycle_counter)(); |
| 81 | | |
| 82 | | // now wait for 1/4 second total |
| 83 | | do |
| 84 | | { |
| 85 | | a = SDL_GetTicks(); |
| 86 | | } while (a - b < 250); |
| 87 | | |
| 88 | | // get the ending cycle count |
| 89 | | end = (*cycle_counter)(); |
| 90 | | |
| 91 | | // compute ticks_per_sec |
| 92 | | ticks_per_second = (end - start) * 4; |
| 93 | | |
| 94 | | // restore our priority |
| 95 | | SetThreadPriority(GetCurrentThread(), priority); |
| 96 | | |
| 97 | | // return the current cycle count |
| 98 | | return (*cycle_counter)(); |
| 99 | | } |
| 100 | | |
| 101 | | //============================================================ |
| 102 | | // performance_cycle_counter |
| 103 | | //============================================================ |
| 104 | | |
| 105 | | static osd_ticks_t performance_cycle_counter(void) |
| 106 | | { |
| 107 | | LARGE_INTEGER performance_count; |
| 108 | | QueryPerformanceCounter(&performance_count); |
| 109 | | return (osd_ticks_t)performance_count.QuadPart; |
| 110 | | } |
| 111 | | |
| 112 | | //============================================================ |
| 113 | | // osd_cycles |
| 114 | | //============================================================ |
| 115 | | |
| 116 | | osd_ticks_t osd_ticks(void) |
| 117 | | { |
| 118 | | return (*cycle_counter)(); |
| 119 | | } |
| 120 | | |
| 121 | | |
| 122 | | //============================================================ |
| 123 | | // osd_ticks_per_second |
| 124 | | //============================================================ |
| 125 | | |
| 126 | | osd_ticks_t osd_ticks_per_second(void) |
| 127 | | { |
| 128 | | if (ticks_per_second == 0) |
| 129 | | { |
| 130 | | // if we haven't computed the value yet, there's no time like the present |
| 131 | | init_cycle_counter(); |
| 132 | | } |
| 133 | | return ticks_per_second; |
| 134 | | } |
| 135 | | |
| 136 | | //============================================================ |
| 137 | | // osd_sleep |
| 138 | | //============================================================ |
| 139 | | |
| 140 | | void osd_sleep(osd_ticks_t duration) |
| 141 | | { |
| 142 | | UINT32 msec; |
| 143 | | |
| 144 | | // make sure we've computed ticks_per_second |
| 145 | | if (ticks_per_second == 0) |
| 146 | | (void)osd_ticks(); |
| 147 | | |
| 148 | | // convert to milliseconds, rounding down |
| 149 | | msec = (UINT32)(duration * 1000 / ticks_per_second); |
| 150 | | |
| 151 | | // only sleep if at least 2 full milliseconds |
| 152 | | if (msec >= 2) |
| 153 | | { |
| 154 | | // take a couple of msecs off the top for good measure |
| 155 | | msec -= 2; |
| 156 | | Sleep(msec); |
| 157 | | } |
| 158 | | } |
| 159 | | |
| 160 | | //============================================================ |
| 161 | 25 | // get_clipboard_text_by_format |
| 162 | 26 | //============================================================ |
| 163 | 27 | |
trunk/src/osd/windows/wintime.c
| r242809 | r242810 | |
| 1 | | // license:BSD-3-Clause |
| 2 | | // copyright-holders:Aaron Giles |
| 3 | | //============================================================ |
| 4 | | // |
| 5 | | // wintime.c - Win32 OSD core timing functions |
| 6 | | // |
| 7 | | //============================================================ |
| 8 | | |
| 9 | | // standard windows headers |
| 10 | | #define WIN32_LEAN_AND_MEAN |
| 11 | | #include <windows.h> |
| 12 | | #include <mmsystem.h> |
| 13 | | |
| 14 | | // MAME headers |
| 15 | | #include "osdcore.h" |
| 16 | | |
| 17 | | |
| 18 | | |
| 19 | | //============================================================ |
| 20 | | // GLOBAL VARIABLES |
| 21 | | //============================================================ |
| 22 | | |
| 23 | | static osd_ticks_t ticks_per_second = 0; |
| 24 | | static osd_ticks_t suspend_ticks = 0; |
| 25 | | static BOOL using_qpc = TRUE; |
| 26 | | |
| 27 | | |
| 28 | | |
| 29 | | //============================================================ |
| 30 | | // osd_ticks |
| 31 | | //============================================================ |
| 32 | | |
| 33 | | osd_ticks_t osd_ticks(void) |
| 34 | | { |
| 35 | | LARGE_INTEGER performance_count; |
| 36 | | |
| 37 | | // if we're suspended, just return that |
| 38 | | if (suspend_ticks != 0) |
| 39 | | return suspend_ticks; |
| 40 | | |
| 41 | | // if we have a per second count, just go for it |
| 42 | | if (ticks_per_second != 0) |
| 43 | | { |
| 44 | | // QueryPerformanceCounter if we can |
| 45 | | if (using_qpc) |
| 46 | | { |
| 47 | | QueryPerformanceCounter(&performance_count); |
| 48 | | return (osd_ticks_t)performance_count.QuadPart - suspend_ticks; |
| 49 | | } |
| 50 | | |
| 51 | | // otherwise, fall back to timeGetTime |
| 52 | | else |
| 53 | | return (osd_ticks_t)timeGetTime() - suspend_ticks; |
| 54 | | } |
| 55 | | |
| 56 | | // if not, we have to determine it |
| 57 | | using_qpc = QueryPerformanceFrequency(&performance_count) && (performance_count.QuadPart != 0); |
| 58 | | if (using_qpc) |
| 59 | | ticks_per_second = (osd_ticks_t)performance_count.QuadPart; |
| 60 | | else |
| 61 | | ticks_per_second = 1000; |
| 62 | | |
| 63 | | // call ourselves to get the first value |
| 64 | | return osd_ticks(); |
| 65 | | } |
| 66 | | |
| 67 | | |
| 68 | | //============================================================ |
| 69 | | // osd_ticks_per_second |
| 70 | | //============================================================ |
| 71 | | |
| 72 | | osd_ticks_t osd_ticks_per_second(void) |
| 73 | | { |
| 74 | | if (ticks_per_second == 0) |
| 75 | | osd_ticks(); |
| 76 | | return ticks_per_second; |
| 77 | | } |
| 78 | | |
| 79 | | |
| 80 | | //============================================================ |
| 81 | | // osd_sleep |
| 82 | | //============================================================ |
| 83 | | |
| 84 | | void osd_sleep(osd_ticks_t duration) |
| 85 | | { |
| 86 | | DWORD msec; |
| 87 | | |
| 88 | | // make sure we've computed ticks_per_second |
| 89 | | if (ticks_per_second == 0) |
| 90 | | (void)osd_ticks(); |
| 91 | | |
| 92 | | // convert to milliseconds, rounding down |
| 93 | | msec = (DWORD)(duration * 1000 / ticks_per_second); |
| 94 | | |
| 95 | | // only sleep if at least 2 full milliseconds |
| 96 | | if (msec >= 2) |
| 97 | | { |
| 98 | | HANDLE current_thread = GetCurrentThread(); |
| 99 | | int old_priority = GetThreadPriority(current_thread); |
| 100 | | |
| 101 | | // take a couple of msecs off the top for good measure |
| 102 | | msec -= 2; |
| 103 | | |
| 104 | | // bump our thread priority super high so that we get |
| 105 | | // priority when we need it |
| 106 | | SetThreadPriority(current_thread, THREAD_PRIORITY_TIME_CRITICAL); |
| 107 | | Sleep(msec); |
| 108 | | SetThreadPriority(current_thread, old_priority); |
| 109 | | } |
| 110 | | } |