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

Comparing BasiliskII/src/Unix/timer_unix.cpp (file contents):
Revision 1.12 by cebix, 2001-07-09T15:44:58Z vs.
Revision 1.19 by gbeauche, 2006-05-01T13:13:18Z

# Line 1 | Line 1
1   /*
2   *  timer_unix.cpp - Time Manager emulation, Unix specific stuff
3   *
4 < *  Basilisk II (C) 1997-2001 Christian Bauer
4 > *  Basilisk II (C) 1997-2005 Christian Bauer
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 228 | Line 228 | uint64 GetTicks_usec(void)
228   // Linux select() changes its timeout parameter upon return to contain
229   // the remaining time. Most other unixen leave it unchanged or undefined.
230   #define SELECT_SETS_REMAINING
231 < #elif defined(__FreeBSD__) || defined(__sun__)
231 > #elif defined(__FreeBSD__) || defined(__sun__) || (defined(__MACH__) && defined(__APPLE__))
232   #define USE_NANOSLEEP
233   #elif defined(HAVE_PTHREADS) && defined(sgi)
234   // SGI pthreads has a bug when using pthreads+signals+nanosleep,
# Line 297 | Line 297 | void Delay_usec(uint32 usec)
297   #endif
298          } while (was_error && (errno == EINTR));
299   }
300 +
301 +
302 + /*
303 + *  Suspend emulator thread, virtual CPU in idle mode
304 + */
305 +
306 + #ifdef HAVE_PTHREADS
307 + #if defined(HAVE_PTHREAD_COND_INIT)
308 + #define IDLE_USES_COND_WAIT 1
309 + static pthread_mutex_t idle_lock = PTHREAD_MUTEX_INITIALIZER;
310 + static pthread_cond_t idle_cond = PTHREAD_COND_INITIALIZER;
311 + #elif defined(HAVE_SEM_INIT)
312 + #define IDLE_USES_SEMAPHORE 1
313 + #include <semaphore.h>
314 + #ifdef HAVE_SPINLOCKS
315 + static spinlock_t idle_lock = SPIN_LOCK_UNLOCKED;
316 + #define LOCK_IDLE spin_lock(&idle_lock)
317 + #define UNLOCK_IDLE spin_unlock(&idle_lock)
318 + #else
319 + static pthread_mutex_t idle_lock = PTHREAD_MUTEX_INITIALIZER;
320 + #define LOCK_IDLE pthread_mutex_lock(&idle_lock)
321 + #define UNLOCK_IDLE pthread_mutex_unlock(&idle_lock)
322 + #endif
323 + static sem_t idle_sem;
324 + static int idle_sem_ok = -1;
325 + #endif
326 + #endif
327 +
328 + void idle_wait(void)
329 + {
330 + #ifdef IDLE_USES_COND_WAIT
331 +        pthread_mutex_lock(&idle_lock);
332 +        pthread_cond_wait(&idle_cond, &idle_lock);
333 +        pthread_mutex_unlock(&idle_lock);
334 + #else
335 + #ifdef IDLE_USES_SEMAPHORE
336 +        LOCK_IDLE;
337 +        if (idle_sem_ok < 0)
338 +                idle_sem_ok = (sem_init(&idle_sem, 0, 0) == 0);
339 +        if (idle_sem_ok > 0) {
340 +                idle_sem_ok++;
341 +                UNLOCK_IDLE;
342 +                sem_wait(&idle_sem);
343 +                return;
344 +        }
345 +        UNLOCK_IDLE;
346 + #endif
347 +
348 +        // Fallback: sleep 10 ms
349 +        Delay_usec(10000);
350 + #endif
351 + }
352 +
353 +
354 + /*
355 + *  Resume execution of emulator thread, events just arrived
356 + */
357 +
358 + void idle_resume(void)
359 + {
360 + #ifdef IDLE_USES_COND_WAIT
361 +        pthread_cond_signal(&idle_cond);
362 + #else
363 + #ifdef IDLE_USES_SEMAPHORE
364 +        LOCK_IDLE;
365 +        if (idle_sem_ok > 1) {
366 +                idle_sem_ok--;
367 +                UNLOCK_IDLE;
368 +                sem_post(&idle_sem);
369 +                return;
370 +        }
371 +        UNLOCK_IDLE;
372 + #endif
373 + #endif
374 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines