ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Windows/timer_windows.cpp
(Generate patch)

Comparing BasiliskII/src/Windows/timer_windows.cpp (file contents):
Revision 1.5 by gbeauche, 2005-06-30T10:20:18Z vs.
Revision 1.7 by gbeauche, 2006-05-01T06:14:08Z

# Line 36 | Line 36
36   #define USECS2TICKS(USECS) (((uint64)(USECS) * frequency) / 1000000)
37   #define TICKS2USECS(TICKS) (((uint64)(TICKS) * 1000000) / frequency)
38  
39 + // From main_windows.cpp
40 + extern HANDLE emul_thread;
41 +
42   // Global variables
43   static uint32 frequency;                                // CPU frequency in Hz (< 4 GHz)
44   static tm_time_t mac_boot_ticks;
# Line 201 | Line 204 | void Delay_usec(uint32 usec)
204   *  Suspend emulator thread, virtual CPU in idle mode
205   */
206  
207 + struct idle_sentinel {
208 +        idle_sentinel();
209 +        ~idle_sentinel();
210 + };
211 + static idle_sentinel idle_sentinel;
212 +
213 + static int idle_sem_ok = -1;
214 + static HANDLE idle_sem = NULL;
215 +
216 + static HANDLE idle_lock = NULL;
217 + #define LOCK_IDLE WaitForSingleObject(idle_lock, INFINITE)
218 + #define UNLOCK_IDLE ReleaseMutex(idle_lock)
219 +
220 + idle_sentinel::idle_sentinel()
221 + {
222 +        idle_sem_ok = 1;
223 +        if ((idle_sem = CreateSemaphore(0, 0, 1, NULL)) == NULL)
224 +                idle_sem_ok = 0;
225 +        if ((idle_lock = CreateMutex(NULL, FALSE, NULL)) == NULL)
226 +                idle_sem_ok = 0;
227 + }
228 +
229 + idle_sentinel::~idle_sentinel()
230 + {
231 +        if (idle_lock) {
232 +                ReleaseMutex(idle_lock);
233 +                CloseHandle(idle_lock);
234 +        }
235 +        if (idle_sem) {
236 +                ReleaseSemaphore(idle_sem, 1, NULL);
237 +                CloseHandle(idle_sem);
238 +        }
239 + }
240 +
241   void idle_wait(void)
242   {
243 +        LOCK_IDLE;
244 +        if (idle_sem_ok > 0) {
245 +                idle_sem_ok++;
246 +                UNLOCK_IDLE;
247 +                WaitForSingleObject(idle_sem, INFINITE);
248 +                return;
249 +        }
250 +        UNLOCK_IDLE;
251 +
252 +        // Fallback: sleep 10 ms (this should not happen though)
253          Delay_usec(10000);
254   }
255  
# Line 213 | Line 260 | void idle_wait(void)
260  
261   void idle_resume(void)
262   {
263 +        LOCK_IDLE;
264 +        if (idle_sem_ok > 1) {
265 +                idle_sem_ok--;
266 +                UNLOCK_IDLE;
267 +                ReleaseSemaphore(idle_sem, 1, NULL);
268 +                return;
269 +        }
270 +        UNLOCK_IDLE;
271   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines