Shop OBEX P1 Docs P2 Docs Learn Events
Stop-ping for Cognews? — Parallax Forums

Stop-ping for Cognews?

coryco2coryco2 Posts: 107
edited 2013-02-07 14:23 in Propeller 1
I was reading in the Propeller Spin Language Reference in the Cognew section how one could use a "Stop" method to prevent multiple starts when starting code in a new cog...
PUB Start
    Stop
    CogID := cognew (Method, @StackSpace)

PUB Stop
   if CogID > -1
     cogstop (Cog ID)

...and thought how this would be smart to implement in my own code. However, when I tried it on the following, Method4 never runs. Why not? I even tried adding "return" in each of the Stop methods, but to no avail.
PUB Start
    StopA
    CogA := cognew(Method1, @CogAStack)
    StopB
    CogB := cognew(Method2, @CogBStack)
    StopC
    CogC := cognew(Method3, @CogCStack)
    Method4

PUB StopA
   if CogA > -1
     cogstop (CogA)

PUB StopB
   if CogB > -1
     cogstop (CogB)

PUB StopC
   if CogC > -1
     cogstop (CogC)

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2013-02-07 10:41
    In this piece of code there is no problem. You should also show the rest of your code! How did you define CogA, is it a long or a byte?

    However, there is no need to have 3 different Stop-methods! You could replace it with:
    PUB Start
      Stop( CogA )
      CogA := cognew...
      ....
    
    PUB Stop( theCogid )
      if theCogid > -1
        cogstop( theCogid )
    
  • coryco2coryco2 Posts: 107
    edited 2013-02-07 12:33
    I defined CogA, CogB and CogC as bytes, but I also tried switching them to longs and it made no difference.

    As to the rest of the code, it works fine without these Stop methods, so it's something about them that is causing the issue. Some of the additional Cog methods do themselves start other cogs (for FullDuplexSerial, etc.); so is it possible that I am just running out of cogs for some reason, or stopping the wrong cogs? I don't how that could happen, though...

    Since my code works without the Stop calls, it seems logical to me that the only way it will not start Method4 is if it is getting stuck in somewhere in one of those Stop calls. But why?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-02-07 12:42
    coryco2 wrote: »
    But why?
    MagIO2 wrote: »
    You should also show the rest of your code!

    If MagIO2's is willing to help, I'd suggest accepting.

    IMO, he knows a lot about this stuff.
  • MagIO2MagIO2 Posts: 2,243
    edited 2013-02-07 13:27
    Sometimes bugs only come in combination. One part alone is running. The other part alone is also fine. But when putting both together you have a Problem.
    So, if you don't have secret code to hide from us, you should archive the project and post the whole package here.

    If it IS secret, you have to find the bug by yourself, as the part you posted should be fine!

    If real strange things go on, you may have problems with the stack-size.
  • coryco2coryco2 Posts: 107
    edited 2013-02-07 13:28
    The rest of my code works fine until I add the Stop methods, so it's something about them that is causing the issue. Would there be any other way Method4 would not start besides the code stalling before it got there, i.e. somewhere in one of the Stop calls?
  • MagIO2MagIO2 Posts: 2,243
    edited 2013-02-07 13:43
    Oh ... ok ... found it ;o)

    The point is, that variables are initialized with 0. So, calling stop will actually kill the main-COG.
    You should initialize the CogA..C variables before first usage or better switch to the following code:
    PUB start
      CogA := stop( CogA )
      CogA := cognew( Method1, @CogAStack ) + 1
      ...
    
    PUB stop( cogNr )
      if cogNr
        cogstop( cogNr - 1 )
      return 0
    

    The basic trick here is, that the COGID is incremented by one. So, if cognew returns -1 (no more COG available) you get 0 which is a) the same value that all VARs are initialized to and b) is equal to FALSE in an if statement.
    a) means that you don't need extra initialization code (defaulting the Cog-ID variables to -1)
    b) means that your if statement does not need a compare
  • MagIO2MagIO2 Posts: 2,243
    edited 2013-02-07 13:56
    @parallax
    This should be corrected and explained in the manual!
  • coryco2coryco2 Posts: 107
    edited 2013-02-07 13:59
    Ah! Yep, that was it. Thanks, much! :-)
  • Daniel HarrisDaniel Harris Posts: 207
    edited 2013-02-07 14:23
    Hi guys,

    Thank you for pointing this out. I've added it to our Manual change list. I'm glad you guys have our back!

    Best regards,
    Daniel
Sign In or Register to comment.