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

Allowing thread local storage for mainthread

Postby genosensor » Thu Mar 23, 2017 3:48 am

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?
- brent

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Allowing thread local storage for mainthread

Postby Giovanni » Thu Mar 23, 2017 8:32 am

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


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 30 guests