How to access thread-local data in Windows

I have stopped liking the idea to utilize the GS segment register for storing the pointer to a thread-local storage. Despite my efforts, the GS register still sometimes ends up containing zero.

Apart for the GS registers there are different options for accessing the thread-local data:

  • The ES register. Clozure CL compiler for Windows uses this register to access thread-local storage and it also avoids emitting “string” instructions (such as CMPS) since those instructions use the ES register. Apparently, they are not used since in Clozure CL only EAX register may contains an untagged value which also helps to make the garbage collection precise. Consequently, this approach would require SBCL to not generate string instructions and to save/restore the ES register while calling foreign code. I consider such tradeoffs unacceptable.
  • Inside the Thread Information Block in the Arbitrary Data Field. This field is always available for any thread at the adress %FS:0x14. Using this approach will entail changing in just some places related to handling the current thread. Having skimmed the code, it seems that there are just a couple of such places and they are pretty straightforward.

For now I'm betting on using %FS:0x14.