src/timer/riscos/SDL_systimer.c
changeset 1895 c121d94672cb
parent 1635 92947e3a18db
child 2669 e27bdcc80744
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
    50 extern void RISCOS_BackgroundTasks(void);
    50 extern void RISCOS_BackgroundTasks(void);
    51 
    51 
    52 /* The first ticks value of the application */
    52 /* The first ticks value of the application */
    53 clock_t start;
    53 clock_t start;
    54 
    54 
    55 void SDL_StartTicks(void)
    55 void
    56 {
    56 SDL_StartTicks(void)
    57 	/* Set first ticks value */
    57 {
    58 	start = clock();
    58     /* Set first ticks value */
    59 }
    59     start = clock();
    60 
    60 }
    61 Uint32 SDL_GetTicks (void)
    61 
    62 {
    62 Uint32
    63 	clock_t ticks;
    63 SDL_GetTicks(void)
    64 
    64 {
    65 	ticks=clock()-start;
    65     clock_t ticks;
       
    66 
       
    67     ticks = clock() - start;
    66 
    68 
    67 
    69 
    68 #if CLOCKS_PER_SEC == 1000
    70 #if CLOCKS_PER_SEC == 1000
    69 
    71 
    70 	return(ticks);
    72     return (ticks);
    71 
    73 
    72 #elif CLOCKS_PER_SEC == 100
    74 #elif CLOCKS_PER_SEC == 100
    73 
    75 
    74 	return (ticks * 10);
    76     return (ticks * 10);
    75 
    77 
    76 #else
    78 #else
    77 
    79 
    78 	return ticks*(1000/CLOCKS_PER_SEC);
    80     return ticks * (1000 / CLOCKS_PER_SEC);
    79 
    81 
    80 #endif
    82 #endif
    81 
    83 
    82 }
    84 }
    83 
    85 
    84 void SDL_Delay (Uint32 ms)
    86 void
    85 {
    87 SDL_Delay(Uint32 ms)
    86     Uint32 now,then,elapsed;
    88 {
       
    89     Uint32 now, then, elapsed;
    87 #if !SDL_THREADS_DISABLED
    90 #if !SDL_THREADS_DISABLED
    88     int is_event_thread;
    91     int is_event_thread;
    89     if (riscos_using_threads)
    92     if (riscos_using_threads) {
    90     {
    93         is_event_thread = 0;
    91        is_event_thread = 0;
    94         if (SDL_EventThreadID()) {
    92        if (SDL_EventThreadID())
    95             if (SDL_EventThreadID() == SDL_ThreadID())
    93        {
    96                 is_event_thread = 1;
    94           if (SDL_EventThreadID() == SDL_ThreadID()) is_event_thread = 1;
    97         } else if (SDL_ThreadID() == riscos_main_thread)
    95        } else if (SDL_ThreadID() == riscos_main_thread) is_event_thread = 1;
    98             is_event_thread = 1;
    96     } else is_event_thread = 1;
    99     } else
    97 #endif
   100         is_event_thread = 1;
    98 
   101 #endif
    99         /*TODO: Next version of Unixlib may allow us to use usleep here */
   102 
   100         /*      for non event threads */
   103     /*TODO: Next version of Unixlib may allow us to use usleep here */
   101 
   104     /*      for non event threads */
   102 	/* Set the timeout interval - Linux only needs to do this once */
   105 
   103 	then = SDL_GetTicks();
   106     /* Set the timeout interval - Linux only needs to do this once */
   104 
   107     then = SDL_GetTicks();
   105 	do {
   108 
   106 		/* Do background tasks required while sleeping as we are not multithreaded */
   109     do {
       
   110         /* Do background tasks required while sleeping as we are not multithreaded */
   107 #if SDL_THREADS_DISABLED
   111 #if SDL_THREADS_DISABLED
   108 		RISCOS_BackgroundTasks();
   112         RISCOS_BackgroundTasks();
   109 #else
   113 #else
   110 		/* For threaded build only run background tasks in event thread */
   114         /* For threaded build only run background tasks in event thread */
   111 		if (is_event_thread) RISCOS_BackgroundTasks();
   115         if (is_event_thread)
   112 #endif
   116             RISCOS_BackgroundTasks();
   113 
   117 #endif
   114 		/* Calculate the time interval left (in case of interrupt) */
   118 
   115 		now = SDL_GetTicks();
   119         /* Calculate the time interval left (in case of interrupt) */
   116 		elapsed = (now-then);
   120         now = SDL_GetTicks();
   117 		then = now;
   121         elapsed = (now - then);
   118 		if ( elapsed >= ms ) {
   122         then = now;
   119 			break;
   123         if (elapsed >= ms) {
   120 		}
   124             break;
   121 		ms -= elapsed;
   125         }
       
   126         ms -= elapsed;
   122 #if !SDL_THREADS_DISABLED
   127 #if !SDL_THREADS_DISABLED
   123             /* Need to yield to let other threads have a go */
   128         /* Need to yield to let other threads have a go */
   124             if (riscos_using_threads) pthread_yield();
   129         if (riscos_using_threads)
   125 #endif
   130             pthread_yield();
   126 
   131 #endif
   127 	} while ( 1 );
   132 
       
   133     }
       
   134     while (1);
   128 }
   135 }
   129 
   136 
   130 #if SDL_THREADS_DISABLED
   137 #if SDL_THREADS_DISABLED
   131 
   138 
   132 /* Non-threaded version of timer */
   139 /* Non-threaded version of timer */
   133 
   140 
   134 int SDL_SYS_TimerInit(void)
   141 int
   135 {
   142 SDL_SYS_TimerInit(void)
   136 	return(0);
   143 {
   137 }
   144     return (0);
   138 
   145 }
   139 void SDL_SYS_TimerQuit(void)
   146 
   140 {
   147 void
   141 	SDL_SetTimer(0, NULL);
   148 SDL_SYS_TimerQuit(void)
   142 }
   149 {
   143 
   150     SDL_SetTimer(0, NULL);
   144 int SDL_SYS_StartTimer(void)
   151 }
   145 {
   152 
   146 	timerStart = SDL_GetTicks();
   153 int
   147 
   154 SDL_SYS_StartTimer(void)
   148 	return(0);
   155 {
   149 }
   156     timerStart = SDL_GetTicks();
   150 
   157 
   151 void SDL_SYS_StopTimer(void)
   158     return (0);
   152 {
   159 }
   153 	/* Don't need to do anything as we use SDL_timer_running
   160 
   154 	   to detect if we need to check the timer */
   161 void
   155 }
   162 SDL_SYS_StopTimer(void)
   156 
   163 {
   157 
   164     /* Don't need to do anything as we use SDL_timer_running
   158 void RISCOS_CheckTimer()
   165        to detect if we need to check the timer */
   159 {
   166 }
   160 	if (SDL_timer_running && SDL_GetTicks() - timerStart >= SDL_alarm_interval)
   167 
   161 	{
   168 
   162 		Uint32 ms;
   169 void
   163 
   170 RISCOS_CheckTimer()
   164 		ms = SDL_alarm_callback(SDL_alarm_interval);
   171 {
   165 		if ( ms != SDL_alarm_interval )
   172     if (SDL_timer_running
   166 		{
   173         && SDL_GetTicks() - timerStart >= SDL_alarm_interval) {
   167 			if ( ms )
   174         Uint32 ms;
   168 			{
   175 
   169 				SDL_alarm_interval = ROUND_RESOLUTION(ms);
   176         ms = SDL_alarm_callback(SDL_alarm_interval);
   170 			} else
   177         if (ms != SDL_alarm_interval) {
   171 			{
   178             if (ms) {
   172 				SDL_alarm_interval = 0;
   179                 SDL_alarm_interval = ROUND_RESOLUTION(ms);
   173 				SDL_timer_running = 0;
   180             } else {
   174 			}
   181                 SDL_alarm_interval = 0;
   175 		}
   182                 SDL_timer_running = 0;
   176 		if (SDL_alarm_interval) timerStart = SDL_GetTicks();
   183             }
   177 	}
   184         }
       
   185         if (SDL_alarm_interval)
       
   186             timerStart = SDL_GetTicks();
       
   187     }
   178 }
   188 }
   179 
   189 
   180 #else
   190 #else
   181 
   191 
   182 /* Threaded version of timer - based on code for linux */
   192 /* Threaded version of timer - based on code for linux */
   185 
   195 
   186 /* Data to handle a single periodic alarm */
   196 /* Data to handle a single periodic alarm */
   187 static int timer_alive = 0;
   197 static int timer_alive = 0;
   188 static SDL_Thread *timer = NULL;
   198 static SDL_Thread *timer = NULL;
   189 
   199 
   190 static int RunTimer(void *unused)
   200 static int
   191 {
   201 RunTimer(void *unused)
   192 	while ( timer_alive ) {
   202 {
   193 		if ( SDL_timer_running ) {
   203     while (timer_alive) {
   194 			SDL_ThreadedTimerCheck();
   204         if (SDL_timer_running) {
   195 		}
   205             SDL_ThreadedTimerCheck();
   196 		SDL_Delay(1);
   206         }
   197 	}
   207         SDL_Delay(1);
   198 	return(0);
   208     }
       
   209     return (0);
   199 }
   210 }
   200 
   211 
   201 /* This is only called if the event thread is not running */
   212 /* This is only called if the event thread is not running */
   202 int SDL_SYS_TimerInit(void)
   213 int
   203 {
   214 SDL_SYS_TimerInit(void)
   204 	timer_alive = 1;
   215 {
   205 	timer = SDL_CreateThread(RunTimer, NULL);
   216     timer_alive = 1;
   206 	if ( timer == NULL )
   217     timer = SDL_CreateThread(RunTimer, NULL);
   207 		return(-1);
   218     if (timer == NULL)
   208 	return(SDL_SetTimerThreaded(1));
   219         return (-1);
   209 }
   220     return (SDL_SetTimerThreaded(1));
   210 
   221 }
   211 void SDL_SYS_TimerQuit(void)
   222 
   212 {
   223 void
   213 	timer_alive = 0;
   224 SDL_SYS_TimerQuit(void)
   214 	if ( timer ) {
   225 {
   215 		SDL_WaitThread(timer, NULL);
   226     timer_alive = 0;
   216 		timer = NULL;
   227     if (timer) {
   217 	}
   228         SDL_WaitThread(timer, NULL);
   218 }
   229         timer = NULL;
   219 
   230     }
   220 int SDL_SYS_StartTimer(void)
   231 }
   221 {
   232 
   222 	SDL_SetError("Internal logic error: RISC OS uses threaded timer");
   233 int
   223 	return(-1);
   234 SDL_SYS_StartTimer(void)
   224 }
   235 {
   225 
   236     SDL_SetError("Internal logic error: RISC OS uses threaded timer");
   226 void SDL_SYS_StopTimer(void)
   237     return (-1);
   227 {
   238 }
   228 	return;
   239 
       
   240 void
       
   241 SDL_SYS_StopTimer(void)
       
   242 {
       
   243     return;
   229 }
   244 }
   230 
   245 
   231 #endif /* SDL_THREADS_DISABLED */
   246 #endif /* SDL_THREADS_DISABLED */
   232 
   247 
   233 #endif /* SDL_TIMER_RISCOS */
   248 #endif /* SDL_TIMER_RISCOS */
       
   249 /* vi: set ts=4 sw=4 expandtab: */