+---------- On Feb 12, Jerry Asher said:
> I am trying to understand where the thread-specific limit is involved
> but I can't.

Heap-->    <--stack3   <--stack2   <--stack1

Stack 3's limit is the top of the heap.  Stack 2's limit is the bottom of
stack 3.  Stack 1's limit is the bottom of stack 2.

If you are willing to allocate the same amount of address space for
every stack, then you can compute the current SP's limits from the
current SP. I'm not sure if that would be faster than looking up a limit
in TLS.

If each stack can be a different size (in terms of address space
allocated to it), then you cannot compute the SP limits from the SP.

> If as a thread, I ask for a new thread to be created and hence a new
> stack, I would guess (but I don't know) that the base for all stack
> pointers are stored in some global, locked, table, along perhaps with
> the limits of the stack which are either stored in that table, or
> local to the stack itself.

Suppose it is. Now you've got to locate the table - which, unless you're
willing to limit the number of simultaneous threads, must be able to
grow and therefore cannot be at a fixed address. Once you've located
the table, you've got to search it using the current SP as the search
key. Maybe you do it by binary search; maybe you do it by hashing the
high bits of SP. Whichever way you choose, I think it's going to require
several instructions.

On some architectures (like all the modern RISCs with 32 general-purpose
registers), you could probably just dedicate a register to being the
stack limit. In such a system, the SP check would probably just take 3
instructions.

The i386 doesn't have any GPRs to spare, but you might still be able to
make it reasonably fast. I think Linux now uses the GS segment register
to point to TLS, so you could put the stack limit at a fixed location in
the GS segment and do the check pretty fast.

Reply via email to