Page 1 of 1
Thread Local Variables?
Posted: Thu Dec 19, 2013 10:23 pm
by genosensor
Is there some accepted way to implement thread local variables in ChiBiOS?
I found this in chthreads.h:
Code: Select all
/**
* @brief Returns the pointer to the @p Thread local storage area, if any.
* @note Can be invoked in any context.
*
* @special
*/
#define chThdLS() (void *)(currp + 1)
This looks encouraging. Can anyone provide more docs or, better yet, a usage example?
Re: Thread Local Variables?
Posted: Thu Dec 19, 2013 10:46 pm
by Giovanni
Hi,
It returns the lower address of the working area, you can put thread-variables there (a structure for example) but the mechanism is not compatible with stack checking as implemented in some ports because the whole working area is assumed to be stack. The check would not catch a stack overflowing on variables placed there.
There are other ways to have a TLS, for example static/automatic variables in the thread function scope (depending if you want a shared TLS or a per-instance TLS) or pass a TLS structure from outside the thread as the thread parameter.
Giovanni
Re: Thread Local Variables?
Posted: Thu Dec 19, 2013 11:02 pm
by genosensor
Thanks.
I was just curious...
It seems to me that the stack overflow problem could be solved by appending the thread local storage to the currp struct, enlarging it,
rather than putting the TLS struct in the working area.
Re: Thread Local Variables?
Posted: Fri Dec 20, 2013 9:47 am
by Giovanni
That is yet another way, you can extend the Thread structure using an hook, see chconf.h.
Giovanni
Re: Thread Local Variables?
Posted: Fri Dec 20, 2013 9:59 am
by genosensor
Excellent. That's just what I was seeking.