Sebastian Huber created a merge request: 
https://gitlab.rtems.org/rtems/rtos/rtems/-/merge_requests/1608

Project:Branches: sebhub/rtems:up/once-memory-order to rtems/rtos/rtems:main
Author: Sebastian Huber

## Summary

Issue #5840: pthread_once() misses the acquire order on its fast path

**Problem:** The acquire fence of `_Once()` precedes the load of the
once state, so it orders nothing which a caller of the fast path reads
afterwards. The fast path also reads the plain `unsigned char` state
without the mutex while other threads write it.

**Fix:** The first commit adds `Atomic_Uchar` with
`_Atomic_Load_uchar()` and `_Atomic_Store_uchar()` to
`<rtems/score/atomic.h>`, in the C++, the C11 and the fallback
variant. The second commit accesses the state through these
operations with a relaxed order. It issues the acquire fence after the
load which reads `ONCE_STATE_COMPLETE` and returns. The release fence
before the store of `ONCE_STATE_COMPLETE` stays.

**Review hints:**
- `_Once()` casts the `unsigned char *` of `pthread_once_t` to
  `Atomic_Uchar *`. Two static assertions check that the size and the
  alignment are equal. The C++ variant of the header makes the same
  assumption for `Atomic_Uint` already.
- A relaxed load and store of a byte compile to plain instructions on
  all 13 RTEMS 7 toolchains. Only a read-modify-write of a byte calls
  libatomic, and the change uses none.
- The slow path is unchanged apart from the atomic accesses. Its
  order comes from the mutex.

## Test

The two commits were tested on a tree whose `once.c` and `atomic.h`
equal those of `main`. It builds without warnings for `sparc/gr740`
in four configurations: uniprocessor and SMP, each without and with
`RTEMS_DEBUG` and `RTEMS_POSIX_API`. All test suites pass on SIS
without a regression in the four configurations, 1013, 1122, 1042
and 1152 test results. The new operations compile in C++17, C11 and
C99.

## AI Details
<!-- Make sure you have read our statement at 
https://www.rtems.org/generative-ai/ -->

### Prompt used

None as a single prompt. The work was done in an interactive Claude Code 
session on the RTEMS tree. The task was to review the specification of 
`pthread_once()`, to compare its behaviour with POSIX and glibc and to fix the 
defects found. Each change was directed and reviewed.

### AI model used

Claude Opus 5.5 (claude-opus-5-5), through Claude Code.

### How AI was used for the contribution

- ( ) Formatting
- ( ) Test creation.
- (x) Code comments.
- ( ) The entire contribution was generated using AI
- ( ) AI code completion such as Copilot in VSCode.

The analysis, the changes and the commit messages were produced in that session 
under my direction.

### Access

I have not used a product which claims copyright in its output, and I have 
legitimate access to the one I used.

-- 
View it on GitLab: 
https://gitlab.rtems.org/rtems/rtos/rtems/-/merge_requests/1608
You're receiving this email because of your account on gitlab.rtems.org. 
Unsubscribe from this thread: 
https://gitlab.rtems.org/-/namespace/49/sent_notifications/6-atnjc5upybxm1yg4tescxwjg5-1d/unsubscribe
 | Manage all notifications: https://gitlab.rtems.org/-/profile/notifications | 
Help: https://gitlab.rtems.org/help


_______________________________________________
bugs mailing list
[email protected]
http://lists.rtems.org/mailman/listinfo/bugs

Reply via email to