Welcome, Guest. Please login or register.

Author Topic: Threaded Code  (Read 6390 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Trev

  • Hero Member
  • *****
  • Join Date: May 2003
  • Posts: 1550
  • Country: 00
    • Show all replies
Re: Threaded Code
« on: November 24, 2010, 09:27:33 PM »
setjmp() and longjmp()? "sjlj" exceptions are used in many C++ implementations, including G++ on AmigaOS. In C, see the Protothreads library (more like fibers than threads) for creative uses http://www.sics.se/~adam/pt/. Protothreads is used by uIP.
 

Offline Trev

  • Hero Member
  • *****
  • Join Date: May 2003
  • Posts: 1550
  • Country: 00
    • Show all replies
Re: Threaded Code
« Reply #1 on: November 24, 2010, 10:19:00 PM »
Then you're just talking about a state machine of some sort, e.g. in pseudo code:

Code: [Select]
unsigned thread = 0;
unsigned state0 = 0;
unsigned state1 = 0;
unsigned state2 = 0;
unsigned state3 = 0;

while (1) {
  switch (thread++ % 4)
  {
  case 0:
    switch (state0++ % 2)
    {
      case 0:
        /* do something */
        break; /* yield */
     
      case 1:
        /* continue doing something */
    }
    break;

  case 1:
    switch (state1++ % 2)
    {
      case 0:
        /* do something */
        break; /* yield */
     
      case 1:
        /* continue doing something */
    }
    break;

  case 2:
    switch (state2++ % 2)
    {
      case 0:
        /* do something */
        break; /* yield */
     
      case 1:
        /* continue doing something */
    }
    break;

  case 3:
    switch (state3++ % 2)
    {
      case 0:
        /* do something */
        break; /* yield */
     
      case 1:
        /* continue doing something */
    }
  }
}

That's essentially what Protothreads does, albeit with the overhead of setjmp/longjmp.
« Last Edit: November 24, 2010, 10:21:48 PM by Trev »
 

Offline Trev

  • Hero Member
  • *****
  • Join Date: May 2003
  • Posts: 1550
  • Country: 00
    • Show all replies
Re: Threaded Code
« Reply #2 on: November 25, 2010, 12:39:51 AM »
This is from the hip and probably dragon-infested, but it looks legal:

Code: [Select]
int thread[] = {
  1, 2, 3, n, 0
};                 /* thread of instructions */

int *ip = thread;  /* initialize instruction pointer */

jmp_buf buf[0xff]; /* 0x00..0xff bytecode operations */
jmp_buf top;       /* top-level interpreter */

if (setjmp(buf[0x00])) {
  /* example uses op 0x00 as terminator, so this should never be executed */
  longjmp(top, 1); /* optionally, use second parameter to raise exceptions */
}

if (setjmp(buf[0x01])) {
  /* do op 0x01, manipulate ip (also the stack) as needed */
  longjmp(top, 1); /* optionally, use second parameter to raise exceptions */
}


if (setjmp(buf[0x02])) {
  /* do op 0x02, manipulate ip (also the stack) as needed */
  longjmp(top, 1); /* optionally, use second parameter to raise exceptions */
}

if (setjmp(buf[n])) {
  /* do op n, manipulate ip (also the stack) as needed */
  longjmp(top, 1); /* optionally, use second parameter to raise exceptions */
}

while (*ip) {
  if (!setjmp(top)) {
    longjmp(buf[*ip++], 1);
  }
  else {
    /* process exception */
  }
}

The overhead of setjmp/longjmp is system specific but probably less than a function call.
 

Offline Trev

  • Hero Member
  • *****
  • Join Date: May 2003
  • Posts: 1550
  • Country: 00
    • Show all replies
Re: Threaded Code
« Reply #3 on: November 25, 2010, 05:04:58 AM »
My abuse of setjmp/longjmp worked as expected. Add a stack and an op to push values, and you have a very simple (yet poorly designed ;-) virtual machine.