Commit 27f59a4057 for aom

commit 27f59a40579554f65b82edd08880ed6f7d8790bc
Author: Frank Galligan <fgalligan@google.com>
Date:   Thu Nov 26 04:11:10 2026 -0800

    av1: add atomic waiter-gated row/lpf/mt sync

    Add AOM_HAVE_ATOMIC_SYNC (GNU C / Clang lock-free bool/int) fast-path
    loads, weak-CAS monotonic progress updates, and waiter-gated wakeups
    for AV1LfSync (both encoder and decoder loop-filter MT) and
    AV1EncRowMultiThreadSync.

    Specifically:
    - Store waiters counters in the tail of cur_sb_col / num_finished_cols.
    - Keep slow wait/wake helpers out of line with AOM_NOINLINE.
    - Gate enc_row_mt->num_tile_cols_done increments and condvar broadcasts
      on do_pipelined_lpf_mt_with_enc in enc_row_mt_worker_hook().
    - Store row_mt_exit with release semantics on worker error and load it
      lock-free with acquire semantics in encode_sb_row().

    Verified bit-exact under Release, ASan+UBSan+IntSan, and TSan builds
    (*AVxEncoderThreadTest*, *AV1DecodeMultiThreadedTest*, 8T enc/dec),
    plus -m32, REALTIME_ONLY=1, HBD=0/QM=0, and CONFIG_MULTITHREAD=0.

    Speedup (AMD EPYC 7B13, 8T, 10-rep AB/BA, bit-exact):
      - avifenc -j 8: -1.48% median (-0.86% paired, stdev 19.55%)
      - aomenc 720p iq --threads=8: -0.90% median (-0.60% paired, sd 4.1%)
      - aomenc 720p rt --threads=8: -1.05% median (-0.38% paired, sd 2.6%)
      - 1T unaffected (MT sync paths require > 1 worker).
    Binary size (x86_64 -O3): libaom.a .text +0.30 KiB (+0.004%).
    Memory: 4 * rows bytes per sync/plane (< 1 KiB at 1080p, 0 at 1T).

    TAG=agy
    CONV=4b696004-0036-42b2-b3fa-d1c7d63a973c
    Change-Id: I31531a46f83096baeb002419e660242c92b47d50

diff --git a/av1/common/thread_common.c b/av1/common/thread_common.c
index 975c0902f8..c04bfe3e41 100644
--- a/av1/common/thread_common.c
+++ b/av1/common/thread_common.c
@@ -11,6 +11,7 @@

 #include <assert.h>
 #include <limits.h>
+#include <stddef.h>

 #include "aom/aom_image.h"
 #include "config/aom_config.h"
@@ -102,9 +103,19 @@ void av1_loop_filter_alloc(AV1LfSync *lf_sync, AV1_COMMON *cm, int rows,
                   aom_malloc(num_workers * sizeof(*(lf_sync->lfdata))));
   lf_sync->num_workers = num_workers;

