[Bug middle-end/102566] [i386] GCC should emit LOCK BTS for simple bit-test-and-set operations with std::atomic

crazylht at gmail dot com gcc-bugzilla@gcc.gnu.org
Fri Oct 22 03:31:05 GMT 2021


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102566

--- Comment #28 from Hongtao.liu <crazylht at gmail dot com> ---
Can be optimize

int gomp_futex_wake = FUTEX_WAKE | FUTEX_PRIVATE_FLAG;
int gomp_futex_wait = FUTEX_WAIT | FUTEX_PRIVATE_FLAG;

void
gomp_mutex_lock_slow (gomp_mutex_t *mutex, int oldval)
{
  /* First loop spins a while.  */
  while (oldval == 1)
    {
      if (do_spin (mutex, 1))
        {
          /* Spin timeout, nothing changed.  Set waiting flag.  */
          oldval = __atomic_exchange_n (mutex, -1, MEMMODEL_ACQUIRE);
          if (oldval == 0)
            return;
          futex_wait (mutex, -1);
          break;
        }
      else
        {
          /* Something changed.  If now unlocked, we're good to go.  */
          oldval = 0;
          if (__atomic_compare_exchange_n (mutex, &oldval, 1, false,
                                           MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
            return;
        }
    }

  /* Second loop waits until mutex is unlocked.  We always exit this
     loop with wait flag set, so next unlock will awaken a thread.  */
  while ((oldval = __atomic_exchange_n (mutex, -1, MEMMODEL_ACQUIRE)))
    do_wait (mutex, -1);
}

with _atomic_fetch_or/and/xor ?


More information about the Gcc-bugs mailing list