Skip to content
GeMRTOS
GeMRTOS

The Generic eMbedded Multiprocessor RTOS

  • Home
  • Download
  • Docs
    • Documentation Browser
    • GeMRTOS Manuals
    • GeMRTOS repository
    • License
  • Services
  • Contact us
Search
GeMRTOS
GeMRTOS

The Generic eMbedded Multiprocessor RTOS

  • Download
  • Docs
    • Documentation Browser
    • Manuals
    • GeMRTOS repository
    • License
  • Services
  • Contact us
GeMRTOS
GeMRTOS

The Generic eMbedded Multiprocessor RTOS

  • Home
  • Download
  • Docs
    • Documentation Browser
    • GeMRTOS Manuals
    • GeMRTOS repository
    • License
  • Services
  • Contact us
Search
GeMRTOS
GeMRTOS

The Generic eMbedded Multiprocessor RTOS

  • Download
  • Docs
    • Documentation Browser
    • Manuals
    • GeMRTOS repository
    • License
  • Services
  • Contact us

GeMRTOS - Features

  • GeMRTOS Mutex and Critical Sections in Multiprocessor RTOS
  • GeMRTOS Signals: Runtime Exception Handling for Nios V RTOS
  • GeMRTOS System Architecture – Multiprocessor Design with Altera Nios V Processors
  • Hybrid Partition Scheduling in GeMRTOS, Multiprocessor RTOS for Altera FPGA
  • GeMRTOS Scheduling Lists: EDF and Fixed Priority RTOS Guide
  • GeMRTOS Tasks: Types, Creation, and Scheduling for Nios V
  • GeMRTOS Trigger Resources: Event Handling Beyond Interrupts
  • GeMRTOS Controller: Complete FPGA RTOS Hardware Guide
  • Data Structures in GeMRTOS: Control Blocks and Linked Lists
  • GeMRTOS Semaphores: Binary and Counting API for Nios V RTOS

GeMRTOS - Getting started

  • Introduction to GeMRTOS
  • Getting Started with GeMRTOS: Multiprocessor RTOS for Altera FPGA (Nios II & Nios V)
  • API references
  • Platform Designer Flow for GeMRTOS Nios V FPGA Development
  • GeMRTOS RTOS Task Periods: Infinite Loop vs Periodic Tasks
  • Install Quartus Prime and WSL for GeMRTOS Nios V on Windows

General

  • Error (16031): Current Internal Configuration mode does not support memory initialization or ROM. Select Internal Configuration mode with ERAM.
  • newlib Thread Safety in GeMRTOS Nios V Multiprocessor RTOS
  • Set Up Questa-Intel FPGA Edition License for GeMRTOS Nios V
  • Installing Nios II Software Built Tools (SBT) for Eclipse in Quartus Prime starting from version 19.1
  • Fix Quartus Prime 23.1 Fatal Error When Creating ALTPLL IP
  • Fix Missing SDRAM Controller IP in Quartus Prime for GeMRTOS
  • GeMRTOS Nios: Fix 256MB Compile Boundary with -relax-all
  • Questa Simulation Setup for GeMRTOS Nios V: Two Common Fixes
  • Quartus Warning 113015: mem_init.hex Width Mismatch BSP Fix
  • GeMRTOS Secondary Processors Not Booting: nios2-download Fix
  • Fix Quartus Prime System Console: jvm.dll and awt.dll Errors
  • Eclipse does not start after full Quartus Prime instalation
View Categories
  • Home
  • GeMRTOS KnowledgeBase
  • GeMRTOS
  • newlib Thread Safety in GeMRTOS Nios V Multiprocessor RTOS

newlib Thread Safety in GeMRTOS Nios V Multiprocessor RTOS

0:00 / 3:15
  • View as Markdown Open this page as plain Markdown (opens in a new tab)
  • Open in ChatGPT Ask questions about this page (opens in a new tab)
  • Open in Claude Ask questions about this page (opens in a new tab)
  • Open in Google AI Studio Ask questions about this page (Google account required) (opens in a new tab)

In GeMRTOS on Altera FPGA platforms with Nios V processors, newlib functions are reentrant — but reentrancy does not guarantee thread safety. In uniprocessor multiprocessing systems, _impure_ptr is used to give newlib functions per-task reentrancy state, making them effectively thread-safe through context switching. However, in multiprocessor systems, this approach breaks down: multiple tasks can execute simultaneously on different processors, so changing _impure_ptr per task is no longer valid. This article explains the distinction and the GeMRTOS solution.

Reentrancy vs Thread Safety in newlib #

Reentrancy means a function can be interrupted mid-execution and safely called again before the first call completes — typically by storing all state in caller-provided or per-call structures rather than global variables. newlib achieves reentrancy by storing per-task state in a struct _reent structure pointed to by _impure_ptr.

Thread safety is a stricter guarantee: a function can be called concurrently by multiple threads or tasks running at the same time without corrupting shared state. In a uniprocessor RTOS, tasks never truly run simultaneously — the scheduler switches _impure_ptr to each task’s own struct _reent during context switches, making newlib calls effectively thread-safe by serialization.

