On 10/8/25 09:14, Paolo Bonzini wrote:
On 10/8/25 18:06, Richard Henderson wrote:
On 10/8/25 08:27, Paolo Bonzini wrote:
@@ -95,7 +96,7 @@ void qobject_destroy(QObject *obj);
  static inline void qobject_unref_impl(QObject *obj)
  {
      assert(!obj || obj->base.refcnt);
-    if (obj && --obj->base.refcnt == 0) {
+    if (obj && qatomic_fetch_dec(&obj->base.refcnt) == 1) {

qatomic_dec_fetch lets you compare against 0, which makes all isa's happier.

Happy to change all fetch_decs to dec_fetches, but wouldn't the compiler be able to change one to the other?

Interesting. I had just written "optimizations tend to get blocked around atomics, so I wouldn't count on it", but then I stopped to actually try it:

int f(int *p) { return __atomic_add_fetch(p, -1, 0) == 0; }
int g(int *p) { return __atomic_fetch_add(p, -1, 0) == 1; }

With current gcc, these two functions compile identically for x86_64, s390x, riscv, and aarch64. I didn't bother testing further.

So, I'm happy with either formulation.


r~

Reply via email to