Stack Location and vtable [re]

NOTE. . To understand my question, you may need to find out about my project and problem. If not, skip directly to the “QUESTION” section below.

< / "> Project

I am working on writing a C ++ class that allows its parents to act as synchronous / blocking threads using Pause () and Resume ().

Here is an example of how this will work.

class BlockingThread
   : public BlockingThreadBase // all the Asm magic happens in BlockingThreadBase
{
  void StartStopHere(void) // called upon the first Resume() call (pure virtual in base)
  {
    printf("1"); Pause();
    printf("3"); Pause();
    printf("5"); Pause();
  }
};

int main(void)
{
  BlockingThread obj;

  obj.Resume(); printf("2");
  obj.Resume(); printf("4");
  obj.Resume(); printf("6");

  return 0;
}

// OUTPUT: 123456

I tried a real thread and some new ideas, but they were too slow to switch between codes 21 million times per second.

- ( ), . obj1.Resume(), , New #1 Stack, Pause(), , .

</" > < >

- BlockingThread main().

 _________________
| Normal | Stack  |
| Stack  | Buffer |
|________|________|

BlockingThread main() .

 ___________________________________
| Normal | Stack  | New #1 | Stack  |
| Stack  | Buffer | Stack  | Buffer |
|________|________|________|________|

BlockingThread main() .

 _____________________________________________________
| Normal | Stack  | New #1 | Stack  | New #2 | Stack  |
| Stack  | Buffer | Stack  | Buffer | Stack  | Buffer |
|________|________|________|________|________|________|

</" >

"" , obj.Resume() ( ), , , StartStopHere(), segfault StartStopHere(). GDB can't find linker symbol for virtual table for 'BlockingThreadBase' value, - BlockingThreadBase. , StartStopHere() .

</" >

( , )

[ ] vtable ( , , ) [] [ ]?

+3
2

, . , libco. http://byuu.org/files/libco_v16.tar.bz2. x86, x86_64, PPC32 PPC64. (ab), setjmp/longjmp posix ucontext ( ). swizzling 5 . 20 / . , .

:

#include "libco.h"
#include <stdio.h>

static cothread_t t1;
static cothread_t t2;

static void foo(void)
{
   for (int i = 1; i < 10; i+=2)
   {
      printf("%d\n", i);
      co_switch(t1); // Swap back to main cothread
   }
}

int main(void)
{
   // Get a handle to the current coinstance.
   t1 = co_active();

   t2 = co_create(10000, foo); // New cothread with stacksize 10000.

   for (int i = 0; i < 10; i+=2)
   {
      printf("%d\n", i);
      co_switch(t2); // Swap to cothread t2.
   }

   co_delete(t2);
}
+1

, . , , , . , vtable ( vpointer) , , - . x86 linux g++ - ( , ) .

, - , vtables. , ++ - .

. , , memcpy (& BlockingThread, sizeof (BlockingThread)) , , .

+1

Source: https://habr.com/ru/post/1790198/


All Articles