3 ms·
I have been playing with defining my own toy ABI, and I came up with a trick for thread-local storage: Allocate the segment for TLS and the thread stack togeth
by Findecanor 5y ago
I have been playing with defining my own toy ABI, and I came up with a trick for thread-local storage:
Allocate the segment for TLS and the thread stack together so that both start (stack grows down, TLS up) at a 2*n - aligned address, with n large enough so that the stack size never exceeds 2*n.
Then, you could always get TLS-1 from SP by setting the lower bits to 1.
To avoid an odd size for the segment, you could put the TLS block at negative offset from the end of the segment.
I had intended mine for machines with 64-bit pointers, but data structures tend to be smaller on 32-bit machines, so they can cope with smaller stacks.
On the M68K Amiga the stack size was fixed and often relatively small: recommended minimum was 16000 bytes, and for large programs recommended size was 80000.
Modern operating systems for CPUs with MMUs grow the stack dynamically but still have to set aside a part of the address space for the maximum it could grow into.
- gpderetta 5y agoIIRC that's how TLS used to work in LinuxThreads.