[libc-commits] [libc] [libc] clean up futex usage (PR #91163)

via libc-commits libc-commits at lists.llvm.org
Mon May 6 06:30:24 PDT 2024


================
@@ -39,14 +40,26 @@ class Futex : public cpp::Atomic<FutexWordType> {
     if (timeout && timeout->is_realtime) {
       op |= FUTEX_CLOCK_REALTIME;
     }
-    return syscall_impl<long>(
-        /* syscall number */ FUTEX_SYSCALL_ID,
-        /* futex address */ this,
-        /* futex operation  */ op,
-        /* expected value */ expected,
-        /* timeout */ timeout ? &timeout->abs_time : nullptr,
-        /* ignored */ nullptr,
-        /* bitset */ FUTEX_BITSET_MATCH_ANY);
+    for (;;) {
+      if (this->load(cpp::MemoryOrder::RELAXED) != expected)
+        return 0;
+
+      long ret = syscall_impl<long>(
+          /* syscall number */ FUTEX_SYSCALL_ID,
+          /* futex address */ this,
+          /* futex operation  */ op,
+          /* expected value */ expected,
+          /* timeout */ timeout ? &timeout->abs_time : nullptr,
+          /* ignored */ nullptr,
+          /* bitset */ FUTEX_BITSET_MATCH_ANY);
+
+      // continue waiting if interrupted; otherwise return the result
+      // which should normally be 0 or -ETIMEOUT
+      if (ret == -EINTR)
+        continue;
+      else
----------------
lntue wrote:

nit: `else` is not needed.

https://github.com/llvm/llvm-project/pull/91163


More information about the libc-commits mailing list