Thread Stacks and Stack Sizes

Synchronization Mechanisms 95 is filled with a signature. The signature is verified every time a thread context switch occurs. If the signature is not valid, this is an indication that a stack overflow has taken place. Another configuration option to aid in thread stack size evaluation is Measure Stack Usage. When this option is enabled, a thread can call cyg_thread_measure_stack_usage to find out the maximum stack used to that point. It is important to realize that this value might not be the absolute maximum since it is possible that no interrupts occurred at the worst possible moment while the thread was executing.

6.2 Synchronization Mechanisms

The eCos kernel provides the mechanisms for the threads in the system to communicate with each other and synchronize access to shared resources. The mechanisms provided by the eCos kernel are: • Mutexes • Semaphores • Condition variables • Flags • Message boxes • Spinlocks for SMP systems The kernel also provides API functions that allow applications to make use of the synchro- nization mechanisms. Some of the synchronization mechanism API functions provided are either blocking or nonblocking. Blocking function calls, such as cyg_semaphore_wait, halt execution of the thread until the API function can complete successfully. Nonblocking function calls, such as cyg_semaphore_trywait, attempt to complete successfully; however, if the API function is not successful, a return code indicates the status of the call so the thread can proceed with its execution. Another type of blocking call, blocking with timeout, also exists for certain synchroniza- tion mechanisms. These are API functions that halt execution of the thread for a specified period of time, such as cyg_semaphore_timed_wait, while attempting to complete the function call successfully. If the function does not complete successfully before the timeout period, the function returns, indicating an unsuccessful status.

6.2.1 Mutexes

The first synchronization mechanism provided by eCos is the mutex. A mutex mutual exclusion object allows multiple threads to share a resource serially. The resource can be an area of mem- ory or a piece of hardware, such as a Direct Memory Access DMA controller. A mutex is similar to a binary semaphore in that it only has two states—locked and unlocked. However, there are a couple of differences between a binary semaphore and a mutex. 96 Chapter 6 • Threads and Synchronization Mechanisms