Files
my_glut/sources/glut_timers.c
2026-03-13 16:48:03 +01:00

171 lines
4.3 KiB
C

#include <stdlib.h>
#include "../headers/glut_timers.h"
#include <stdio.h>
static int is_updating_time=0;
static timer_list_entry *timers_first = NULL;
static timer_list_entry *timers_last = NULL;
static timer_list_entry *unissued_timers_first = NULL;
static timer_list_entry *unissued_timers_last = NULL;
/*CODE duplication*/
void timers_add_to_unissued(timer_list_entry *toAdd){
//printf("timers add to unissued begin\n");
if(unissued_timers_last != NULL){
unissued_timers_last->next = toAdd;
}
else{
if(NULL == unissued_timers_first){
unissued_timers_first=toAdd;
}
}
unissued_timers_last = toAdd;
//printf("timers add to unissued end\n");
}
void timers_add(timer_list_entry *toAdd){
//printf("timers add begin\n");
if(is_updating_time){
timers_add_to_unissued( toAdd);
return;
}
if(timers_last != NULL){
timers_last->next = toAdd;
}
else{
if(NULL == timers_first){
timers_first=toAdd;
}
}
timers_last = toAdd;
//printf("timers add end\n");
}
/**
FULL OF PRINT STATEMENTS DUE TO A BUG MYSTERIOUSLY FIXED, see decrease_timers()
*/
/* unsafe, not checking the validity of the previous, toRemove precondition */
void timers_remove(
timer_list_entry *previous,
timer_list_entry *toRemove
){
/*
printf("timers remove\n");
printf("timers remove previous is %p\n", (void*) previous);
printf("timers remove toRemove is %p\n", (void*) toRemove);
if(NULL != previous){
printf("timers remove previous->next is %p\n", (void*) previous->next);
if(previous->next != toRemove){
printf("timers remove previous->next is not the same as toRemove\n");
}
}
else{
printf("previous counts as NULL\n");
}
if(toRemove->next == NULL){
printf("timers remove toRemove->next is NULL\n");
}
else{
printf("timers remove toRemove->next is %p\n", (void*) toRemove->next);
}
*/
if( toRemove == timers_first){
// printf("timers remove toremove is the first\n");
timers_first = timers_first->next;
if(NULL == timers_first){
timers_last = NULL;
}
}
else{
// printf("timers remove toremove is not the first\n");
previous->next = toRemove->next;
/* could move elsewhere, timer_last is an auxiliary variable */
if( NULL == previous->next){
timers_last = previous;
}
// printf("timers remove previous->next is now %p\n", (void*) previous->next);
}
/* i added this and now works. should not need it, smells like bigger bug */
toRemove->next = NULL;
// printf("timers remove end\n");
}
void append_unissued_timers(){
if( NULL == unissued_timers_first){
return;
}
timers_add(unissued_timers_first);
timers_last=unissued_timers_last;
unissued_timers_first = NULL;
unissued_timers_last = NULL;
}
/**
FULL OF PRINT STATEMENTS DUE TO A BUG MYSTERIOUSLY FIXED
*/
void decrease_timers(){
timer_list_entry *current, *previous;
previous = NULL;
int count = 0; /*DEBUG ONLY*/
current = timers_first;
is_updating_time=1;
//printf("decrease timers begin\n");
while( current != NULL){
/*
printf("decreasing timer\n");
printf("timer addr would be %p\n", (void*) current);
*/
current->remaining_ms--;
//printf("decreased timer\n");
if( 0 >= current->remaining_ms){
//printf("will crash on removal?\n");
timers_remove( previous, current);
//printf("removed timer\nbreak on callback?\n");
current->callback(0); /*should receive an int (i guess the runover time)*/
//printf("callback done\nbreak on free?\n");
free( current);
//printf("was not the free\n");
/* would make a removal from head inconsistent */
if(previous != NULL){
//printf("previous is not null during the looping, sliding\n");
current=previous;
}
}
/*
printf("updating cursors\n");
printf("previous is %p\n", (void*) previous);
printf("current is %p\n", (void*) current);
*/
previous = current;
current = current->next;
/*
printf("updated cursors\n");
printf("previous is %p\n", (void*) previous);
printf("current is %p\n", (void*) current);
*/
count++;
// printf("wasn't even the counter\n");
}
// printf("out of the timers loop\n");
/*
printf("processed %d timers\n", count);
*/
is_updating_time=0;
append_unissued_timers();
// printf("reached the end of decrease timers\n");
}
int timers_isEmpty(){
return ( NULL == timers_first); /*could also check timer_last but it is an auxiliary variable */
}