Page 1 of 1

Sleep function

Posted: Sat Mar 21, 2015 7:30 am
by TheLittleWho
I am trying to implement my own sleep function: `void sleep(int ticks);` that will stop kernel execution for number of specified ticks.

I tried this:

Code: Select all

void sleep(int ticks) {
    int start = get_tick(); // get_tick return current tick of system
    int ticks_left = ticks;

    while (ticks_left >= 0) {
          ticks_left -= (get_tick() - start);
    }
}
But it never stops... Where am i wrong? Who can help me to fix it?

Re: Sleep function

Posted: Sat Mar 21, 2015 7:42 am
by Combuster
Actually, the implementation of this sleep function only ever delays until the next tick. In addition, the bug you experience right now is in the code you did not post.

Both of these seem to indicate you haven't done any reasonable amount of debugging on your own. What does a simple thing such as print debugging tell you about all the variables that are relevant?

Re: Sleep function

Posted: Sat Mar 21, 2015 8:08 am
by ExeTwezz
Your code is wrong. You can save the counter value, see if `saved_counter + sleep_for' is more than the current counter value, and if it is, then stop waiting.

Also, as Combuster said, you can debug the problematic function using usual printing function.