Problem with setjmp/longjmp

Joined
Mar 16, 2010
Messages
1
Reaction score
0
Hi,

The following code is just not working

#define STACK_SIZE 1524

static void mt_allocate_stack(struct thread_struct *mythrd)
{
unsigned int sp = 0;
void *stck;

stck = (void *)malloc(STACK_SIZE);

/*
* get double word aligned SP -- stacks grow from high
* to low
*/
sp = (unsigned int)&((stck));
sp = sp + STACK_SIZE;
while((sp % 8) != 0)
sp--;

#ifdef linux
/*
* keep double word aligned but put in enough
* space to handle local variables for MtSched
*/
(mythrd->saved_state[0]).__jmpbuf[JB_BP] = (int)sp;
(mythrd->saved_state[0]).__jmpbuf[JB_SP] = (int)sp-500;
#endif

void mt_sched()
{
fprintf(stdout,"\n Inside the mt_sched");
fflush(stdout);

if ( setjmp(current_thread->saved_state) == 0 )
{
mt_allocate_stack(current_thread);

fprintf(stdout,"\n Jumping to thread = %u",current_thread->thread_id);
fflush(stdout);
longjmp(current_thread->saved_state, 2);
}
else
{
fprintf(stdout,"\n\n Calling the function = ");
fflush(stdout);
}
}
}

All I am doing here is saving a state and then allocating a new stack and storing the pointer in the saved state.

But the code seg faults at this is line
else
{
fprintf(stdout,"\n\n Calling the function = ");
fflush(stdout);
}

It does not matter what I put in this else statement it always segfaults.

Can somebody tell me why is that happening.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top