Thread-local Storage

Hi,

supposedly, a serious conceptual issue has sneaked in between routine messages at Github ...

The issue is local storage of threads that is conceptually foreseen in Pthreads, useful and commonly needed. If I understand correctly, there are concerns about overhead in memory and table look-up at context switching.

I discussed this with a colleague who had done significant development for PSION Symbian a longer time ago, and we both felt that the following approach previously taken by Symbian makes sense:

  TLS was implemented without any indirection. Threads genuinely provided an integer variable that could be used to pointer at TLS memory, when needed. Consequently, the runtime system would neither require look-ups in allocation tables or so, nor would it generate dead memory except for a null pointer in threads without TLS. As the number of threads in RIOT is expected to remain below some (very finite) limit, I suppose the latter is not an issue.

  Any comments/opinions?

Cheers,

Thomas

Hi,

isn't the only difference between heap and TLS memory the memory protection? As we do not support MMUs nor even MPUs (As far as I know), were would be the difference between TLS memory and heap memory?

Heiko

Hi,

There would be no difference in the memory usage of heap as we don't have differenciatable user and kernel space on platforms without MMU. The benefit would be "just" to provide a more complete POSIX thread interface.

Best, Martin

Hi,

isn't the only difference between heap and TLS memory the memory protection? As we do not support MMUs nor even MPUs (As far as I know), were would be the difference between TLS memory and heap memory?

It's

ptr = malloc(): points to the same memory in every thread

vs.

ptr = tls_malloc(): points to different memory in every thread when accessed.

So the only difference is not memory protection, but memory mapping.

The global errno variable is a nice example of when that might be useful. Right now, RIOT on newlib platforms is not thread safe at all, I guess. :wink:

Still we can't implement that (so easily) without MMU...

Kaspar