Confusing document about wa size when creating thread Topic is solved

Report here problems in any of ChibiOS components. This forum is NOT for support.
xiaomin
Posts: 6
Joined: Mon Jul 24, 2017 1:43 pm
Has thanked: 1 time

Confusing document about wa size when creating thread

Postby xiaomin » Thu Oct 18, 2018 7:37 pm

file: chdynamic.c; function: chThdCreateFromHeap
file: chthreads.c; function: chThdCreateStatic

When creating static thread, the stack size is defined in THD_WORKING_AREA macro (chthreads.h). Apparently the size here is stack size.

static THD_WORKING_AREA(xxx_thd_wa, 1000);
chThdCreateStatic(xxx_thd_wa, sizeof(xxx_thd_wa), NORMALPRIO, xxx_thd_fun, NULL)

When creating thread from heap, the wa size is passed to size parameter, but the size here is wa size, which should include not only stack size, but also overhead space for thread_t structure.

chThdCreateFromHeap(heap_mem_p, 1000, "xxx_thd", NORMALPRIO, xxx_thd_fun, NULL);

You can see the same number 1000 will generate different size of stack for the thread in these two cases.
I think either a clear description shall be added to chThdCreateFromHeap parameter document indicating the relationship between wa size and stack size, or a helper macro THD_WORKING_AREA_SIZE shall be recommended to be used in the document.

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

Re: Confusing document about wa size when creating thread  Topic is solved

Postby Giovanni » Thu Oct 18, 2018 7:45 pm

Hi,

Both chThdCreateStatic() and chThdCreateFromHeap() take as parameter the size of the working area, not the stack size, I believe this is documented.

THD_WORKING_AREA creates a working area starting from a stack size, it is meant to have overhead. This macros converts from stack size to WA size:

Code: Select all

 * @brief   Calculates the total Working Area size.
 *
 * @param[in] n         the stack size to be assigned to the thread
 * @return              The total used memory in bytes.
 *
 * @api
 */
#define THD_WORKING_AREA_SIZE(n)                                            \
  MEM_ALIGN_NEXT(sizeof(thread_t) + PORT_WA_SIZE(n), PORT_STACK_ALIGN)


It can be used with chThdCreateFromHeap().

Giovanni


Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 22 guests