src/timer/riscos/SDL_systimer.c
changeset 955 d74fbf56f2f6
parent 769 b8d311d90021
child 1035 974ba6ae0fa3
     1.1 --- a/src/timer/riscos/SDL_systimer.c	Sat Sep 11 18:00:41 2004 +0000
     1.2 +++ b/src/timer/riscos/SDL_systimer.c	Fri Sep 17 13:20:10 2004 +0000
     1.3 @@ -36,10 +36,19 @@
     1.4  #include "SDL_timer.h"
     1.5  #include "SDL_timer_c.h"
     1.6  
     1.7 +#ifdef DISABLE_THREADS
     1.8  /* Timer start/reset time */
     1.9  static Uint32 timerStart;
    1.10  /* Timer running function */
    1.11  void RISCOS_CheckTimer();
    1.12 +#else
    1.13 +#include <pthread.h>
    1.14 +extern Uint32 riscos_main_thread;
    1.15 +extern int riscos_using_threads;
    1.16 +extern Uint32 SDL_ThreadID();
    1.17 +extern Uint32 SDL_EventThreadID(void);
    1.18 +#endif
    1.19 +
    1.20  
    1.21  extern void RISCOS_BackgroundTasks(void);
    1.22  
    1.23 @@ -75,18 +84,36 @@
    1.24  
    1.25  }
    1.26  
    1.27 -extern void DRenderer_FillBuffers();
    1.28 -
    1.29  void SDL_Delay (Uint32 ms)
    1.30  {
    1.31      Uint32 now,then,elapsed;
    1.32 +#ifndef DISABLE_THREADS
    1.33 +    int is_event_thread;
    1.34 +    if (riscos_using_threads)
    1.35 +    {
    1.36 +       is_event_thread = 0;
    1.37 +       if (SDL_EventThreadID())
    1.38 +       {
    1.39 +          if (SDL_EventThreadID() == SDL_ThreadID()) is_event_thread = 1;
    1.40 +       } else if (SDL_ThreadID() == riscos_main_thread) is_event_thread = 1;
    1.41 +    } else is_event_thread = 1;
    1.42 +#endif
    1.43 +
    1.44 +/*TODO: Next version of unixlib may allow us to use usleep here */
    1.45 +/*      for non event threads */
    1.46  
    1.47  	/* Set the timeout interval - Linux only needs to do this once */
    1.48  	then = SDL_GetTicks();
    1.49  
    1.50  	do {
    1.51  		/* Do background tasks required while sleeping as we are not multithreaded */
    1.52 +#ifdef DISABLE_THREADS
    1.53  		RISCOS_BackgroundTasks();
    1.54 +#else
    1.55 +		/* For threaded build only run background tasks in event thread */
    1.56 +		if (is_event_thread) RISCOS_BackgroundTasks();
    1.57 +#endif
    1.58 +
    1.59  		/* Calculate the time interval left (in case of interrupt) */
    1.60  		now = SDL_GetTicks();
    1.61  		elapsed = (now-then);
    1.62 @@ -95,10 +122,18 @@
    1.63  			break;
    1.64  		}
    1.65  		ms -= elapsed;
    1.66 +#ifndef DISABLE_THREADS
    1.67 +            /* Need to yield to let other threads have a go */
    1.68 +            if (riscos_using_threads) pthread_yield();
    1.69 +#endif
    1.70  
    1.71  	} while ( 1 );
    1.72  }
    1.73  
    1.74 +#ifdef DISABLE_THREADS
    1.75 +
    1.76 +/* Non-threaded version of timer */
    1.77 +
    1.78  int SDL_SYS_TimerInit(void)
    1.79  {
    1.80  	return(0);
    1.81 @@ -144,3 +179,56 @@
    1.82  		if (SDL_alarm_interval) timerStart = SDL_GetTicks();
    1.83  	}
    1.84  }
    1.85 +
    1.86 +#else
    1.87 +
    1.88 +/* Threaded version of timer - based on code for linux */
    1.89 +
    1.90 +#include "SDL_thread.h"
    1.91 +
    1.92 +/* Data to handle a single periodic alarm */
    1.93 +static int timer_alive = 0;
    1.94 +static SDL_Thread *timer = NULL;
    1.95 +
    1.96 +static int RunTimer(void *unused)
    1.97 +{
    1.98 +	while ( timer_alive ) {
    1.99 +		if ( SDL_timer_running ) {
   1.100 +			SDL_ThreadedTimerCheck();
   1.101 +		}
   1.102 +		SDL_Delay(1);
   1.103 +	}
   1.104 +	return(0);
   1.105 +}
   1.106 +
   1.107 +/* This is only called if the event thread is not running */
   1.108 +int SDL_SYS_TimerInit(void)
   1.109 +{
   1.110 +	timer_alive = 1;
   1.111 +	timer = SDL_CreateThread(RunTimer, NULL);
   1.112 +	if ( timer == NULL )
   1.113 +		return(-1);
   1.114 +	return(SDL_SetTimerThreaded(1));
   1.115 +}
   1.116 +
   1.117 +void SDL_SYS_TimerQuit(void)
   1.118 +{
   1.119 +	timer_alive = 0;
   1.120 +	if ( timer ) {
   1.121 +		SDL_WaitThread(timer, NULL);
   1.122 +		timer = NULL;
   1.123 +	}
   1.124 +}
   1.125 +
   1.126 +int SDL_SYS_StartTimer(void)
   1.127 +{
   1.128 +	SDL_SetError("Internal logic error: RISCOS uses threaded timer");
   1.129 +	return(-1);
   1.130 +}
   1.131 +
   1.132 +void SDL_SYS_StopTimer(void)
   1.133 +{
   1.134 +	return;
   1.135 +}
   1.136 +
   1.137 +#endif /* DISABLE_THREADS */