Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why makecontext does not work with pthreads

From makecontext() manual...

Due to limitations in the current pthread implementation, makecontext should not be used in programs which link against the pthread(3) library (whether threads are used or not).

Now my question is, why it doesn't work and what are the alternative methods. Actually I'm interested in switching stacks in a user-level thread at some points, but I'm seeing that when I call swapcontext(), I get segmentation faults every now and then. What should I do?

I want to achieve something like this:

void thread_func(void * thread_args)
{
 a();
 b();
 getcontext/makecontext/swapcontext to call c();
 d();
 ....
}

So in this case, I want to use a separate stack when executing function c().

like image 420
MetallicPriest Avatar asked Nov 17 '11 14:11

MetallicPriest


1 Answers

Due to limitations in the current pthread implementation, makecontext should not be used in programs which link against the pthread(3) library

That section of the manual applies to LinuxThreads, which used to round %esp value up to find current thread descriptor. That (obviously) wouldn't produce a valid thread descriptor if you were executing on an alternate stack.

LinuxThreads are no longer used by any Linux distributed in the last 5+ years, and {get,make,swap}context work just fine with NPTL threads.

EDIT: Actually, I only see the "due to limitations" in NetBSD docs, not in Linux docs.

when I do swapcontext, I get segmentation faults every now and then

You have a bug that shows up as a segmentation fault now and then. You haven't supplied enough info to guess where that bug may be.

like image 150
Employed Russian Avatar answered Sep 18 '22 13:09

Employed Russian