Skip to content

posix: implement POSIX semaphore interfaces - #457

Open
oI0ck wants to merge 8 commits into
masterfrom
michal.lach/sem
Open

oI0ck wants to merge 8 commits into
masterfrom
michal.lach/sem

Conversation

@oI0ck

@oI0ck oI0ck commented Jan 29, 2026 •

Copy link
Copy Markdown
Member

TASK: RTOS-1407

Description

This PR introduces implementation of POSIX standard semaphore interfaces.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Chore (refactoring, style fixes, git/CI config, submodule management, no code logic changes)

How Has This Been Tested?

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing linter checks and tests passed.
  • My changes generate no new compilation warnings for any of the targets.

Special treatment

@oI0ck
oI0ck requested a review from Darchiv January 29, 2026 18:07
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @oI0ck, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces comprehensive support for POSIX standard semaphore interfaces within the libphoenix library. It enables developers to use both named and unnamed semaphores for synchronization, integrating with the posixsrv for named semaphore management and extending the core sys/threads functionality for unnamed semaphores. This significantly enhances the concurrency primitives available in the Phoenix-RTOS environment.

Highlights

  • POSIX Semaphore API: Implemented the full suite of POSIX semaphore functions, including sem_open, sem_close, sem_unlink, sem_init, sem_destroy, sem_wait, sem_trywait, sem_timedwait, sem_post, and sem_getvalue.
  • Named and Unnamed Semaphores: The implementation distinguishes between named semaphores (managed via inter-process communication with posixsrv) and unnamed semaphores (managed internally).
  • Core Semaphore Operations: Introduced semaphoreTryDown and semaphoreCount functions in the underlying sys/threads.h and sys/semaphore.c to support the POSIX API.
  • Messaging Integration: Named semaphores utilize msgSend and ioctl calls to communicate with a posixsrv for their operations, defining specific IOCTL commands (SEM_UP, SEM_DOWN, SEM_DOWN_TRY, SEM_DOWN_TIMEOUT).
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

Comment thread include/semaphore.h Outdated

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This PR introduces POSIX semaphore implementation. The changes are extensive, adding new headers and implementation files for named and unnamed semaphores. The overall structure is good, but I've found several critical issues in the implementation, including race conditions, incorrect logic leading to deadlocks, and potential memory corruption. There are also some areas where the code can be simplified and made more robust. Please address the critical issues before merging.

Comment thread posix/sem.c Outdated
Comment thread posix/sem.c Outdated
Comment thread sys/semaphore.c
Comment thread posix/sem.c
Comment thread posix/sem.c Outdated
Comment thread posix/sem.c Outdated
Comment thread sys/semaphore.c Outdated
@github-actions

github-actions Bot commented Jan 29, 2026 •

Copy link
Copy Markdown

Unit Test Results

14 250 tests  +520   13 389 ✅ +518   58m 31s ⏱️ + 1m 59s
   790 suites + 10      859 💤 ±  0 
     1 files   ±  0        2 ❌ +  2 

For more details on these failures, see this check.

Results for commit 63ca129. ± Comparison against base commit 9bffcee.

♻️ This comment has been updated with latest results.

@oI0ck
oI0ck marked this pull request as draft January 29, 2026 18:37
Comment thread include/semaphore.h Outdated
@oI0ck
oI0ck marked this pull request as ready for review February 2, 2026 16:06
Comment thread sys/semaphore.c Outdated
Comment thread sys/semaphore.c Outdated
Comment thread sys/semaphore.c Outdated
Comment thread posix/sem.c Outdated
Comment thread posix/sem.c Outdated
Comment thread posix/sem.c Outdated
Comment thread posix/sem.c Outdated
Comment thread posix/sem.c
Comment thread include/semaphore.h Outdated
Comment thread include/sys/semaphore.h Outdated
@oI0ck
oI0ck force-pushed the michal.lach/sem branch 2 times, most recently from cf48092 to f3710e3 Compare February 10, 2026 13:19
@oI0ck
oI0ck force-pushed the michal.lach/sem branch from 239fd04 to 706ff80 Compare May 29, 2026 15:51
Comment thread include/sys/semaphore.h Outdated
Comment thread posix/sem.c Outdated
@oI0ck
oI0ck force-pushed the michal.lach/sem branch from 706ff80 to c6a2d17 Compare June 22, 2026 09:18
Comment thread include/sys/semaphore.h Outdated
@datadog-phoenix-rtos

datadog-phoenix-rtos Bot commented Jun 22, 2026 •

Copy link
Copy Markdown

Pipelines

⚠️ Warnings

🚦 3 Pipeline jobs failed

ci | call-ci / build (armv7a9-zynq7000-zedboard)   View in Datadog   GitHub Actions

ci | call-ci / build (armv7m7-imxrt106x-evk)   View in Datadog   GitHub Actions

ci | call-ci / tests-summary   View in Datadog   GitHub Actions

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 057606b | Docs | Give us feedback!

@oI0ck
oI0ck force-pushed the michal.lach/sem branch 2 times, most recently from 23e321c to 057606b Compare June 22, 2026 09:24
@oI0ck

oI0ck commented Jun 22, 2026

Copy link
Copy Markdown
Member Author

@oI0ck
oI0ck force-pushed the michal.lach/sem branch from 057606b to 8b13146 Compare June 30, 2026 12:19
@oI0ck

oI0ck commented Jun 30, 2026

Copy link
Copy Markdown
Member Author

