Valid contexts, Example – Comtrol eCos User Manual
Page 41

Thread creation
of space is reserved at the stack limit and filled with a special signature: every time a thread context switch occurs
this signature is checked, and if invalid that is a good indication (but not absolute proof) that a stack overflow has
occurred. This form of stack checking is enabled by default when the system is built with debugging enabled. A
related configuration option is
CYGFUN_KERNEL_THREADS_STACK_MEASUREMENT
: enabling this option means that
a thread can call the function
cyg_thread_measure_stack_usage
to find out the maximum stack usage to date.
Note that this is not necessarily the true maximum because, for example, it is possible that in the current run no
interrupt occurred at the worst possible moment.
Valid contexts
cyg_thread_create
may be called during initialization and from within thread context. It may not be called from
inside a DSR.
Example
A simple example of thread creation is shown below. This involves creating five threads, one producer and four
consumers or workers. The threads are created in the system’s
cyg_user_start
: depending on the configuration
it might be more appropriate to do this elsewhere, for example inside
main
.
#include
<
cyg/hal/hal_arch.h
>
#include
<
cyg/kernel/kapi.h
>
// These numbers depend entirely on your application
#define NUMBER_OF_WORKERS
4
#define PRODUCER_PRIORITY
10
#define WORKER_PRIORITY
11
#define PRODUCER_STACKSIZE
CYGNUM_HAL_STACK_SIZE_TYPICAL
#define WORKER_STACKSIZE
(CYGNUM_HAL_STACK_SIZE_MINIMUM + 1024)
static unsigned char producer_stack[PRODUCER_STACKSIZE];
static unsigned char worker_stacks[NUMBER_OF_WORKERS][WORKER_STACKSIZE];
static cyg_handle_t producer_handle, worker_handles[NUMBER_OF_WORKERS];
static cyg_thread_t producer_thread, worker_threads[NUMBER_OF_WORKERS];
static void
producer(cyg_addrword_t data)
{
...
}
static void
worker(cyg_addrword_t data)
{
...
}
void
cyg_user_start(void)
{
int i;
41