[libc-commits] [libc] [llvm] [libc][CndVar] reimplmement conditional variable with FIFO ordering (PR #192748)
Schrodinger ZHU Yifan via libc-commits
libc-commits at lists.llvm.org
Mon Apr 20 17:55:15 PDT 2026
================
@@ -18,8 +19,8 @@ namespace LIBC_NAMESPACE_DECL {
static_assert(sizeof(CndVar) == sizeof(cnd_t));
LLVM_LIBC_FUNCTION(int, cnd_init, (cnd_t * cond)) {
- CndVar *cndvar = reinterpret_cast<CndVar *>(cond);
- return CndVar::init(cndvar) ? thrd_error : thrd_success;
+ new (cond) CndVar(false);
----------------
SchrodingerZhu wrote:
@michaelrj-google From my understanding, placement new is the correct way to tell the compiler: “from this point, start the lifetime of a `CndVar` object at this memory location (and effectively drop any previous assumptions about the object at that location)”.
As you can see here, we accept a pointer to public interface type `cnd_t` and start using it as our internal `CndVar*`, so placement new feels more appropriate. Consider if we LTO other operations into the same module, this is how the compiler can see that we actually initialized a `CndVar` object at this `cnd_t*` location and then access it via `CndVar*`.
Although `CndVar` and `cnd_t` are trivial objects anyway, I still prefer to make it more “correct” in the object model. That is why I prefer placement new when initializing `CndVar`.
https://github.com/llvm/llvm-project/pull/192748
More information about the libc-commits
mailing list