Shop OBEX P1 Docs P2 Docs Learn Events
Cog end — Parallax Forums

Cog end

EMHmark7EMHmark7 Posts: 93
edited 2014-04-21 15:38 in Propeller 1
Hi,

I do not find in the doc:

Q1) When code assigned to a cog ends, is the cog ended and released (erased)?

Good example for cog use:
Here I need to run a separate cog because the calling cog loop is the one that needs to continue reading incoming requests for Stop requests,
so it cannot wait for the end of the actual task for that
otherwise, impossible to stop that process if we wait for the end of the process in order to know if we want to end the process.

So, every time I start that process, I start it in a separate cog, because the show must go on in the calling cog.
That raises the Q1) about how to manage recurring starts and stops of a procedure in a separate cog.

Comments

  • ElectrodudeElectrodude Posts: 1,658
    edited 2014-04-21 11:22
    It does in Spin. There is an implicit cogstop(cogid), which kills the current cog, when the cognew'ed function returns or ends. I assume this is true in C, but I've never used propgcc.
  • SRLMSRLM Posts: 5,045
    edited 2014-04-21 15:31
    propgcc does not place an implicit cogstop(cogid()) when a cog reaches the end of an execution. You can try it out:
    #include <stdio.h>
    #include <stdlib.h>
    #include <propeller.h>
    
    void do_toggle(void *arg)
    {
        //cogstop(cogid());
    }
    
    void main (void)
    {
    
        for(;;){
            int stacksize = sizeof(_thread_state_t)+sizeof(int)*15;
            int *stack = (int*) malloc(stacksize);
            int cog;
            // start the cog
            cog = cogstart(do_toggle, NULL, stack, stacksize);
            printf("do_toggle started on COG %d\n", cog);
            waitcnt(CLKFREQ/2 + CNT);
        }
    }
    

    Without the cogstop it produces
    Propeller Version 1 on /dev/ttyUSB0
    Loading a.out to hub memory
    14368 bytes sent                  
    Verifying RAM ... OK
    [ Entering terminal mode. Type ESC or Control-C to exit. ]
    do_toggle started on COG 1
    do_toggle started on COG 2
    do_toggle started on COG 3
    do_toggle started on COG 4
    do_toggle started on COG 5
    do_toggle started on COG 6
    do_toggle started on COG 7
    do_toggle started on COG -1
    

    With the cogstop it produces
    [ Entering terminal mode. Type ESC or Control-C to exit. ]
    do_toggle started on COG 1
    do_toggle started on COG 1
    do_toggle started on COG 1
    do_toggle started on COG 1
    
  • EMHmark7EMHmark7 Posts: 93
    edited 2014-04-21 15:38
    Ok Thanks,

    So I catch that we can do it from within the running code, and it can know its own cogID with cogid().

    Thanks again,
    Marc
Sign In or Register to comment.