Why _impure_ptr Fails in Multiprocessor Systems #

In a multiprocessor system, two or more tasks can execute simultaneously on separate processors. If both tasks call a newlib function at the same time, both access _impure_ptr concurrently. Since _impure_ptr is a single global pointer, there is no guarantee that each processor is referencing its own task’s struct _reent — a context switch on one processor can change _impure_ptr while another processor is mid-execution of a newlib call that depends on it. This leads to corrupted reentrancy state and unpredictable behaviour.

The root problem is that the uniprocessor assumption — only one task runs at a time — no longer holds. Serializing _impure_ptr switches through context switching is insufficient when multiple processors execute tasks in parallel.

The GeMRTOS Solution: Hardware Mutex #

GeMRTOS provides a hardware mutex — GRTOS_USER_CRITICAL_SECTION_GET and GRTOS_USER_CRITICAL_SECTION_RELEASE — that enforces mutual exclusion across all processors at the hardware level. When a task needs to call a newlib function that is not multiprocessor-safe, it should bracket the call with the GeMRTOS mutex to prevent concurrent access:

  1. Acquire the GeMRTOS mutex with GRTOS_USER_CRITICAL_SECTION_GET.
  2. Execute the newlib function call.
  3. Release the mutex with GRTOS_USER_CRITICAL_SECTION_RELEASE.

Important: while the GeMRTOS mutex is held, all GeMRTOS kernel functions are suspended until it is released. Avoid holding the mutex while accessing slow I/O devices or performing lengthy operations, as this will block the entire RTOS kernel across all processors for the duration.

Key Takeaways #

  • Reentrancy ≠ thread safety: newlib functions are reentrant via _impure_ptr / struct _reent, but this only provides thread safety when tasks are serialized by a uniprocessor scheduler.
  • In a GeMRTOS multiprocessor Nios V system, multiple tasks run simultaneously on separate processors — changing _impure_ptr per task during context switches no longer prevents concurrent access.
  • The GeMRTOS hardware mutex (GRTOS_USER_CRITICAL_SECTION_GET / RELEASE) is the correct mechanism for making newlib calls safe across all processors.
  • Keep mutex hold-time minimal — the entire GeMRTOS kernel is suspended while the hardware mutex is held, so it must not wrap slow I/O operations.
  • This issue applies to any library or code that relies on global or shared state; the same mutex pattern should be used for any non-multiprocessor-safe function called from GeMRTOS tasks.
Avalon MM, GeMRTOS, Nios 2, Nios V, RISC-V

Share This Article :

  • Facebook
  • X
  • LinkedIn
  • Pinterest
Still stuck? How can we help?

How can we help?

2 thoughts on “newlib Thread Safety in GeMRTOS Nios V Multiprocessor RTOS”

  1. Dave Nadler
    August 9, 2026 at 7:24 pm

    I think the above is not correct? newlib has a poorly documented feature… Build newlib adding -D__DYNAMIC_REENT__ to CFLAGS build options), and Newlib dynamically obtains `_REENT` through `__getreent()` instead of using a single global `-impure_ptr`. In turn, `__getreent()` must be implemented to return the appropriate reentrancy structure for the current processor, task management must allocate it, and maintain thread-specific reentrancy pointers during task switching etc. You should get better performance using this feature than a hardware mutex perhaps blocking for a long time…

    Would appreciate a reply at DRN@Nadler.com, Thanks!
    Best Regards, Dave

    Reply
    1. Prof. Ricardo Cayssials, PhD
      October 21, 2026 at 12:47 am

      Hi Dave,
      Please accept my apologies for the delayed reply. In the GeMRTOS implementation, we have evaluated Newlib’s reent structure, but not as a replacement for the mutex. The mutex serves a different purpose: preserving the consistency of the operating system while it is running.

      There were two main reasons we decided not to adapt Newlib’s reent mechanism:

      1) Newlib’s dynamic reentrancy support is primarily intended for multithreaded, single-processor systems. Adapting it for a multiprocessor system would require substantial work, which might need to be repeated with each new Newlib version.
      2) Any such modification would require rebuilding Newlib, which could make the application development workflow considerably more cumbersome.

      If these circumstances change, we will reconsider using this feature.

      Thank you for reaching out. We appreciate your message and remain at your disposal.

      Kind regards,
      Ricardo Cayssials
      GeMRTOS

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Table of Contents
  • Reentrancy vs Thread Safety in newlib
  • Why _impure_ptr Fails in Multiprocessor Systems
  • The GeMRTOS Solution: Hardware Mutex
  • Key Takeaways

Copyright © 2026 - contact us - Dorrego 287 - B8000FLE - Bahía Blanca - Argentina - +542914311867  GeMRTOS

Nios II, Nios V, Quartus Prime, ModelsSim are property of their respective companies.
Altera, Quartus, Cyclone, MAX and Agilex are trademarks of Altera Corporation.
GeMRTOS is property of R. Cayssials.
**All contact forms on this site are protected by reCAPTCHA. Google Privacy

Altera Solution Acceleration Partner - Member

GeMRTOS is a Member in Altera’s ASAP Program