Hi,
What happens if another thread calls wait() with a
- smaller
- larger
value than the one in wait_for at the moment?
Currently what will happen is *every* wait() will adjust to the new time.
Generally speaking you might want to keep a count of seconds since boot, and do something like this:
Code: Select all
extern unsigned int secsSinceBoot; // unsigned int may not be the best type ;)
void wait(unsigned int secs)
{
unsigned int start = secsSinceBoot;
unsigned int end = secsSinceBoot + secs;
while(1)
{
unsigned int curr = secsSinceBoot;
if(curr == end)
break;
}
}
Note though that using "==" like that will not always work - if your program misses a timeslice you'll be waiting another 4 billion seconds for the next loop
You can adjust the above to something like this:
Code: Select all
extern unsigned int secsSinceBoot; // unsigned int may not be the best type ;)
void wait(unsigned int secs)
{
unsigned int start = secsSinceBoot;
unsigned int end = secsSinceBoot + secs;
int overflow = 0;
if(end < start)
overflow = 1;
while(1)
{
unsigned int curr = secsSinceBoot;
if(overflow && curr < start && end >= curr)
break;
else if(end >= curr)
break;
}
}
You could also store information about pending waits in your task structure, and as soon as one finishes (check in the timer), wake the task. Until it's woken, don't switch to that task.