Allowing thread local storage for mainthread

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
Post Reply
genosensor
Posts: 65
Joined: Thu Oct 03, 2013 1:06 am
Has thanked: 1 time
Been thanked: 1 time

Allowing thread local storage for mainthread

Post by genosensor »

ChiBiOS threads support thread local storage between the Thread struct and the thread's stack (which typically grows down toward the Thread struct). These thread local variables are handy and worked great until I tried using it on mainthread.
Then, instant segfault panic, because the mainthread's Thread struct is located just before the system's VTimer list base!

Why can't the mainthread Thread struct be at the base of the thread's stack, as is the case with all other threads?

In chsys.c, delete the mainthread declaration and change:

Code: Select all

setcurrp(_thread_init(&mainthread, NORMALPRIO));
currp->p_stklimit = &__main_thread_stack_base__;

to:

Code: Select all

setcurrp(_thread_init((Thread *)&__main_thread_stack_base__, NORMALPRIO));
currp->p_stklimit =
    &__main_thread_stack_base__ + sizeof(Thread)/sizeof(stkalign_t) + 1;


Are there ChiBiOS supported systems where this would not work?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Allowing thread local storage for mainthread

Post by Giovanni »

Hi,

In RT4 the thread structure is no more placed at the base of the working area because:
1) It was a perfect target for an overflowing stack, making debugging harder.
2) It was conficting with MPU protection, now there is a guard page in that place.

You may use the thread structure extension hook and add a field pointing to your TLS, you could allocate it anywhere, not necessarily below the stack.

Giovanni
Post Reply