Shop OBEX P1 Docs P2 Docs Learn Events
Does running CogNew create a copy of all my object's vars? — Parallax Forums

Does running CogNew create a copy of all my object's vars?

Dennis FerronDennis Ferron Posts: 480
edited 2009-04-03 22:01 in Propeller 1
I'm trying to debug a Spin object where my start routine launches a worker subroutine second Spin cog. I'm trying to use some vars to communicate between the parent object and the subroutine started by the parent, but it seems like when I try to access those vars in the subroutine, all I get are copies of the variables. For instance:

VAR
   long TestComm
   long Stack[noparse][[/noparse]16]

PUB Start
   TestComm := 11
   cognew(DoWork, @Stack)

   repeat
       TestComm := 22

PRI DoWork

   repeat
      ' Print out TestComm:  value is 11, never prints 22





So, I'm guessing cognew on a Spin routine creates a copy of the object? What if you had a lot of data used by the worker routine, that isn't needed by the Start'ing object; is there are way to avoid making 2 copies?

If it is another copy, I wonder if there is a hack to set the value of the object base address pointer within the DoWork routine so that it points again at the original object, so that referring to the variables refers to the original ones and not the copy? It would be something like cognew(DoWork(@@0), @Stack) and DoWork(base_address) would reset its own base address to the value passed in?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-03 21:00
    No, there's no copying. There's only the one copy of the variables and they're accessible by both cogs. When you do a COGNEW, it finds a free cog, copies the Spin interpreter from ROM to the cog's RAM and starts it. That interpreter uses the stack you provide for any return addresses and local variables needed by the interpreted code.

    There's only one copy of TestComm. Perhaps there's something in the timing between the two cogs. It's impossible to tell from the short fragments you've supplied.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2009-04-03 21:13
    Dennis Ferron,

    Substituting 'TestComm' with an LED pin number on the Propeller DEMO board, I get the secondary number to pass through. There must be something in another part of your code that causing the problem.

    VAR
       long Pin
       long Stack[noparse][[/noparse]16]
    
    PUB Start
       Pin := 16
       cognew(DoWork, @Stack)
    
       repeat
           Pin := 23
    
    PRI DoWork
    
       repeat
          dira[noparse][[/noparse]Pin]:=1
          outa[noparse][[/noparse]Pin]:=1
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-04-03 21:19
    shouldn't cognew use the adress of DoWork?

    cognew( @DoWork, @stack)

    That's why you see value 11 and never 22. The repeat loop is never reached and the print loop is executed by cog 0.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2009-04-03 21:45
    MagIO2,

    That only applies to an PASM program. SPIN just requires the spin method.



    COGNEW (SpinMethod <(ParameterList)>, StackPointer)

    COGNEW (AsmAddress, Parameter)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-04-03 22:01
    Thanks Beau. These are the little differences that you need to learn the hard way or by giving wrong comments in the forum ;o)
    And as you see I currently prefer running PASM code in all other COGs.

    So I'd be really interested what Dennis did wrong in the code he run. The posted code looks to be correct.
Sign In or Register to comment.