Nios® V Processor Software Developer Handbook

ID 743810
Date 10/12/2024
Public
Document Table of Contents

13.2.4. The newlib ANSI C Standard Library

Programs built on top of FreeRTOS can also call the ANSI C standard library functions. Consider the following scenarios 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. The Intel FPGA FreeRTOS implementation creates a new instance of the structure for each task. During a context switch, Nios® V processor updates the value of _impure_ptr to point to the current task's version of this structure. In this way, Nios® V processor treats the contents of the structure pointed to by _impure_ptr as thread local. For example, through this mechanism each task has its own version of errno. Nios® V processor allocates this thread-local data 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.
  • The FreeRTOS implementation provides appropriate task locking (_malloc_lock()/unlock() and _env_lock()/unlock()) to ensure that heap accesses (calls to malloc(), free(), etc(), and setenv()) are also thread-safe. By default, the freertos.os_thread_safe_newlib flag is set to 1 in the Board Support Package Editor to enable the thread-safe newlib library.