Thread Local Variables?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
genosensor
Posts: 65
Joined: Thu Oct 03, 2013 1:06 am
Has thanked: 1 time
Been thanked: 1 time

Thread Local Variables?

Post 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?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Thread Local Variables?

Post 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
genosensor
Posts: 65
Joined: Thu Oct 03, 2013 1:06 am
Has thanked: 1 time
Been thanked: 1 time

Re: Thread Local Variables?

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Thread Local Variables?

Post by Giovanni »

That is yet another way, you can extend the Thread structure using an hook, see chconf.h.

Giovanni
genosensor
Posts: 65
Joined: Thu Oct 03, 2013 1:06 am
Has thanked: 1 time
Been thanked: 1 time

Re: Thread Local Variables?

Post by genosensor »

Excellent. That's just what I was seeking.
Post Reply