PDA

View Full Version : Does running CogNew create a copy of all my object's vars?



Dennis Ferron
04-04-2009, 04:30 AM
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[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?

Mike Green
04-04-2009, 05:00 AM
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 Schwabe
04-04-2009, 05:13 AM
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[16]

PUB Start
Pin := 16
cognew(DoWork, @Stack)

repeat
Pin := 23

PRI DoWork

repeat
dira[Pin]:=1
outa[Pin]:=1


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe (mailto:bschwabe@parallax.com)

IC Layout Engineer
Parallax, Inc.

MagIO2
04-04-2009, 05:19 AM
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 Schwabe
04-04-2009, 05:45 AM
MagIO2,

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



COGNEW (SpinMethod <(ParameterList)>, StackPointer)

COGNEW (AsmAddress, Parameter)

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe (mailto:bschwabe@parallax.com)

IC Layout Engineer
Parallax, Inc.

MagIO2
04-04-2009, 06:01 AM
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.