Nios® V Processor Software Developer Handbook

ID 743810
Date 7/08/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

11.3.4. The newlib ANSI C Standard Library

Programs built on top of MicroC/OS-II can also call the ANSI C standard library functions. Some consideration is necessary in a multi-threaded environment to ensure that the C standard library functions are thread-safe. The newlib C library stores all global variables in a single structure referenced through the pointer _impure_ptr. However, the Intel FPGA MicroC/OS-II implementation creates a new instance of the structure for each task. During a context switch, the value of _impure_ptr is updated to point to the current task's version of this structure. In this way, the contents of the structure pointed to by _impure_ptr are treated as thread local. For example, through this mechanism each task has its own version of errno.

This thread-local data is allocated at the top of the task’s stack. You must make allowance for thread-local data storage when allocating memory for stacks. In general, the _reent structure consumes approximately 900 bytes of data.

In addition, the MicroC/OS-II implementation provides appropriate task locking to ensure that heap accesses (calls to malloc() and free()) are also thread-safe.