Page 1 of 1
Why doesn't the main thread need a SETUP_CONTEXT?
Posted: Fri Jul 18, 2014 5:21 pm
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?
Re: Why doesn't the main thread need a SETUP_CONTEXT?
Posted: Fri Jul 18, 2014 7:11 pm
by skute
chSysInit() setups the context for the main thread, if it's corrupted then you might have a stack overflow.
Re: Why doesn't the main thread need a SETUP_CONTEXT?
Posted: Fri Jul 18, 2014 7:18 pm
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
Re: Why doesn't the main thread need a SETUP_CONTEXT?
Posted: Fri Jul 18, 2014 9:27 pm
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)
Re: Why doesn't the main thread need a SETUP_CONTEXT?
Posted: Fri Jul 18, 2014 11:41 pm
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).