<p>One of the problems, when writing embedded multi-threaded applications, is that the thread functions do save the registers in the function entry code even if the system does not require it, exiting such a function would terminate the thread so there is no need to preserve the register values. This can waste tens of bytes for each thread.<br/>
<divclass="fragment"><preclass="fragment"><spanclass="preprocessor">#include <<aclass="code"href="ch_8h.html"title="ChibiOS/RT main include file.">ch.h</a>></span>
<spanclass="keyword">static</span><aclass="code"href="group__core.html#gac8b681d521d3b6c25e7a0304674732c9"title="Static working area allocation.">WORKING_AREA</a>(waMyThread, 64);
<aclass="code"href="group__threads.html#ga048e588238dd336c6059133b3d0c3435"title="Creates a new thread into a static memory area.">chThdCreateStatic</a>(waMyThread, <spanclass="keyword">sizeof</span>(waMyThread), <aclass="code"href="group__scheduler.html#gad6a6b28f0c1cf8e0da714ef771fb90a0"title="Normal user priority.">NORMALPRIO</a>, MyThread, NULL);
...
}
</pre></div><p> The resulting ASM code for the thread function would be something like this: </p>
</pre></div><p> Being that function a thread there is no need to save those registers, in embedded applications often the RAM is a scarce resource. That space can be saved by modifying the code as follow, using some advanced GCC extensions: </p>
<divclass="fragment"><preclass="fragment"><spanclass="preprocessor">#include <<aclass="code"href="ch_8h.html"title="ChibiOS/RT main include file.">ch.h</a>></span>
<spanclass="keyword">static</span><aclass="code"href="group__core.html#gac8b681d521d3b6c25e7a0304674732c9"title="Static working area allocation.">WORKING_AREA</a>(waMyThread, 64);
<aclass="code"href="group___a_r_m_c_mx___c_o_r_e.html#gadb1276c6bdf715206ee694f223112e30"title="Stack and memory alignment enforcement.">__attribute__</a>((noreturn))
<spanclass="comment">/* Do thread inner work */</span>
}
<aclass="code"href="group__threads.html#ga24ab3a3a4d70214ee360867a1c3c75ac"title="Terminates the current thread by specifying an exit status code.">chThdExit</a>(1);
<aclass="code"href="group__threads.html#ga048e588238dd336c6059133b3d0c3435"title="Creates a new thread into a static memory area.">chThdCreateStatic</a>(waMyThread, <spanclass="keyword">sizeof</span>(waMyThread), <aclass="code"href="group__scheduler.html#gad6a6b28f0c1cf8e0da714ef771fb90a0"title="Normal user priority.">NORMALPRIO</a>,
</pre></div><p> This will make GCC believe that the function cannot return and there is no need to save registers. The code will be a bit less readable and less portable on other compilers however. </p>
Generated on Sun Nov 28 2010 14:09:56 for ChibiOS/RT by <ahref="http://www.doxygen.org/index.html"><imgsrc="doxygen.png"alt="doxygen"align="middle"border="0"></a> 1.7.1</small></address>