Skip to content

Restore semaphore value on timed wait failure#46

Open
Old-Ding wants to merge 1 commit into
FreeRTOS:mainfrom
Old-Ding:codex/restore-sem-value-on-timeout
Open

Restore semaphore value on timed wait failure#46
Old-Ding wants to merge 1 commit into
FreeRTOS:mainfrom
Old-Ding:codex/restore-sem-value-on-timeout

Conversation

@Old-Ding

@Old-Ding Old-Ding commented Jul 1, 2026

Copy link
Copy Markdown

Summary

  • restore the POSIX semaphore count when sem_timedwait() fails to take the FreeRTOS semaphore
  • keep sem_post() and the existing wait model unchanged

Root cause

sem_timedwait() decrements pxSem->value before attempting to wait. If the wait times out, the semaphore is not acquired, but the decrement was not undone. Repeated timed waits can therefore drive the stored semaphore value increasingly negative.

Closes #26

Testing

  • git diff --check
  • verified the diff is limited to FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_semaphore.c
  • verified the file keeps CRLF line endings

Not run: cmake -S test/posix_build_test -B test/posix_build_test/build and build. This machine does not have cmake, gcc, clang, make, or ninja installed.

@Old-Ding Old-Ding marked this pull request as ready for review July 1, 2026 09:17
@Old-Ding

Old-Ding commented Jul 4, 2026

Copy link
Copy Markdown
Author

Follow-up verification:

  • Rechecked the sem_timedwait() path: pxSem->value is decremented before the wait attempt, and this PR restores it only when xSemaphoreTake() fails. Successful waits keep the existing consume semantics.
  • git diff --check origin/main...HEAD passes on the branch.
  • I found no duplicate open PR for this issue.

I still do not have CMake or a C toolchain available on this Windows host to run test/posix_build_test locally.

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.

sem_timedwait always decrements semaphore value

2 participants