Skip to content

posix: implement POSIX semaphore interfaces - #457

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

posix: implement POSIX semaphore interfaces#457
oI0ck wants to merge 4 commits into
masterfrom
michal.lach/sem

Conversation

@oI0ck

@oI0ck oI0ck commented Jan 29, 2026

Copy link
Copy Markdown
Member

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
@github-actions

github-actions Bot commented Jan 29, 2026

Copy link
Copy Markdown

Unit Test Results

12 396 tests  +1 089   11 618 ✅ +1 018   55m 45s ⏱️ + 1m 54s
   756 suites +   66      777 💤 +   70 
     1 files   ±    0        1 ❌ +    1 

For more details on these failures, see this check.

Results for commit 78ea982. ± Comparison against base commit b7974a5.

♻️ 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
Comment thread sys/semaphore.c Outdated
Comment thread sys/semaphore.c
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 Outdated
Comment thread include/semaphore.h

#define SEM_FAILED ((sem_t *)0xDAAB0000)

typedef struct _sem_t {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

_sem_t identifier is not used anywhere. If it's not required by POSIX - remove it.

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
#define SEMCTL_PATH ("/dev/posix/semctl")
#define SEMAPHORE_MAX_COUNT (128)
#define SEMAPHORE_NAME_MAX (NAME_MAX - sizeof(SEMAPHORE_PATH) - 1)
#define SEM_VALUE_MAX INT_MAX

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.

  1. I think this should be defined per-architecture in libphoenix/include/arch/*/limits.h (like NAME_MAX). It can be just #define SEM_VALUE_MAX INT_MAX, but the important part is that the definition should be available via limits.h.

  2. Is SEMAPHORE_MAX_COUNT used anywhere? Is should be SEM_NSEMS_MAX and defined in limits.h like in the point above. Please actually enforce the limit. Note that _POSIX_SEM_NSEMS_MAX == 256 which is the minimum value for this limit.

  3. See sysconf(). You could implement _SC_SEM_VALUE_MAX and _SC_SEM_NSEMS_MAX.

  4. Also, this is a good place to static-assert SEM_VALUE_MAX with _POSIX_SEM_VALUE_MAX and SEM_NSEMS_MAX with _POSIX_SEM_NSEMS_MAX in case someone changes limits.h incorrectly. Maybe also check whether the limit does not exceed the semaphore value type size (unsigned int).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Applied

Comment thread posix/sem.c Outdated
else {
}

sem = (sem_t *)malloc(sizeof(*sem));

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.

Type cast redundant

@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 2 times, most recently from a3396ff to 74bdc2c Compare July 28, 2026 10:13
@oI0ck
oI0ck marked this pull request as ready for review July 28, 2026 10:33
@oI0ck
oI0ck requested review from a team and julianuziemblo July 28, 2026 10:33
@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
if (strcmp(name, dent->d_name) == 0) {
strcpy(path, SEMAPHORE_PATH);
strncpy(path + strlen(SEMAPHORE_PATH), dent->d_name, PATH_MAX - strlen(SEMAPHORE_PATH));
lookup(path, NULL, &oid);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lookup could have failed here, leading to uninitialized dev oid

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Right, I got TOCTOU'd, nice catch.

Comment thread include/semaphore.h Outdated
#include <sys/threads.h>
#include <sys/semaphore.h>

#define SEM_FAILED ((sem_t *)0xDAAB0000)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why this value in particular?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Why not?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

are you sure this cannot be an allocated address on any of our targets?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Will change to NULL

Comment thread posix/sem.c Outdated

dirp = opendir(SEMAPHORE_PATH);
if (dirp == NULL) {
/* posixsrv has yet not initialized the semaphore subsystem */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nitpick: not yet

Comment thread posix/sem.c Outdated
return SEM_FAILED;
}

sem = (sem_t *)malloc(sizeof(*sem));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nitpick: unnecessary cast

Comment thread posix/sem.c
unsigned int value = 0;
sem_t *sem;

(void)mode;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

does POSIX not enforce passing mode along? or we just don't want to handle it for now? in the second case, add a TODO here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

create_dev() does not take a mode parameter, there is a TODO in there to add it. Devices are created by default with 0666 mode.

I can leave a comment and revisit this after this in another PR.

Comment thread posix/sem.c
if (sem->type == smNamed) {
msg.type = mtClose;
msg.oid = sem->named;
ret = msgSend(sem->named.port, &msg);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if (ret == 0) ret = msg.o.err probably

Comment thread posix/sem.c

new.type = smUnnamed;
ret = semaphoreCreate(&new.unnamed, value);
*sem = new;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nitpick: this is set unconditionally. What is semaphoreCreate failed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Will change this to returning -ENOSPC if semaphoreCreate() fails. Seems like the most appropriate errno that is compilant with POSIX.

Comment thread posix/sem.c Outdated
if (ret == EOK) {
msg.type = mtDestroy;
msg.oid = oid;
ret = msgSend(oid.port, &msg);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if (ret == 0) ret = msg.o.err probably

Comment thread posix/utils.c Outdated
Comment on lines +23 to +54
if (path == NULL) {
errno = EINVAL;
return;
}

slash = strrchr(path, '/');

if (slash == NULL) {
*dir = ".";
*base = path;
if (dir != NULL) {
*dir = ".";
}

if (base != NULL) {
*base = path;
}
}
else if (slash == path) {
*base = path + 1;
*dir = "/";
if (base != NULL) {
*base = path + 1;
}

if (dir != NULL) {
*dir = "/";
}
}
else {
*dir = path;
*base = slash + 1;
if (dir != NULL) {
*dir = path;
}

if (base != NULL) {
*base = slash + 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

but.. why?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I wanted to make dir and base optional if you only want one of these. Since now I use destroy_dev(), I can drop this commit

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd drop it since it somewhat obfuscates the function, but that's not a bad idea IMO

Comment thread posix/utils.c Outdated
Comment on lines +23 to +27
if (path == NULL) {
errno = EINVAL;
return;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this seems especially wrong as now we could silently change errno but return 0 (from the calling function).

Comment thread posix/sem.c
break;
}

if (strcmp(name, dent->d_name) == 0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Leading / is not stripped from name as it in semaphore_create() from phoenix-rtos-posixsrv.

Comment thread posix/sem.c
now += offs;

timeout += abs_timeout->tv_sec * 1000000 + abs_timeout->tv_nsec / 1000;
timeout -= now;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note that with absolute timeout this could be negative value (timeout could be in the past) which is not handled correctly by sem_msg_down (results in unbounded down).

Comment thread posix/sem.c
else {
ret = -EINVAL;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

sem memory is not freed.

Comment thread include/semaphore.h
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <time.h>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Missing C++ support (extern "C).

Comment thread include/sys/semaphore.h
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <assert.h>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Missing C++ support (extern "C).

@oI0ck
oI0ck force-pushed the michal.lach/sem branch 2 times, most recently from c06bb0a to 0fcb1c0 Compare July 28, 2026 15:38
oI0ck added 3 commits July 29, 2026 14:28
Up until C23, C used _Static_assert for compile time assertions.
C23 changed it to static_assert, to match C++ keyword.

Since we can include some of our headers in C++ source, this static
assertions should be portable between C++ and C source.

TASK: RTOS-1407
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.

4 participants