Previous 199869 Revisions Next

r34298 Friday 9th January, 2015 at 23:47:56 UTC by Couriersud
For unix and windows, moved osd_ticks() and friends to
src/osd/modules/lib. For mainline and SDL windows the implementations
slightly differed. Dropped the SDL one in favour of mainline windows
osd_ticks() implementation. (nw)
[src/osd/modules/lib]osdlib_unix.c osdlib_win32.c
[src/osd/sdl]sdlos_unix.c sdlos_win32.c
[src/osd/windows]windows.mak wintime.c

trunk/src/osd/modules/lib/osdlib_unix.c
r242809r242810
44#include <sys/mman.h>
55#include <sys/types.h>
66#include <signal.h>
7#include <time.h>
8#include <sys/time.h>
9#ifdef SDLMAME_EMSCRIPTEN
10#include <emscripten.h>
11#endif
712
13// MAME headers
14#include "osdcore.h"
815#include "osdlib.h"
916
1017//============================================================
r242809r242810
8895#error "MALLOC_DEBUG not yet supported"
8996#endif
9097}
98
99//============================================================
100//   osd_cycles
101//============================================================
102
103osd_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
118osd_ticks_t osd_ticks_per_second(void)
119{
120    return (osd_ticks_t) 1000000;
121}
122
123//============================================================
124//  osd_sleep
125//============================================================
126
127void 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
r242809r242810
66
77#define WIN32_LEAN_AND_MEAN
88#include <windows.h>
9#include <mmsystem.h>
10
911#include <stdlib.h>
1012#ifndef _MSC_VER
1113#include <unistd.h>
r242809r242810
164166    }
165167#endif
166168}
169
170
171//============================================================
172//  GLOBAL VARIABLES
173//============================================================
174
175static osd_ticks_t ticks_per_second = 0;
176static osd_ticks_t suspend_ticks = 0;
177static BOOL using_qpc = TRUE;
178
179
180
181//============================================================
182//  osd_ticks
183//============================================================
184
185osd_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
224osd_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
236void 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
r242809r242810
2626
2727
2828
29//============================================================
30//   osd_cycles
31//============================================================
3229
33osd_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
48osd_ticks_t osd_ticks_per_second(void)
49{
50   return (osd_ticks_t) 1000000;
51}
52
53//============================================================
54//  osd_sleep
55//============================================================
56
57void 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
7330#if (SDLMAME_SDL2)
7431
7532//============================================================
trunk/src/osd/sdl/sdlos_win32.c
r242809r242810
2020#include "osdcore.h"
2121#include "strconv.h"
2222
23//============================================================
24//  PROTOTYPES
25//============================================================
2623
27static osd_ticks_t init_cycle_counter(void);
28static osd_ticks_t performance_cycle_counter(void);
29
3024//============================================================
31//  STATIC VARIABLES
32//============================================================
33
34// global cycle_counter function and divider
35static osd_ticks_t      (*cycle_counter)(void) = init_cycle_counter;
36static osd_ticks_t      (*ticks_counter)(void) = init_cycle_counter;
37static 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
45static 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
105static 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
116osd_ticks_t osd_ticks(void)
117{
118   return (*cycle_counter)();
119}
120
121
122//============================================================
123//  osd_ticks_per_second
124//============================================================
125
126osd_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
140void 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//============================================================
16125//  get_clipboard_text_by_format
16226//============================================================
16327
trunk/src/osd/windows/windows.mak
r242809r242810
350350   $(WINOBJ)/winfile.o \
351351   $(WINOBJ)/winmisc.o \
352352   $(OSDOBJ)/modules/sync/sync_windows.o \
353   $(WINOBJ)/wintime.o \
354353   $(WINOBJ)/winutf8.o \
355354   $(WINOBJ)/winutil.o \
356355   $(WINOBJ)/winclip.o \
trunk/src/osd/windows/wintime.c
r242809r242810
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
23static osd_ticks_t ticks_per_second = 0;
24static osd_ticks_t suspend_ticks = 0;
25static BOOL using_qpc = TRUE;
26
27
28
29//============================================================
30//  osd_ticks
31//============================================================
32
33osd_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
72osd_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
84void 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}


Previous 199869 Revisions Next


© 1997-2024 The MAME Team