Hello,
I’m trying to port itimer procedures to SDL_Timer so they can run on win32
and other system sdl supports. I wrote this procedure:-----------
#include <SDL.h>
static Uint32 timerID;
Uint32 timer_signal(Uint32 interval, void *param);
SDL_mutex *mut;
int awake_signal;
int
timer_init( void )
{
SDL_InitSubSystem( SDL_INIT_TIMER );
mut=SDL_CreateMutex();
timerID = SDL_AddTimer(80, timer_signal, NULL);
if( !timerID ) return 1;
return 0;
}
void
timer_sleep( void )
{
int is_break = 0;
while( timer_count <= 0.0 ) {
if(SDL_mutexP(mut)==-1){
fprintf(stderr, "Couldn't lock mutex\n");
exit(-1);
}
awake_signal = 1;
if(SDL_mutexV(mut)==-1){
fprintf(stderr, "Couldn't unlock mutex\n");
exit(-1);
}
is_break = 0;
while (awake_signal == 1) {
if(SDL_mutexP(mut)==-1){
fprintf(stderr, "Couldn't lock mutex\n");
exit(-1);
}
if (awake_signal == 0) is_break = 1;
if(SDL_mutexV(mut)==-1){
fprintf(stderr, "Couldn't unlock mutex\n");
exit(-1);
}
if (is_break) break;
}
}
}
int
timer_end( void )
{
SDL_RemoveTimer(timerID);
SDL_DestroyMutex(mut);
SDL_QuitSubSystem( SDL_INIT_TIMER );
return 0;
}
Uint32 timer_signal(Uint32 interval, void *param)
{
if(SDL_mutexP(mut)==-1){
fprintf(stderr, “Couldn’t lock mutex\n”);
exit(-1);
}
awake_signal = 0;
if(SDL_mutexV(mut)==-1){
fprintf(stderr, “Couldn’t unlock mutex\n”);
exit(-1);
}
if( !fuse_emulation_paused ) timer_count += 1.0;
return interval;
}
the problem is that it runs almost fine on win32, but doesn’t on linux.
I’m trying to replace the following unix procedures.
other problem is that the with SDL procedures 4 times less than native
linux
procedure (itimer is set to run every 20 ms, SDL Timer is run every 80 ms).
static struct itimerval timer_old_timer;
static struct sigaction timer_old_handler;
static void timer_setup_timer(void);
static void timer_setup_handler(void);
void timer_signal( int signo );
int timer_init(void)
{
timer_count = 0.0;
timer_setup_handler();
timer_setup_timer();
return 0;
}
static void timer_setup_timer(void)
{
struct itimerval timer;
timer.it_interval.tv_sec=0;
timer.it_interval.tv_usec=20000;
timer.it_value.tv_sec=0;
timer.it_value.tv_usec=20000;
setitimer(ITIMER_REAL,&timer,&timer_old_timer);
}
static void timer_setup_handler(void)
{
struct sigaction handler;
handler.sa_handler=timer_signal;
sigemptyset(&handler.sa_mask);
handler.sa_flags=0;
sigaction(SIGALRM,&handler,&timer_old_handler);
}
void
timer_signal( int signo GCC_UNUSED )
{
/* If the emulator is running, note that time has passed */
if( !fuse_emulation_paused ) timer_count += 1.0;
}
void timer_sleep(void)
{
/* Go to sleep iff we’re emulating things fast enough */
while( timer_count <= 0.0 ) pause();
}
int timer_end(void)
{
/* Restore the old timer */
setitimer(ITIMER_REAL,&timer_old_timer,NULL);
/* And the old signal handler */
sigaction(SIGALRM,&timer_old_handler,NULL);
return 0;
}
Any thoughts on that?
Thanks in advance,
Marek