tags:

views:

65

answers:

1

I want to implement POSIX compliant microthreads in Linux environment. Basic idea is as follows:

  1. Using technique described here, assign new stack space for each fiber.
  2. Using setitimer, create timer that will send signals in constant time interval. Signal handler for this timer will act as a scheduler and switch between fibers.

The problem is, that doing longjmp in signal handler, won't terminate the handler, so kernel will wait for it's termination, instead for delivering new signals. This makes switching contexts impossible, because there are no signals to initiate the switches. One solution would be to unblock SIGALRM, so many signals can execute the handler at the same time, but this will cause race conditions problems.

What is the best and simplest way to implement preemptive microthreads ? All examples I found on Google were not preemptive.

+1  A: 

The solution is to use sigsetjmp / siglongjmp, intstead of setjmp/longjmp. sig* versions preserve signal masks :)

monkey