Reputation: 30765
I have used makecontext/swapcontext successfully for shifting the stack. However, when I try to use it with pthread_mutex_lock or pthread_mutex_unlock, I always receive segmentation fault. Any idea, why is that so. The code is shown below.
Now I read from the swapcontext 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).
Any workaround to solve this?
static const unsigned int SWAP_STACK_SIZE = 8192;
// These are globally defined variables.
// Since each thread will have its own stack, they are defined as arrays.
static ucontext_t uctx_main[8], uctx_func[8];
static char func_stack[8][SWAP_STACK_SIZE];
// tid is thread ID here, values are 0, 1, 2, 3, etc...
if (getcontext(&uctx_func[tid]) == -1)
handle_error("getcontext");
uctx_func[tid].uc_stack.ss_sp = func_stack[tid];
uctx_func[tid].uc_stack.ss_size = SWAP_STACK_SIZE;
uctx_func[tid].uc_link = &uctx_main[tid];
makecontext(&uctx_func[tid], (void(*)())pthread_mutex_unlock, 1, &mutex);
if (swapcontext(&uctx_main[tid], &uctx_func[tid]) == -1)
handle_error("swapcontext");
Upvotes: 0
Views: 1079
Reputation: 409206
The manual page states that makecontext
passes the arguments as type int
. If you are on a 64-bit Linux then pointers will be 64 bits while int
will be 32 bit, and weird stuff will start to happen.
Upvotes: 1