Sleep function not working

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
robijnix2
Posts: 2
Joined: Fri Dec 27, 2013 6:29 am

Sleep function not working

Post 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
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Sleep function not working

Post by sortie »

You will want to understand the volatile keyword and how the code is optimized to a while true.
robijnix2
Posts: 2
Joined: Fri Dec 27, 2013 6:29 am

Re: Sleep function not working

Post by robijnix2 »

Ah oke makes sense. Thanks man.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Sleep function not working

Post 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.
Post Reply