Page 1 of 1

Sleep function not working

Posted: Fri Dec 27, 2013 6:33 am
by robijnix2
Hello,

I'm very new to OSdev, and I'm stuck. I have this sleep function:

Code: Select all

void sleep(uint32_t dur)
{
	uint32_t goal_tick=tick+dur;
	while(tick<=goal_tick);
	return;
}
and this is my PIT IRQ:

Code: Select all

static void timer_irq(uint32_t n)
{
	tick++;
}
Now this code does not work, it just hangs forever. However, when I change the sleep function to:

Code: Select all

void sleep(uint32_t dur)
{
	uint32_t goal_tick=tick+dur;
	while(tick<=goal_tick&&pow(5,5));//just add some random function call
	return;
}
it suddenly does work?
can someone explain this to me?

Thanks

Re: Sleep function not working

Posted: Fri Dec 27, 2013 7:09 am
by sortie
You will want to understand the volatile keyword and how the code is optimized to a while true.

Re: Sleep function not working

Posted: Fri Dec 27, 2013 7:15 am
by robijnix2
Ah oke makes sense. Thanks man.

Re: Sleep function not working

Posted: Fri Dec 27, 2013 10:53 am
by sortie
When you encounter problems like this, you should use tools like objdump (1) to examine the executed instructions. This would have revealed the function was optimized to an infinite loop. Be sure to do this when the code doesn't do what you thought it should.