cederom commented on PR #21719: URL: https://github.com/apache/nuttx/pull/21719#issuecomment-7206595675
Thanks, I agree with doing this in NuttX rather than in the Rust `libc` crate. The size of `pthread_mutex_t` depends on Kconfig (`PTHREAD_MUTEX_TYPES`, `PTHREAD_MUTEX_UNSAFE`, robust flags, `PRIORITY_INHERITANCE`/`SEMHOLDER_INITIALIZER`), and so do the offsets and the sem flags. Moving the mutex to the front of `pthread_mutex_s` would not remove those dependencies, so Rust still could not hardcode one constant. It would help to say in the PR description that the other pthread objects are already fine. `PTHREAD_COND_INITIALIZER` is effectively all zeros (`SEM_INITIALIZER(0)`, `CLOCK_REALTIME == 0`). `pthread_rwlock_t` and `pthread_once_t` only contain a mutex, and they reach it through `pthread_mutex_lock()`, so this change covers them. Some points: 1. **Unlocking a zero-filled mutex is not covered.** With `CONFIG_PTHREAD_MUTEX_UNSAFE`, or `CONFIG_PTHREAD_MUTEX_DEFAULT_UNSAFE` with a NORMAL mutex, `pthread_mutex_unlock()` sees holder 0, so `mutex_is_locked()` is true. There is no errcheck, so it calls `pthread_mutex_give()` on a sem with `flags == 0`. As far as I can tell, that treats it as a counting semaphore and turns the shared `semcount`/`mholder` word into 1. From then on `pthread_mutex_checkinit()` never fires, and the object is neither a valid mutex nor a zero-filled one. POSIX leaves unlocking an unlocked NORMAL mutex undefined, but one more `pthread_mutex_checkinit()` call in unlock would keep the object usable. With robust/errcheck the code already returns EPERM, which is fine. 2. **`memcpy()` over the holder word.** `pthread_mutex_lazyinit()` copies the whole object, including `val.mholder` (set to 0 in the local copy). Meanwhile, other threads may read the same word with `atomic_read_acquire()` without the global lock. The value written is the same, so this works in practice, but it is a formal data race and TSAN-type tools will report it. One option: copy everything except the holder word (two `memcpy()`s around `val`), then do the `atomic_set_release()` as you do now. 3. **Comment wording.** "Treat it as statically initialized, like other libcs do": in glibc and musl, all-zero simply *is* `PTHREAD_MUTEX_INITIALIZER`. They don't detect it and initialize lazily. Suggest something like "...so code that zero-fills a mutex, which works on glibc/musl, works here too." 4. **Description accuracy.** "One extra byte load per lock": the check is a 32-bit acquire load of `mholder`. On SMP ARM/RISC-V that is a load plus a barrier on every lock/trylock. It is still cheap, but please describe it accurately (and give a number if you have one). 5. **trylock can now block.** On first use, `pthread_mutex_trylock()` may sleep on `g_lazyinit_lock`. That only happens once per mutex and is held very briefly, so I think it's acceptable, but a one-line comment would help. 6. **Testing.** The lazy-init race only really happens on SMP, so please run `sim:smp` ostest before this goes in. It would also be good to cover a config with `CONFIG_PTHREAD_MUTEX_TYPES`, one with `CONFIG_PTHREAD_MUTEX_UNSAFE`, `CONFIG_PRIORITY_INHERITANCE`, and a protected or kernel build (`g_lazyinit_lock` and `g_mutex_initializer` live in the userspace libc copy there). 32-bit could be `sim` with `CONFIG_SIM_M32` or real hardware. Please include before/after logs as already requested. 7. Nit: the commit message could reference #20407 like the description does. Point 1 is the only behavioural change I'd ask for. The others are wording, robustness and testing. -- 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]
