This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Initializing initial bottom of stack variable in pthread.c


I looked at the libc-ports mailing list and what I
found does not quite answer my question.  In our case,
our stack is growing down, so to try and be more
clear, I'm going to try to step through the code with
simple numbers.

Let's suppose STACK_SIZE = 5, and the top of the
initial stack is at 25.  pthread_create gets called
and along the way the initial pthread and manager
thread get created with STACK_SIZE and no more. 
Suppose the CURRENT_STACK_FRAME is 22 when we init the
bos.  Then from pthread_initialize:

 __pthread_initial_thread_bos =
    (char *)(((long)CURRENT_STACK_FRAME - 2 *
STACK_SIZE) & ~(STACK_SIZE - 1));

evaluates to 8.  If I'm looking at this right, our
main thread's stack resides from 21-25, and our
manager resides from 16-20.  Our worker thread gets
created and is given space from 11-15.  When
pthread_self gets called from the worker (so let's
assume our stack pointer is 13).  Line 7 below
evaluates to true since 13 >= 8, so the code now
thinks our worker thread is the initial thread since
__pthread_initial_thread_bos was set well below the
memory that was actually allocated for the main and
manager threads, and the very first worker thread got
allocated free space that was below the main and
manager thread space but still above 
__pthread_initial_thread_bos.

I understand that the sizes must be at least
STACK_SIZE, and that the threads can contain more
memory than this and that __pthread_initial_thread_bos
must also include the extra memory taken up.  I am
still thinking that __pthread_initial_thread_bos, so
let me try a picture of what I think is happening

-------------------   top of initial stack
| Initial thread  |
|                 |
-------------------
| Manager thread  |
|                 |
-------------------
| Free space      |
|                 |
-------------------   __pthread_initial_thread_bos

Then the very first worker thread gets assigned space
out of Free space wich is between top of initial stack
& __pthread_initial_thread_bos.

I appreciate the help.

Thanks

Jay

1 static inline pthread_descr thread_self (void)
2 {
3 #ifdef THREAD_SELF
4  return THREAD_SELF;
5 #else
6  char *sp = CURRENT_STACK_FRAME;
7  if (sp >= __pthread_initial_thread_bos)
8    return &__pthread_initial_thread;
9  else if (sp >= __pthread_manager_thread_bos)
10    return &__pthread_manager_thread;
11  else if (__pthread_nonstandard_stacks)
12    return __pthread_find_self();
13  else
14 #ifdef _STACK_GROWS_DOWN
15    return (pthread_descr)(((unsigned long)sp |
(STACK_SIZE-1))+1) - 1;
16 #else
17    return (pthread_descr)((unsigned long)sp &~ 
(STACK_SIZE-1));
19 #endif
#endif
}

--- Daniel Jacobowitz <drow@false.org> wrote:

> On Tue, Aug 01, 2006 at 07:57:39AM -0700, Jay wrote:
> > So the bottom of stack variable needs to be set to
> > STACK_SIZE for the thread manager + 2 * STACK_SIZE
> - 1
> > for the initial thread from the top of the stack
> (for
> > a total of 3 * STACK_SIZE - 1)?
> 
> No.  The stack size of the initial thread must be
> _at least_
> STACK_SIZE.  That's all.
> 
> > so is this a linuxthreads problem in glibc, or is
> it a
> > problem with the stack space that is getting
> assigned
> > by the kernel?
> 
> I can't follow your report at all, but you might
> want to look at the
> recent discussion of allocations above and below the
> initial stack
> pointer on the libc-ports mailing list.
> 
> -- 
> Daniel Jacobowitz
> CodeSourcery
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]