CI shows problems with this change, I'm converting it to a draft until I am sure that it is in mergable state.

@oI0ck
oI0ck marked this pull request as draft June 30, 2026 12:35
@oI0ck
oI0ck force-pushed the michal.lach/sem branch 3 times, most recently from 070fbcb to 67a05ba Compare June 30, 2026 17:32
@oI0ck
oI0ck force-pushed the michal.lach/sem branch from 67a05ba to 75a25a1 Compare July 20, 2026 23:10
@oI0ck
oI0ck force-pushed the michal.lach/sem branch from 74bdc2c to c0eed79 Compare July 28, 2026 10:49
Comment thread posix/sem.c Outdated
Comment thread include/semaphore.h Outdated
Comment thread posix/sem.c Outdated
Comment thread posix/sem.c Outdated
Comment thread posix/sem.c Outdated
Comment thread posix/sem.c Outdated
Comment thread posix/sem.c
Comment thread posix/sem.c Outdated
Comment thread posix/utils.c Outdated
Comment thread posix/utils.c Outdated
Comment thread posix/sem.c Outdated
Comment thread posix/sem.c Outdated
Comment thread posix/sem.c
Comment thread include/semaphore.h
Comment thread include/phoenix/semaphore.h
@oI0ck
oI0ck force-pushed the michal.lach/sem branch 3 times, most recently from 0fcb1c0 to 78ea982 Compare July 29, 2026 12:28
Comment thread posix/sem.c
Comment thread posix/sem.c Outdated
Comment thread posix/sem.c Outdated
Comment thread posix/sem.c
Comment thread posix/sem.c
@oI0ck
oI0ck force-pushed the michal.lach/sem branch 2 times, most recently from 657275d to 169a290 Compare July 31, 2026 15:26
@oI0ck
oI0ck requested a review from ziemleszcz July 31, 2026 15:28
@ziemleszcz

Copy link
Copy Markdown
Contributor

Please rebase thsi PR and resolve conficts.

@ziemleszcz ziemleszcz self-assigned this Sep 25, 2026
@ziemleszcz
ziemleszcz requested a review from a team September 25, 2026 15:01
No functional change.

TASK: RTOS-1407
Comment thread include/semaphore.h Outdated
Comment thread include/assert.h Outdated
Comment thread sys/semaphore.c
Comment thread posix/sem.c
Comment thread posix/sem.c
oI0ck and others added 7 commits October 2, 2026 13:25
semaphoreUp() incremented the counter unconditionally, so a semaphore
posted SEM_VALUE_MAX times wrapped silently. POSIX requires sem_post() to
fail with EOVERFLOW instead, so report -EOVERFLOW and leave the value
untouched.

semaphoreCreate() accepted any initial value. Reject one above the
limit, and a NULL semaphore.

TASK: RTOS-1407
sem_getvalue() and sem_trywait() need a way to read the counter and to take
the semaphore without blocking. Add the two primitives they are built on.

Both take the internal mutex with mutexLock() rather than mutexTry():
contention on it is an implementation detail, not a property of the
semaphore, so letting it surface would make semaphoreCount() report a
spurious 0 and semaphoreTryDown() fail while the semaphore is free.

TASK: RTOS-1407
Assisted-by: claude-opus-5
semaphoreDown() takes a relative timeout, resolves it against the monotonic
clock and waits on the condvar's own clock. sem_timedwait() is specified in
terms of an absolute CLOCK_REALTIME deadline, which that interface cannot
express without the caller converting between clock domains by hand.

Add semaphoreDownAtClock(), which passes the deadline and the clock straight
to condClockWait() and lets the kernel resolve both. A deadline that has
already passed comes back as -ETIME before the caller is parked, so the
value is still checked first and an available semaphore is still taken. The
one value the kernel cannot express is 0, which it reads as "no deadline",
so the epoch is turned into -ETIME here.

PH_CLOCK_RELATIVE is rejected: the retry loop hands the same value to every
condClockWait() call, which would restart a relative timeout after each
wakeup.

TASK: RTOS-1407
Assisted-by: claude-opus-5
Add the POSIX semaphore API. Unnamed semaphores are backed directly by
libphoenix's semaphore_t. Named ones live in posixsrv, which exposes each of
them as a device node under /dev/posix/sem/.

A named sem_t holds an open descriptor on that node rather, so operations
travel as ordinary ioctl() calls and the kernel drops the server-side reference
when the process exits. The descriptor is opened O_CLOEXEC, giving the POSIX
requirement that named semaphores are closed across a successful exec().

TASK: RTOS-1407
Assisted-by: claude-opus-5
semaphoreUp() signalled only on the 0 -> 1 transition, assuming that a positive
value meant there were no waiters. This is incorrect. Track the number of waiters
and signal whenever a waiter is present.

TASK: RTOS-1407
Comment thread sys/semaphore.c
Comment on lines +125 to +129
err = condClockWait(s->cond, s->mutex, deadline, clock);
--s->waiters;
} while ((err == 0) || (err == -EINTR));

mutexUnlock(s->mutex);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

condClockWait may return with lock not held

Comment on lines +32 to +34
/* not required by POSIX, but ensures that
* sem_getvalue() does not return an overflowed value.
*/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* not required by POSIX, but ensures that
* sem_getvalue() does not return an overflowed value.
*/
/*
* not required by POSIX, but ensures that
* sem_getvalue() does not return an overflowed value.
*/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants