Sleep function

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
TheLittleWho
Member
Member
Posts: 51
Joined: Sun Mar 01, 2015 7:58 am

Sleep function

Post 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?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Sleep function

Post 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?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
ExeTwezz
Member
Member
Posts: 104
Joined: Sun Sep 21, 2014 7:16 am
Libera.chat IRC: exetwezz

Re: Sleep function

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