Why doesn't the main thread need a SETUP_CONTEXT?

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Post Reply
xnr
Posts: 9
Joined: Fri Jul 18, 2014 4:49 pm

Why doesn't the main thread need a SETUP_CONTEXT?

Post by xnr »

I am porting ChibiOS 2.6.4 to new platform. Unfortunately, my demo application crashes when a context switch to the main thread takes place. I figured out that the main thread context contains garbage data. I think this happens because the context isn't setup like it is for other normal threads.

How do the other ports handle this?
skute
Posts: 64
Joined: Wed Aug 29, 2012 10:17 pm

Re: Why doesn't the main thread need a SETUP_CONTEXT?

Post by skute »

chSysInit() setups the context for the main thread, if it's corrupted then you might have a stack overflow.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Why doesn't the main thread need a SETUP_CONTEXT?

Post by Giovanni »

Correct, it uses the same macro, there must be some other detail causing this. All ports do the same thing for the main() thread, it is not port-dependent.

Giovanni
xnr
Posts: 9
Joined: Fri Jul 18, 2014 4:49 pm

Re: Why doesn't the main thread need a SETUP_CONTEXT?

Post by xnr »

I can see that mainthread is passed to _thread_init, but that doesn't use SETUP_CONTEXT. Could you point me to the code line or provide a call stack?

(It's not that I don't believe you, but I want to understand where the main thread's context is setup)
skute
Posts: 64
Joined: Wed Aug 29, 2012 10:17 pm

Re: Why doesn't the main thread need a SETUP_CONTEXT?

Post by skute »

SETUP_CONTEXT isn't required because the main thread is already running when it is initialized. All other threads need the SETUP_CONTEXT so that the first context switch can happen successfully.

After the chSysInit() the context gets pushed to the main thread stack in the same way as all other threads (because of an interrupt or a blocking OS call).
Post Reply