u7k4rs6 opened a new pull request, #21719:
URL: https://github.com/apache/nuttx/pull/21719

   ## Summary
   
   `PTHREAD_MUTEX_INITIALIZER` isn't all zeros on NuttX, but Rust's libc crate 
and other FFI code assume it is (#20407). Locking a zero-filled mutex returns 
`EBUSY` or hangs, and destroying one hits `DEBUGASSERT(pid != 0)`.
   
   Every initialized mutex has `SEM_TYPE_MUTEX` set, so lock, trylock and 
destroy now check for zero sem flags and set the mutex up the same way 
`PTHREAD_MUTEX_INITIALIZER` would, under a small global lock. The flags are 
stored last, so other threads wait on that lock until the mutex is ready. The 
rwlock gets fixed too since it locks through these.
   
   I went with fixing it on the NuttX side because the initializer's layout 
depends on the config, so one constant in Rust can't be right everywhere. Happy 
to do it differently if you'd prefer, see my comment on #20407.
   
   ## Impact
   
   Zero-filled mutexes now work like statically initialized ones, as on glibc 
and musl. Initialized mutexes only pay for a one-byte acquire load per lock. No 
API or config changes.
   
   ## Testing
   
   Host: Linux 7.0, gcc 15.2, `sim:nsh` (CMake).
   
   The test is in apache/nuttx-apps (linked below): it destroys an unused 
zero-filled mutex, trylocks one, and has two threads contend on a fresh one.
   
   Without this PR the test fails:
   
   ```
   Testing zero-filled mutex
   dump_assert_info: Assertion failed pid != 0: at file: 
/libs/libc/pthread/pthread_mutex_destroy.c:81 task: ostest
   ```
   
   With it, the whole ostest passes:
   
   ```
   Testing zero-filled mutex
                Thread1 Thread2
        Zero Loops      32      32
        Zero Errors     0       0
   ...
   ostest_main: Exiting with status 0
   ```
   
   nxstyle is clean on the changed files. I haven't tried SMP or 32-bit targets 
yet.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to