~ubuntu-branches/ubuntu/feisty/basilisk2/feisty

« back to all changes in this revision

Viewing changes to src/Unix/timer_unix.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2005-07-30 20:42:20 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050730204220-1nl1cg2jkjvy63ry
Tags: 0.9.20050730-1
* New upstream CVS snapshot.
* Build-depend on virtual libsdl-dev (not libsdl1.2-dev).
* Invoke init rules also on clean (to separate better from official
  builds).
* Update URL of upstream source in debian/copyright.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  timer_unix.cpp - Time Manager emulation, Unix specific stuff
3
3
 *
4
 
 *  Basilisk II (C) 1997-2002 Christian Bauer
 
4
 *  Basilisk II (C) 1997-2005 Christian Bauer
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
228
228
// Linux select() changes its timeout parameter upon return to contain
229
229
// the remaining time. Most other unixen leave it unchanged or undefined.
230
230
#define SELECT_SETS_REMAINING
231
 
#elif defined(__FreeBSD__) || defined(__sun__)
 
231
#elif defined(__FreeBSD__) || defined(__sun__) || (defined(__MACH__) && defined(__APPLE__))
232
232
#define USE_NANOSLEEP
233
233
#elif defined(HAVE_PTHREADS) && defined(sgi)
234
234
// SGI pthreads has a bug when using pthreads+signals+nanosleep,
297
297
#endif
298
298
        } while (was_error && (errno == EINTR));
299
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
        if (idle_sem_ok < 0)
 
337
                idle_sem_ok = (sem_init(&idle_sem, 0, 0) == 0);
 
338
        if (idle_sem_ok > 0) {
 
339
                LOCK_IDLE;
 
340
                idle_sem_ok++;
 
341
                UNLOCK_IDLE;
 
342
                sem_wait(&idle_sem);
 
343
                return;
 
344
        }
 
345
#endif
 
346
        Delay_usec(10000);
 
347
#endif
 
348
}
 
349
 
 
350
 
 
351
/*
 
352
 *  Resume execution of emulator thread, events just arrived
 
353
 */
 
354
 
 
355
void idle_resume(void)
 
356
{
 
357
#ifdef IDLE_USES_COND_WAIT
 
358
        pthread_cond_signal(&idle_cond);
 
359
#else
 
360
#ifdef IDLE_USES_SEMAPHORE
 
361
        if (idle_sem_ok > 1) {
 
362
                LOCK_IDLE;
 
363
                idle_sem_ok--;
 
364
                UNLOCK_IDLE;
 
365
                sem_post(&idle_sem);
 
366
                return;
 
367
        }
 
368
#endif
 
369
#endif
 
370
}