Shop OBEX P1 Docs P2 Docs Learn Events
Asynchronous function call in Spin language? — Parallax Forums

Asynchronous function call in Spin language?

CarvtumCarvtum Posts: 2
edited 2010-03-31 17:40 in Propeller 1
Hi
Is there a way to implement an asynchronous call from one object to another (located in an other Cog) so that the call will return immediately, while the called object starts working with the initiated job parallel with the caller. In other words how do you implement a "fork" with the Spin language?

Sorry, I am new in Spin and could not find an example for this kind of use of the spin language.

Comments

  • pullmollpullmoll Posts: 817
    edited 2010-03-30 13:25
    Carvtum said...
    Hi
    Is there a way to implement an asynchronous call from one object to another (located in an other Cog) so that the call will return immediately, while the called object starts working with the initiated job parallel with the caller. In other words how do you implement a "fork" with the Spin language?

    Sorry, I am new in Spin and could not find an example for this kind of use of the spin language.


    There is an example in the description of either COGINIT or COGNEW.
    You can call either with the name of a function and one argument (address) and the address of a stack range to use.
    VAR
          long stack[noparse][[/noparse]20]
    OBJ
          aobj : "AnObject"
    
    PUB/PRI forking(myparam) | cog
      cog := COGNEW(aobj.myfunc(@myparam), @stack) + 1
      if cog < 1
        do something if failed
    
    


    The size of the required stack depends on the nesting of subroutines called from the aobj.myfunc function and the number of local variables of each. There is no stack checking, so it's all your job to give it enough room.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    He died at the console of hunger and thirst.
    Next day he was buried. Face down, nine edge first.

    Post Edited (pullmoll) : 3/30/2010 1:31:52 PM GMT
  • KyeKye Posts: 2,200
    edited 2010-03-30 14:38
    Mmm, I'm·not sure if·cognew works across objects.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • mparkmpark Posts: 1,305
    edited 2010-03-30 15:33
    Kye is correct—you can't cognew methods from another object. The cognew typically is done inside the other object's start pub. You call aObj.start and it does the forking.
  • CarvtumCarvtum Posts: 2
    edited 2010-03-31 05:56
    OK. Thanks Mpark. I sought out the idea: you have to call such a synchronous method of an other object that in turn launches an other method to run "asynchronously", parallel, as an other "thread" on an other Cog.
  • ericballericball Posts: 774
    edited 2010-03-31 15:55
    Do be aware that there is a significant startup delay (~8000 CLK cycles) between when COGNEW is executed and when the new code starts executing.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Composite NTSC sprite driver: Forum
    NTSC & PAL driver templates: ObEx Forum
    OnePinTVText driver: ObEx Forum
  • StefanL38StefanL38 Posts: 2,292
    edited 2010-03-31 17:40
    cogs and objects are complete different things

    you can have ONE object using all eight cogs
    or you can have n objects using just one cog and every mixture inbetween.

    here is a short democode to show how ONE object can use two cogs and how then the two cogs work independent from each other
    manipulating one and the same variable

    best regards

    Stefan
    CON
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
      
    VAR
      long heart_stack[noparse][[/noparse] 20]
      long CogStack1[noparse][[/noparse] 20]
      long CogStack2[noparse][[/noparse] 20]
      
      long MyTestVar
    
    OBJ
      debug : "FullDuplexSerial"
    
    
    PUB Main
    'the FIRST PUB-Method inside a spinfile is ALWAYS the startpoint where the program starts to run
      debug.start(31, 30, 0, 115200)
      
      MyTestVar := 100
      debug.str(string("Start MyTestVar="))
      debug.dec(MyTestVar)
      debug.Tx(13)
    
      'both cognew-commands are in the SAME file (=same object)
      'in this case you can access variables simply by the variable-name
      cognew(M1,@CogStack1)
      cognew(M2,@CogStack2)
      
      repeat
        waitcnt(clkfreq + cnt)
        debug.str(string("MyTestVar="))
        debug.dec(MyTestVar)
        debug.Tx(13)
    
    
    
    PUB M1
      repeat
        waitcnt(ClkFreq * 3 + cnt)
        MyTestVar := 1
    
    
    PUB M2
      repeat
        waitcnt(ClkFreq * 5 + cnt)
        MyTestVar := 2
    
    
Sign In or Register to comment.