Skip to content
GeMRTOS
GeMRTOS

The Generic eMbedded Multiprocessor RTOS

  • Home
  • Download Now!
  • GeMRTOS
    • License
    • Download
    • GeMRTOS Documentation
      • Documentation Browser
      • GeMRTOS Manuals
    • GeMRTOS repository
  • Log in
  • Contact us
Search
GeMRTOS
GeMRTOS

The Generic eMbedded Multiprocessor RTOS

  • Download Now!!!
  • GeMRTOS
    • License
    • Download now!
    • GeMRTOS documentation
    • GeMRTOS repository
  • Login
  • Contact us
  • FPGA Laboratory ACCESS
  • Cursos de FPGA (en español)
  • Challenges
GeMRTOS
GeMRTOS

The Generic eMbedded Multiprocessor RTOS

  • Home
  • Download Now!
  • GeMRTOS
    • License
    • Download
    • GeMRTOS Documentation
      • Documentation Browser
      • GeMRTOS Manuals
    • GeMRTOS repository
  • Log in
  • Contact us
Search
GeMRTOS
GeMRTOS

The Generic eMbedded Multiprocessor RTOS

  • Download Now!!!
  • GeMRTOS
    • License
    • Download now!
    • GeMRTOS documentation
    • GeMRTOS repository
  • Login
  • Contact us
  • FPGA Laboratory ACCESS
  • Cursos de FPGA (en español)
  • Challenges

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

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

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
  • Home
  • GeMRTOS KnowledgeBase
  • GeMRTOS
  • GeMRTOS Semaphores: Binary and Counting API for Nios V RTOS

GeMRTOS Semaphores: Binary and Counting API for Nios V RTOS

In GeMRTOS on Altera FPGA platforms with Nios V processors, a semaphore is a resource for preventing concurrent execution of code sections. GeMRTOS supports binary semaphores, counting semaphores, and (built on the same underlying resource) mutexes — see the companion article on Mutex and Critical Sections for mutex-specific creation and usage. When a task requests a semaphore, access is granted only if the current count is greater than 0 — granting decrements the count; releasing increments it. Requests can be configured as blocking (with an optional timeout) or non-blocking.

Semaphore API Reference #

Creating a Semaphore #

GeMRTOS provides a dedicated creation function per semaphore kind, rather than a single generic constructor:

GS_RCB *gu_SemaphoreCreateCounting(int max_count, int initial_count);
GS_RCB *gu_SemaphoreCreateBinary(int initial_count);

gu_SemaphoreCreateCounting allocates a counting semaphore, letting up to max_count tasks hold it concurrently, starting at initial_count. gu_SemaphoreCreateBinary allocates a binary semaphore (0 = empty, 1 = available) — equivalent to a counting semaphore with max_count = 1, exposed as its own function for clarity at the call site. Both build on the same GS_RCB structure, with T_SEMAPHORE_RESOURCE fields appended, and both must be called before any gu_SemaphoreTake/gu_SemaphoreGive call on that resource.

ParameterDescription
max_count(Counting only) The maximum count value the semaphore can reach — once at this value, it can no longer be given.
initial_countThe count assigned to the semaphore when created.

Returns: a handle (GS_RCB *) to the newly created semaphore, used in all subsequent calls, or NULL if creation failed.

Requesting (Taking) a Semaphore #

G_UINT32 gu_SemaphoreTake(GS_RCB *presource, G_UINT64 ticks_to_wait);

gu_SemaphoreTake requests a semaphore. If the current count is greater than 0, it is granted and the count is decremented. If the count is 0, the calling task is suspended until the semaphore becomes available or ticks_to_wait elapses.

ParameterDescription
presourceHandle returned by a gu_SemaphoreCreate* function.
ticks_to_waitTime to wait, in system ticks. Pass 0 to return immediately without blocking; pass G_LATEST_TIME to wait indefinitely.

Returns: G_TRUE if the semaphore was granted, G_FALSE if it was unavailable and the timeout elapsed.

Releasing (Giving) a Semaphore #

G_UINT32 gu_SemaphoreGive(GS_RCB *presource);

gu_SemaphoreGive releases a held semaphore. If one or more tasks are waiting, the highest-priority waiting task is granted it directly; otherwise the count is simply incremented.

ParameterDescription
presourceHandle returned by a gu_SemaphoreCreate* function.

Returns: G_TRUE on success, G_FALSE otherwise.

Destroying a Semaphore #

G_UINT32 gu_SemaphoreDestroy(GS_RCB *psemaphore);

gu_SemaphoreDestroy returns a semaphore’s GS_RCB to the free pool. It fails (returns G_FALSE) if any task currently holds or is waiting on it — a semaphore must be idle before it can be destroyed.

T_SEMAPHORE_RESOURCE Structure #

The T_SEMAPHORE_RESOURCE structure is embedded as the semaphore field within a GS_RCB resource. Access its fields using: (GS_RCB *)->semaphore.<field>

Structure Fields #

TypeFieldDescription
TIMEPRIORITYSEM_GrantedPriorityPriority assigned to the task the semaphore is granted to.
G_UINT32SEM_Current_CountCurrent count. When 0, no more grants are allowed. Initialized to SEM_Maximum_Count at creation.
G_UINT32SEM_Maximum_CountMaximum number of times the semaphore can be taken.
G_UINT32SEM_RecurrenceFor a recursive mutex, the number of times it has been taken by its current holder.

Key Takeaways #

  • Each semaphore kind has its own creation function — gu_SemaphoreCreateCounting, gu_SemaphoreCreateBinary, and (for mutexes) gu_SemaphoreCreateMutex / gu_SemaphoreCreateRecursiveMutex — rather than one generic constructor with a mode flag.
  • gu_SemaphoreTake blocks the calling task, at its own priority, for up to ticks_to_wait system ticks; pass 0 for a non-blocking attempt or G_LATEST_TIME to wait indefinitely.
  • gu_SemaphoreGive always grants the highest-priority waiting task first; if none is waiting, it just increments the count.
  • A semaphore must be created before any gu_SemaphoreTake/gu_SemaphoreGive call on it — creating one from inside a task is fine as long as it happens before any task’s first use of it.
  • gu_SemaphoreDestroy returns a semaphore to the free pool, but only once it’s idle (no holder, no waiters).
  • The T_SEMAPHORE_RESOURCE structure (via (GS_RCB *)->semaphore.field) exposes SEM_Current_Count, SEM_GrantedPriority, and — for recursive mutexes — SEM_Recurrence for runtime inspection.
Avalon MM, GeMRTOS, Nios 2, Nios V, RISC-V
Share This Article :
  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest
Still stuck? How can we help?

How can we help?

Leave a Reply Cancel reply

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

Table of Contents
  • Semaphore API Reference
    • Creating a Semaphore
    • Requesting (Taking) a Semaphore
    • Releasing (Giving) a Semaphore
    • Destroying a Semaphore
  • T_SEMAPHORE_RESOURCE Structure
    • Structure Fields
  • 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.
GeMRTOS is property of R. Cayssials.
**All contact forms on this site are protected by reCAPTCHA. Google Privacy

GeMRTOS joins Intel Partner Alliance as Gold member.