Shop OBEX P1 Docs P2 Docs Learn Events
C language: what does pause(0) do? — Parallax Forums

C language: what does pause(0) do?

John KauffmanJohn Kauffman Posts: 653
edited 2014-12-19 06:38 in Propeller 1
For following I expected "hello" repeated on screen. I get one "hello" only.
What does arg of zero do with pause()? Web search talks about handing execution off to another thread but that doesn't seem to apply to Prop C.

#include "simpletools.h"
int main(void){
while(1){
print("hello");
pause(0);
}//while
}

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2014-12-17 08:22
    The parameter is multiplied by st_pauseTicks (usually set to 1ms worth of clock ticks). This is then passed to waitcnt(time+CNT). Go figure.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2014-12-17 08:42
    Thanks.
    Maybe deep below that getting ticks operation there is a division which makes /0. Go figure.
  • kuronekokuroneko Posts: 3,623
    edited 2014-12-17 08:46
    Not really. Calling waitcnt like this (time == 0) will always result in a rollover wait simply because there is a delay between fetching cnt, perfoming the add and finally invoking waitcnt. You'll see the same behaviour in SPIN.
  • mindrobotsmindrobots Posts: 6,506
    edited 2014-12-17 08:49
    Did you wait 53 seconds (or so) to see if the next Hello comes out? It could cause it to wait until the counter overflows.

    Sorry, couldn't resist: "Give me a 'Hello', Vasili. One 'Hello' only, please."
  • DomanikDomanik Posts: 233
    edited 2014-12-17 09:03
    mindrobots wrote: »
    Did you wait 53 seconds (or so) to see if the next Hello comes out? It could cause it to wait until the counter overflows.
    Yep it wraps around eventually. Must DEC 0 then test for 0.
  • DavidZemonDavidZemon Posts: 2,973
    edited 2014-12-18 15:07
    The fact that confusion happens so often around waitcnt rolling over... it makes me wonder if maybe the pause() method and any other waitcnt wrapper functions should perform a check to ensure rollover doesn't occur. something like:
    if (MINIMUM_WAIT > time) waitcnt(TIME + CNT)
    

    Of course one could say "they should just learn what waitcnt does and how the propeller works" but then... I think that ruins the point of calling a standard function called "pause".
  • AribaAriba Posts: 2,690
    edited 2014-12-18 20:41
    With a pause function like that:
    void pause(int millis)
    {
      int time = getcnt();
      while( millis-- > 0 ) {
         time += MILLISEC1;
         waitcnt(time);
      }
    }
    
    ... you can have delays up to 2^31 milliseconds and 0 will just immediatly return from the function. (It's not tested and you need to define the constant MILLISEC1 with the clockticks for 1 millisecond.)

    Andy
  • DavidZemonDavidZemon Posts: 2,973
    edited 2014-12-18 20:53
    Heck, it could be written in inline assembly and then you could take into account the loop overhead for each millisecond and you'd have a very accurate and flexible pause function
  • AribaAriba Posts: 2,690
    edited 2014-12-18 21:31
    There is no overhead for every loop with the code I have posted. But there is a little overhead for the call and return and for the instructions before and after the loop. They can be compensated with a constant value: int time = getcnt() - CMPENSATION;

    Andy
  • DavidZemonDavidZemon Posts: 2,973
    edited 2014-12-19 06:38
    Ariba wrote: »
    There is no overhead for every loop with the code I have posted. But there is a little overhead for the call and return and for the instructions before and after the loop. They can be compensated with a constant value: int time = getcnt() - CMPENSATION;

    Andy

    Ah! You're right! :D brilliant.
Sign In or Register to comment.