Previous 199869 Revisions Next

r34299 Saturday 10th January, 2015 at 00:12:58 UTC by Couriersud
Moved OSX osd_sleep and friends to modules/lib (nw)
[src/osd/modules/lib]osdlib_macosx.c
[src/osd/sdl]sdlos_macosx.c

trunk/src/osd/modules/lib/osdlib_macosx.c
r242810r242811
66
77#include <mach/mach.h>
88#include <mach/mach_time.h>
9#include <mach/mach_traps.h>
910#include <Carbon/Carbon.h>
1011
1112// MAME headers
13#include "osdcore.h"
1214#include "osdlib.h"
1315
16// FIXME: We shouldn't use SDL functions in here
17
18#include "sdlinc.h"
19
1420//============================================================
1521//  osd_process_kill
1622//============================================================
r242810r242811
102108{
103109    return setenv(name, value, overwrite);
104110}
111
112//============================================================
113//  PROTOTYPES
114//============================================================
115
116static osd_ticks_t init_cycle_counter(void);
117static osd_ticks_t mach_cycle_counter(void);
118
119//============================================================
120//  STATIC VARIABLES
121//============================================================
122
123static osd_ticks_t      (*cycle_counter)(void) = init_cycle_counter;
124static osd_ticks_t      (*ticks_counter)(void) = init_cycle_counter;
125static osd_ticks_t      ticks_per_second;
126
127//============================================================
128//  init_cycle_counter
129//
130//  to avoid total grossness, this function is split by subarch
131//============================================================
132
133static osd_ticks_t init_cycle_counter(void)
134{
135    osd_ticks_t start, end;
136    osd_ticks_t a, b;
137
138    cycle_counter = mach_cycle_counter;
139    ticks_counter = mach_cycle_counter;
140
141    // wait for an edge on the timeGetTime call
142    a = SDL_GetTicks();
143    do
144    {
145        b = SDL_GetTicks();
146    } while (a == b);
147
148    // get the starting cycle count
149    start = (*cycle_counter)();
150
151    // now wait for 1/4 second total
152    do
153    {
154        a = SDL_GetTicks();
155    } while (a - b < 250);
156
157    // get the ending cycle count
158    end = (*cycle_counter)();
159
160    // compute ticks_per_sec
161    ticks_per_second = (end - start) * 4;
162
163    // return the current cycle count
164    return (*cycle_counter)();
165}
166
167//============================================================
168//  performance_cycle_counter
169//============================================================
170
171//============================================================
172//  mach_cycle_counter
173//============================================================
174static osd_ticks_t mach_cycle_counter(void)
175{
176    return mach_absolute_time();
177}
178
179//============================================================
180//   osd_cycles
181//============================================================
182
183osd_ticks_t osd_ticks(void)
184{
185    return (*cycle_counter)();
186}
187
188
189//============================================================
190//  osd_ticks_per_second
191//============================================================
192
193osd_ticks_t osd_ticks_per_second(void)
194{
195    if (ticks_per_second == 0)
196    {
197        // if we haven't computed the value yet, there's no time like the present
198        init_cycle_counter();
199    }
200    return ticks_per_second;
201}
202
203
204
205//============================================================
206//  osd_sleep
207//============================================================
208
209void osd_sleep(osd_ticks_t duration)
210{
211    UINT32 msec;
212
213    // make sure we've computed ticks_per_second
214    if (ticks_per_second == 0)
215        (void)osd_ticks();
216
217    // convert to milliseconds, rounding down
218    msec = (UINT32)(duration * 1000 / ticks_per_second);
219
220    // only sleep if at least 2 full milliseconds
221    if (msec >= 2)
222    {
223        // take a couple of msecs off the top for good measure
224        msec -= 2;
225        usleep(msec*1000);
226    }
227}
228
trunk/src/osd/sdl/sdlos_macosx.c
r242810r242811
2222// MAME headers
2323#include "osdcore.h"
2424
25//============================================================
26//  PROTOTYPES
27//============================================================
2825
29static osd_ticks_t init_cycle_counter(void);
30static osd_ticks_t mach_cycle_counter(void);
31
3226//============================================================
33//  STATIC VARIABLES
34//============================================================
35
36static osd_ticks_t      (*cycle_counter)(void) = init_cycle_counter;
37static osd_ticks_t      (*ticks_counter)(void) = init_cycle_counter;
38static osd_ticks_t      ticks_per_second;
39
40//============================================================
41//  init_cycle_counter
42//
43//  to avoid total grossness, this function is split by subarch
44//============================================================
45
46static osd_ticks_t init_cycle_counter(void)
47{
48   osd_ticks_t start, end;
49   osd_ticks_t a, b;
50
51   cycle_counter = mach_cycle_counter;
52   ticks_counter = mach_cycle_counter;
53
54   // wait for an edge on the timeGetTime call
55   a = SDL_GetTicks();
56   do
57   {
58      b = SDL_GetTicks();
59   } while (a == b);
60
61   // get the starting cycle count
62   start = (*cycle_counter)();
63
64   // now wait for 1/4 second total
65   do
66   {
67      a = SDL_GetTicks();
68   } while (a - b < 250);
69
70   // get the ending cycle count
71   end = (*cycle_counter)();
72
73   // compute ticks_per_sec
74   ticks_per_second = (end - start) * 4;
75
76   // return the current cycle count
77   return (*cycle_counter)();
78}
79
80//============================================================
81//  performance_cycle_counter
82//============================================================
83
84//============================================================
85//  mach_cycle_counter
86//============================================================
87static osd_ticks_t mach_cycle_counter(void)
88{
89   return mach_absolute_time();
90}
91
92//============================================================
93//   osd_cycles
94//============================================================
95
96osd_ticks_t osd_ticks(void)
97{
98   return (*cycle_counter)();
99}
100
101
102//============================================================
103//  osd_ticks_per_second
104//============================================================
105
106osd_ticks_t osd_ticks_per_second(void)
107{
108   if (ticks_per_second == 0)
109   {
110      // if we haven't computed the value yet, there's no time like the present
111      init_cycle_counter();
112   }
113   return ticks_per_second;
114}
115
116
117
118//============================================================
119//  osd_sleep
120//============================================================
121
122void osd_sleep(osd_ticks_t duration)
123{
124   UINT32 msec;
125
126   // make sure we've computed ticks_per_second
127   if (ticks_per_second == 0)
128      (void)osd_ticks();
129
130   // convert to milliseconds, rounding down
131   msec = (UINT32)(duration * 1000 / ticks_per_second);
132
133   // only sleep if at least 2 full milliseconds
134   if (msec >= 2)
135   {
136      // take a couple of msecs off the top for good measure
137      msec -= 2;
138      usleep(msec*1000);
139   }
140}
141
142
143//============================================================
14427//  osd_get_clipboard_text
14528//============================================================
14629


Previous 199869 Revisions Next


© 1997-2024 The MAME Team