+#if AOM_HAVE_ATOMIC_SYNC
+  const size_t sb_col_count = (size_t)rows * 2;
+#else
+  const size_t sb_col_count = (size_t)rows;
+#endif
   for (int j = 0; j < MAX_MB_PLANE; j++) {
-    CHECK_MEM_ERROR(cm, lf_sync->cur_sb_col[j],
-                    aom_malloc(sizeof(*(lf_sync->cur_sb_col[j])) * rows));
+    CHECK_MEM_ERROR(
+        cm, lf_sync->cur_sb_col[j],
+        aom_malloc(sizeof(*(lf_sync->cur_sb_col[j])) * sb_col_count));
+#if AOM_HAVE_ATOMIC_SYNC
+    lf_sync->waiters[j] = lf_sync->cur_sb_col[j] + rows;
+    memset(lf_sync->waiters[j], 0, sizeof(*(lf_sync->waiters[j])) * rows);
+#endif
   }
   CHECK_MEM_ERROR(
       cm, lf_sync->job_queue,
@@ -212,6 +223,34 @@ static inline void cdef_row_mt_sync_write(AV1CdefSync *const cdef_sync,
 // Returns true if the sync read is successful and the caller can continue.
 // Returns false if the caller should exit because a worker has encountered an
 // error.
+#if AOM_HAVE_ATOMIC_SYNC
+// Keep cold mutex/condvar wait path out of sync_read() fast path.
+AOM_NOINLINE static bool sync_read_slow(AV1LfSync *const lf_sync, int r, int c,
+                                        int plane, int nsync) {
+  int *const sb_col = &lf_sync->cur_sb_col[plane][r - 1];
+  pthread_mutex_t *const mutex = &lf_sync->mutex_[plane][r - 1];
+  int *const waiters = &lf_sync->waiters[plane][r - 1];
+  int cur;
+  pthread_mutex_lock(mutex);
+  __atomic_add_fetch(waiters, 1, __ATOMIC_SEQ_CST);
+  while (c > (cur = __atomic_load_n(sb_col, __ATOMIC_SEQ_CST)) - nsync) {
+    pthread_cond_wait(&lf_sync->cond_[plane][r - 1], mutex);
+  }
+  __atomic_sub_fetch(waiters, 1, __ATOMIC_RELEASE);
+  const bool lf_mt_continue = cur != LF_SYNC_EXIT;
+  pthread_mutex_unlock(mutex);
+  return lf_mt_continue;
+}
+
+// Keep cold condvar broadcast out of sync_write() fast path.
+AOM_NOINLINE static void sync_write_wake_slow(AV1LfSync *const lf_sync, int r,
+                                              int plane) {
+  pthread_mutex_lock(&lf_sync->mutex_[plane][r]);
+  pthread_cond_broadcast(&lf_sync->cond_[plane][r]);
+  pthread_mutex_unlock(&lf_sync->mutex_[plane][r]);
+}
+#endif  // AOM_HAVE_ATOMIC_SYNC
+
 static inline bool sync_read(AV1LfSync *const lf_sync, int r, int c,
                              int plane) {
   bool lf_mt_continue = true;
@@ -219,6 +258,15 @@ static inline bool sync_read(AV1LfSync *const lf_sync, int r, int c,
   const int nsync = lf_sync->sync_range;

   if (r && !(c & (nsync - 1))) {
+#if AOM_HAVE_ATOMIC_SYNC
+    const int cur =
+        __atomic_load_n(&lf_sync->cur_sb_col[plane][r - 1], __ATOMIC_ACQUIRE);
+    if (LIKELY(c <= cur - nsync)) {
+      lf_mt_continue = cur != LF_SYNC_EXIT;
+    } else {
+      lf_mt_continue = sync_read_slow(lf_sync, r, c, plane, nsync);
+    }
+#else
     pthread_mutex_t *const mutex = &lf_sync->mutex_[plane][r - 1];
     pthread_mutex_lock(mutex);

@@ -227,6 +275,7 @@ static inline bool sync_read(AV1LfSync *const lf_sync, int r, int c,
     }
     lf_mt_continue = lf_sync->cur_sb_col[plane][r - 1] != LF_SYNC_EXIT;
     pthread_mutex_unlock(mutex);
+#endif
   }
 #else
   (void)lf_sync;
@@ -253,6 +302,23 @@ static inline void sync_write(AV1LfSync *const lf_sync, int r, int c,
   }

   if (sig) {
+#if AOM_HAVE_ATOMIC_SYNC
+    int *const sb_col = &lf_sync->cur_sb_col[plane][r];
+    int prev = __atomic_load_n(sb_col, __ATOMIC_RELAXED);
+    // After an error cur_sb_col[plane][r] holds LF_SYNC_EXIT. CAS keeps it
+    // from being lowered, so workers waiting in sync_read() still see it and
+    // exit.
+    while (prev < cur) {
+      if (__atomic_compare_exchange_n(sb_col, &prev, cur, true,
+                                      __ATOMIC_SEQ_CST, __ATOMIC_RELAXED)) {
+        break;
+      }
+    }
+    if (UNLIKELY(__atomic_load_n(&lf_sync->waiters[plane][r],
+                                 __ATOMIC_SEQ_CST) > 0)) {
+      sync_write_wake_slow(lf_sync, r, plane);
+    }
+#else
     pthread_mutex_lock(&lf_sync->mutex_[plane][r]);

     // After an error cur_sb_col[plane][r] holds LF_SYNC_EXIT. AOMMAX keeps it
@@ -262,6 +328,7 @@ static inline void sync_write(AV1LfSync *const lf_sync, int r, int c,

     pthread_cond_broadcast(&lf_sync->cond_[plane][r]);
     pthread_mutex_unlock(&lf_sync->mutex_[plane][r]);
+#endif
   }
 #else
   (void)lf_sync;
@@ -363,7 +430,12 @@ void av1_set_vert_loop_filter_done(AV1_COMMON *cm, AV1LfSync *lf_sync,
   for (int sb_row = 0; sb_row < sb_rows; ++sb_row) {
     for (int plane = 0; plane < MAX_MB_PLANE; ++plane) {
       pthread_mutex_lock(&lf_sync->mutex_[plane][sb_row]);
+#if AOM_HAVE_ATOMIC_SYNC
+      __atomic_store_n(&lf_sync->cur_sb_col[plane][sb_row], LF_SYNC_EXIT,
+                       __ATOMIC_SEQ_CST);
+#else
       lf_sync->cur_sb_col[plane][sb_row] = LF_SYNC_EXIT;
+#endif
       pthread_cond_broadcast(&lf_sync->cond_[plane][sb_row]);
       pthread_mutex_unlock(&lf_sync->mutex_[plane][sb_row]);
     }
diff --git a/av1/common/thread_common.h b/av1/common/thread_common.h
index 5d467c5d00..1736f54afc 100644
--- a/av1/common/thread_common.h
+++ b/av1/common/thread_common.h
@@ -23,6 +23,14 @@
 extern "C" {
 #endif

+#if CONFIG_MULTITHREAD && defined(__GCC_ATOMIC_BOOL_LOCK_FREE) &&              \
+    __GCC_ATOMIC_BOOL_LOCK_FREE == 2 && defined(__GCC_ATOMIC_INT_LOCK_FREE) && \
+    __GCC_ATOMIC_INT_LOCK_FREE == 2
+#define AOM_HAVE_ATOMIC_SYNC 1
+#else
+#define AOM_HAVE_ATOMIC_SYNC 0
+#endif
+
 struct AV1Common;

 typedef struct AV1LfMTInfo {
@@ -40,6 +48,11 @@ typedef struct AV1LfSyncData {
 #endif
   // Allocate memory to store the loop-filtered superblock index in each row.
   int *cur_sb_col[MAX_MB_PLANE];
+#if AOM_HAVE_ATOMIC_SYNC
+  // Active waiter count per (plane, row) for waiter-gated wakeup. Points into
+  // the tail of cur_sb_col[plane]; do not free separately.
+  int *waiters[MAX_MB_PLANE];
+#endif
   // The optimal sync_range for different resolution and platform should be
   // determined by testing. Currently, it is chosen to be a power-of-2 number.
   int sync_range;
@@ -277,10 +290,13 @@ static inline void loop_filter_frame_mt_init(
   }
   lf_sync->lf_mt_exit = false;

-  // Initialize cur_sb_col to -1 for all SB rows.
+  // Initialize cur_sb_col to -1 and waiters to 0 for all SB rows.
   for (int i = 0; i < MAX_MB_PLANE; i++) {
     memset(lf_sync->cur_sb_col[i], -1,
            sizeof(*(lf_sync->cur_sb_col[i])) * sb_rows);
+#if AOM_HAVE_ATOMIC_SYNC
+    memset(lf_sync->waiters[i], 0, sizeof(*(lf_sync->waiters[i])) * sb_rows);
+#endif
   }

   enqueue_lf_jobs(lf_sync, start_mi_row, end_mi_row, planes_to_lf,
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index f45628fb82..564761c34c 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -1391,11 +1391,16 @@ static inline void encode_sb_row(AV1_COMP *cpi, ThreadData *td,

 #if CONFIG_MULTITHREAD
     if (row_mt_enabled) {
+#if AOM_HAVE_ATOMIC_SYNC
+      const bool row_mt_exit =
+          __atomic_load_n(&enc_row_mt->row_mt_exit, __ATOMIC_ACQUIRE);
+#else
       pthread_mutex_lock(enc_row_mt->mutex_);
       const bool row_mt_exit = enc_row_mt->row_mt_exit;
       pthread_mutex_unlock(enc_row_mt->mutex_);
+#endif
       // Exit in case any worker has encountered an error.
-      if (row_mt_exit) return;
+      if (UNLIKELY(row_mt_exit)) return;
     }
 #endif

diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index b5c7f91e50..073374bf7d 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -1423,6 +1423,14 @@ typedef struct {
    * encoding in the ith superblock row.
    */
   int *num_finished_cols;
+#if AOM_HAVE_ATOMIC_SYNC
+  /*!
+   * Number of worker threads currently waiting on each superblock row's
+   * condvar. Points into the tail of num_finished_cols; do not free
+   * separately.
+   */
+  int *waiters;
+#endif
   /*!
    * Denotes the superblock interval at which conditional signalling should
    * happen. Also denotes the minimum number of extra superblocks of the top row
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index 2500d91560..cc497fffa6 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -11,6 +11,7 @@

 #include <assert.h>
 #include <stdbool.h>
+#include <stddef.h>

 #include "aom_util/aom_pthread.h"

@@ -117,11 +118,47 @@ void av1_row_mt_sync_write_dummy(AV1EncRowMultiThreadSync *row_mt_sync, int r,
   (void)cols;
 }

+#if AOM_HAVE_ATOMIC_SYNC
+// Keep cold mutex/condvar wait path out of av1_row_mt_sync_read() fast path.
+AOM_NOINLINE static void row_mt_sync_read_slow(
+    AV1EncRowMultiThreadSync *row_mt_sync, int r, int min_finished) {
+  int *const finished_col = &row_mt_sync->num_finished_cols[r - 1];
+  pthread_mutex_t *const mutex = &row_mt_sync->mutex_[r - 1];
+  pthread_cond_t *const cond = &row_mt_sync->cond_[r - 1];
+  int *const waiters = &row_mt_sync->waiters[r - 1];
+  pthread_mutex_lock(mutex);
+  __atomic_add_fetch(waiters, 1, __ATOMIC_SEQ_CST);
+  while (__atomic_load_n(finished_col, __ATOMIC_SEQ_CST) < min_finished) {
+    pthread_cond_wait(cond, mutex);
+  }
+  __atomic_sub_fetch(waiters, 1, __ATOMIC_RELEASE);
+  pthread_mutex_unlock(mutex);
+}
+
+// Keep cold condvar signal out of av1_row_mt_sync_write() fast path.
+AOM_NOINLINE static void row_mt_sync_write_wake_slow(
+    AV1EncRowMultiThreadSync *row_mt_sync, int r) {
+  pthread_mutex_t *const mutex = &row_mt_sync->mutex_[r];
+  pthread_mutex_lock(mutex);
+  pthread_cond_signal(&row_mt_sync->cond_[r]);
+  pthread_mutex_unlock(mutex);
+}
+#endif  // AOM_HAVE_ATOMIC_SYNC
+
 void av1_row_mt_sync_read(AV1EncRowMultiThreadSync *row_mt_sync, int r, int c) {
 #if CONFIG_MULTITHREAD
   const int nsync = row_mt_sync->sync_range;

   if (r) {
+#if AOM_HAVE_ATOMIC_SYNC
+    const int min_finished =
+        c + nsync + row_mt_sync->intrabc_extra_top_right_sb_delay;
+    if (LIKELY(__atomic_load_n(&row_mt_sync->num_finished_cols[r - 1],
+                               __ATOMIC_ACQUIRE) >= min_finished)) {
+      return;
+    }
+    row_mt_sync_read_slow(row_mt_sync, r, min_finished);
+#else
     pthread_mutex_t *const mutex = &row_mt_sync->mutex_[r - 1];
     pthread_mutex_lock(mutex);

@@ -130,6 +167,7 @@ void av1_row_mt_sync_read(AV1EncRowMultiThreadSync *row_mt_sync, int r, int c) {
       pthread_cond_wait(&row_mt_sync->cond_[r - 1], mutex);
     }
     pthread_mutex_unlock(mutex);
+#endif
   }
 #else
   (void)row_mt_sync;
@@ -154,6 +192,24 @@ void av1_row_mt_sync_write(AV1EncRowMultiThreadSync *row_mt_sync, int r, int c,
   }

   if (sig) {
+#if AOM_HAVE_ATOMIC_SYNC
+    int *const finished_col = &row_mt_sync->num_finished_cols[r];
+    int prev = __atomic_load_n(finished_col, __ATOMIC_RELAXED);
+    // When a thread encounters an error, num_finished_cols[r] is set to maximum
+    // column number. In this case, the CAS keeps num_finished_cols[r] from
+    // being lowered, preventing workers in av1_row_mt_sync_read() from waiting
+    // indefinitely.
+    while (prev < cur) {
+      if (__atomic_compare_exchange_n(finished_col, &prev, cur, true,
+                                      __ATOMIC_SEQ_CST, __ATOMIC_RELAXED)) {
+        break;
+      }
+    }
+    if (UNLIKELY(__atomic_load_n(&row_mt_sync->waiters[r], __ATOMIC_SEQ_CST) >
+                 0)) {
+      row_mt_sync_write_wake_slow(row_mt_sync, r);
+    }
+#else
     pthread_mutex_lock(&row_mt_sync->mutex_[r]);

     // When a thread encounters an error, num_finished_cols[r] is set to maximum
@@ -166,6 +222,7 @@ void av1_row_mt_sync_write(AV1EncRowMultiThreadSync *row_mt_sync, int r, int c,

     pthread_cond_signal(&row_mt_sync->cond_[r]);
     pthread_mutex_unlock(&row_mt_sync->mutex_[r]);
+#endif
   }
 #else
   (void)row_mt_sync;
@@ -198,8 +255,18 @@ static void row_mt_sync_mem_alloc(AV1EncRowMultiThreadSync *row_mt_sync,
   }
 #endif  // CONFIG_MULTITHREAD

-  CHECK_MEM_ERROR(cm, row_mt_sync->num_finished_cols,
-                  aom_malloc(sizeof(*row_mt_sync->num_finished_cols) * rows));
+#if AOM_HAVE_ATOMIC_SYNC
+  const size_t cols_count = (size_t)rows * 2;
+#else
+  const size_t cols_count = (size_t)rows;
+#endif
+  CHECK_MEM_ERROR(
+      cm, row_mt_sync->num_finished_cols,
+      aom_malloc(sizeof(*row_mt_sync->num_finished_cols) * cols_count));
+#if AOM_HAVE_ATOMIC_SYNC
+  row_mt_sync->waiters = row_mt_sync->num_finished_cols + rows;
+  memset(row_mt_sync->waiters, 0, sizeof(*row_mt_sync->waiters) * rows);
+#endif

   row_mt_sync->rows = rows;
   // Set up nsync.
@@ -616,7 +683,11 @@ static int enc_row_mt_worker_hook(void *arg1, void *unused) {
     error_info->setjmp = 0;
 #if CONFIG_MULTITHREAD
     pthread_mutex_lock(enc_row_mt_mutex_);
+#if AOM_HAVE_ATOMIC_SYNC
+    __atomic_store_n(&enc_row_mt->row_mt_exit, true, __ATOMIC_RELEASE);
+#else
     enc_row_mt->row_mt_exit = true;
+#endif
     // Wake up all the workers waiting in launch_loop_filter_rows() to exit in
     // case of an error.
     pthread_cond_broadcast(enc_row_mt->cond_);
@@ -732,9 +803,13 @@ static int enc_row_mt_worker_hook(void *arg1, void *unused) {
 #endif
     this_tile->abs_sum_level += td->abs_sum_level;
     row_mt_sync->num_threads_working--;
-    enc_row_mt->num_tile_cols_done[sb_row]++;
+    if (do_pipelined_lpf_mt_with_enc) {
+      enc_row_mt->num_tile_cols_done[sb_row]++;
+#if CONFIG_MULTITHREAD
+      pthread_cond_broadcast(enc_row_mt->cond_);
+#endif
+    }
 #if CONFIG_MULTITHREAD
-    pthread_cond_broadcast(enc_row_mt->cond_);
     pthread_mutex_unlock(enc_row_mt_mutex_);
 #endif
   }
@@ -1992,6 +2067,10 @@ void av1_encode_tiles_row_mt(AV1_COMP *cpi) {
       // Initialize num_finished_cols to -1 for all rows.
       memset(row_mt_sync->num_finished_cols, -1,
              sizeof(*row_mt_sync->num_finished_cols) * max_sb_rows_in_tile);
+#if AOM_HAVE_ATOMIC_SYNC
+      memset(row_mt_sync->waiters, 0,
+             sizeof(*row_mt_sync->waiters) * max_sb_rows_in_tile);
+#endif
       row_mt_sync->next_mi_row = this_tile->tile_info.mi_row_start;
       row_mt_sync->num_threads_working = 0;
       row_mt_sync->intrabc_extra_top_right_sb_delay =
@@ -2119,6 +2198,10 @@ void av1_fp_encode_tiles_row_mt(AV1_COMP *cpi) {
       // Initialize num_finished_cols to -1 for all rows.
       memset(row_mt_sync->num_finished_cols, -1,
              sizeof(*row_mt_sync->num_finished_cols) * max_mb_rows);
+#if AOM_HAVE_ATOMIC_SYNC
+      memset(row_mt_sync->waiters, 0,
+             sizeof(*row_mt_sync->waiters) * max_mb_rows);
+#endif
       row_mt_sync->next_mi_row = this_tile->tile_info.mi_row_start;
       row_mt_sync->num_threads_working = 0;