Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Problem with setjmp/longjmp (http://www.velocityreviews.com/forums/t717880-problem-with-setjmp-longjmp.html)

harman 03-16-2010 12:11 PM

Problem with setjmp/longjmp
 
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.


All times are GMT. The time now is 08:08 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.