C spills internals all over
C spills internals all over
Posted Mar 18, 2025 17:24 UTC (Tue) by NYKevin (subscriber, #129325)In reply to: C spills internals all over by farnz
Parent article: Warming up to frozen pages for networking
Yes, relaxed is usually used for refcounting, but in fact I was mistaken, you can't use relaxed atomics for *decrementing* the refcount, because that needs to be an acquire-release (whoever decrements the refcount to zero is then going to run the destructor, and we need to ensure that any reads or writes happen-before the object is destructed, so your decrement must synchronize with their decrement, but we don't know in advance which decrement is the acquire and which is the release, so they both have to be acq-rel).
You can only use relaxed atomics for refcount increments, and even then, only if you already have a strong reference. Upgrading a weak reference is an altogether more complicated operation because you have to ensure that the object is not destructed out from under you, or at least you have to detect that case and fail gracefully when it happens, and those both entail at least acquire semantics.