Shop OBEX P1 Docs P2 Docs Learn Events
Simple, I think, question re start method in a referenced object — Parallax Forums

Simple, I think, question re start method in a referenced object

jstjohnzjstjohnz Posts: 91
edited 2011-07-14 05:28 in Propeller 1
Is there any need to use this form:
VAR
long    _acme
long    cog  

PUB Start(acme) : okay
   _acme:=acme                  'value to pass to ASM in PAR
   okay  := cog := cognew(@entry, @_acme) + 1  'Start driver in new cog   

As opposed to this form:
VAR
long    cog  

PUB Start(acme) : okay

   okay  := cog := cognew(@entry, @acme) + 1  'Start driver in new cog   

The first method declares it's own local copy of the parameter passed by spin, assigns the passed parameter to it, then uses this local copy in the COGNEW directive. Is this necessary or beneficial in any way, or is it OK to just pass the parameter in COGNEW as in the 2nd example?

Comments

  • Mark_TMark_T Posts: 1,981
    edited 2011-07-12 17:30
    I think, from what I understand, that the second example will fail - you pass the address of a function argument to the new cog, but that argument is on the stack and vanishes when the Start() function returns - the cog keeps hold of the address and by the time it looks at it it could be pointing to totally different data.

    In the first example the passed value is copied to a global variable whose extent is not limited to the function call (extent is the time window in which a variable is defined)
  • jstjohnzjstjohnz Posts: 91
    edited 2011-07-12 20:31
    Makes sense, thank you.
  • ericballericball Posts: 774
    edited 2011-07-13 08:06
    As Mark_T says, the second form won't work because the input parameter is on the stack thus won't be usable by the PASM driver. The more typical form is:
    VAR
    long    cog  
    
    PUB Start(parmptr) : okay
    
       okay  := cog := cognew(@entry, parmptr) + 1  'Start driver in new cog
    
    So the caller passes in the address of a long parameter block. If other functions in the same object need access to the parameter block, then a copy needs to be made, as per the first form: (In fact, I'm having difficulty coming up with a reason to use your first form.)
    VAR
    long    cog
    word   parmptr
    
    PUB Start(_parmptr) : okay
    
       okay  := cog := cognew(@entry, parmptr := _parmptr) + 1  'Start driver in new cog
    
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-07-13 08:21
    ericball wrote: »
    (In fact, I'm having difficulty coming up with a reason to use your first form.)
    The first form would make sense if he wanted to keep the variable internal to the object, and he was just passing an initial value to use. He could have multiple instances of the object where each instance needed a private copy of _acme to communicate to the cog.

    The second form would work if he ensured that the new cog was done using the stack variable "acme". He could either use a waitcnt after the cognew, or he could have the cog put a different value in acme, such as a zero,and wait for it to change before exiting the start method.
  • jstjohnzjstjohnz Posts: 91
    edited 2011-07-13 12:44
    ericball wrote: »
    As Mark_T says, the second form won't work because the input parameter is on the stack thus won't be usable by the PASM driver. The more typical form is:
    VAR
    long    cog  
    
    PUB Start(parmptr) : okay
    
       okay  := cog := cognew(@entry, parmptr) + 1  'Start driver in new cog
    
    So the caller passes in the address of a long parameter block. If other functions in the same object need access to the parameter block, then a copy needs to be made, as per the first form: (In fact, I'm having difficulty coming up with a reason to use your first form.)
    VAR
    long    cog
    word   parmptr
    
    PUB Start(_parmptr) : okay
    
       okay  := cog := cognew(@entry, parmptr := _parmptr) + 1  'Start driver in new cog
    

    OK, somehow I put the @ signs in there, they weren't intended. I'm just trying to pass, in PAR, a pointer to a data structure in hub ram so that the PASM code can access that hubram data. And I'm trying to figure out if there's a need to create a local copy of the parameter (the pointer) that's passed to the start method before using cognew.

    So my question should have been this:

    First form with a local copy of the passed param (which is a pointer):
    VAR
    long cog
    long _acmeptr
    
    PUB Start(acmeptr) : okay
       _acmeptr:=acmeptr                  
       okay  := cog := cognew(@entry, _acmeptr) + 1  'Start driver in new cog, pass pointer to acme in PAR
    

    or 2nd form:
    VAR
    long cog
    
    PUB Start(acmeptr) : okay
       okay  := cog := cognew(@entry, acmeptr) + 1  'Start driver in new cog, pass pointer to acme in PAR
    

    As I understand it now, either approach should work. However I'm not clear on your statement "If other functions in the same object need access to the parameter block then a copy needs to be made".
  • ericballericball Posts: 774
    edited 2011-07-14 05:28
    jstjohnz wrote: »
    As I understand it now, either approach should work. However I'm not clear on your statement "If other functions in the same object need access to the parameter block then a copy needs to be made".

    Say, for example, in the same object as the Start function you have other functions which update the COG driver parameter block. Then either the calling function would need to pass in the pointer or the object would need to keep a local copy.
Sign In or Register to comment.