Using the Boehm-Demers-Weiser GC with a new threading library
I am developing a threading library, and wish to make it work with your GC. As I
understand this may very well not be trivial, and by no means would I ask you to
work on this task, I thought at least I could ask you for some information as to
how I should proceed.
I have two aspects in my threading library:
- The first one is cooperative threads, implemented in C via copying of the
stack, and setjmp/longjmp. Every new thread saves its stack and setjmp result in
the heap before cooperating to another thread by restoring its stack and doing a
longjmp. If I'm not clear here, I can probably explain better, but I'm sure this
is rather common.
- The second one is that I want my threads to be able to become asynchronous,
and this is done in a rather complicated way involving pthreads. Every thread
starts as cooperative. If I want a thread that can become asynchronous, I have
to specify it when I create it. If this is the case, then I have to create a
pthread in order to get a new stack (this is where it really differs from GNU
PTH). This is where it gets fishy. The new pthread will do nothing and wait on a
condition variable. Then the main thread (the one which was doing stack copies
and setjmp, scheduling cooperative threads) schedules that new thread by doing a
longjmp back and forth from it, just like "plain" cooperative threads. If that
threads wishes to become asynchronous, it saves its stack, jumps to the next
cooperative thread (the main thread), which then wakes up the PThread, which in
turn will restore the saved stack and longjump to where it was when it was
cooperative. Still there?
If I try to rephrase this: I'm using pthreads for creating a stack. My main
thread jumps back and forth from/to any thread/pthread's stack. And if a thread
wants to become asynchronous, we save it, restore the stack which was doing a
pthread_cond_wait, wake it up, and let it jump to where we saved it.
This is rather complex, and perhaps suboptimal compared to executing pthreads