Thread priorities, Stacks and stack sizes – Comtrol eCos User Manual

Page 40

Advertising
background image

Thread creation

The second argument to

cyg_thread_create

is a pointer to such a function. The third argument

entry_data

is used to pass additional data to the function. Typically this takes the form of a pointer to some static data, or a
small integer, or

0

if the thread does not require any additional data.

If the thread entry function ever returns then this is equivalent to the thread calling

cyg_thread_exit

. Even

though the thread will no longer run again, it remains registered with the scheduler. If the application needs to
re-use the cyg_thread data structure then a call to

cyg_thread_delete

is required first.

Thread Priorities

The

sched_info

argument provides additional information to the scheduler. The exact details depend on the

scheduler being used. For the bitmap and mlqueue schedulers it is a small integer, typically in the range 0 to 31,
with 0 being the highest priority. The lowest priority is normally used only by the system’s idle thread. The exact
number of priorities is controlled by the kernel configuration option

CYGNUM_KERNEL_SCHED_PRIORITIES

.

It is the responsibility of the application developer to be aware of the various threads in the system, including those
created by eCos packages, and to ensure that all threads run at suitable priorities. For threads created by other
packages the documentation provided by those packages should indicate any requirements.

The

functions

cyg_thread_set_priority

,

cyg_thread_get_priority

,

and

cyg_thread_get_current_priority

can be used to manipulate a thread’s priority.

Stacks and Stack Sizes

Each thread needs its own stack for local variables and to keep track of function calls and returns. Again it is
expected that this stack is provided by the calling code, usually in the form of static data, so that the kernel does not
need any dynamic memory allocation facilities.

cyg_thread_create

takes two arguments related to the stack, a

pointer to the base of the stack and the total size of this stack. On many processors stacks actually descend from
the top down, so the kernel will add the stack size to the base address to determine the starting location.

The exact stack size requirements for any given thread depend on a number of factors. The most important is
of course the code that will be executed in the context of this code: if this involves significant nesting of
function calls, recursion, or large local arrays, then the stack size needs to be set to a suitably high value.
There are some architectural issues, for example the number of cpu registers and the calling conventions
will have some effect on stack usage. Also, depending on the configuration, it is possible that some other
code such as interrupt handlers will occasionally run on the current thread’s stack. This depends in
part on configuration options such as

CYGIMP_HAL_COMMON_INTERRUPTS_USE_INTERRUPT_STACK

and

CYGSEM_HAL_COMMON_INTERRUPTS_ALLOW_NESTING

.

Determining an application’s actual stack size requirements is the responsibility of the application developer,
since the kernel cannot know in advance what code a given thread will run. However, the system does provide
some hints about reasonable stack sizes in the form of two constants:

CYGNUM_HAL_STACK_SIZE_MINIMUM

and

CYGNUM_HAL_STACK_SIZE_TYPICAL

. These are defined by the appropriate HAL package. The

MINIMUM

value is

appropriate for a thread that just runs a single function and makes very simple system calls. Trying to create a
thread with a smaller stack than this is illegal. The

TYPICAL

value is appropriate for applications where application

calls are nested no more than half a dozen or so levels, and there are no large arrays on the stack.

If the stack sizes are not estimated correctly and a stack overflow occurs, the probably result is some form of
memory corruption. This can be very hard to track down. The kernel does contain some code to help detect stack
overflows, controlled by the configuration option

CYGFUN_KERNEL_THREADS_STACK_CHECKING

: a small amount

40

Advertising