From 3db4ad9a15b1afaddb909e109c29e21bb91813fb Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sun, 7 Jun 2020 17:59:55 +0300 Subject: [PATCH] Use a relaxed load before test_and_set to not lock cache line on contention (AMD spinlock recommendation per ) --- .../boost/smart_ptr/detail/spinlock_gcc_atomic.hpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/include/boost/smart_ptr/detail/spinlock_gcc_atomic.hpp b/include/boost/smart_ptr/detail/spinlock_gcc_atomic.hpp index c899a59..f2c77f0 100644 --- a/include/boost/smart_ptr/detail/spinlock_gcc_atomic.hpp +++ b/include/boost/smart_ptr/detail/spinlock_gcc_atomic.hpp @@ -7,13 +7,9 @@ # pragma once #endif -// -// Copyright (c) 2008 Peter Dimov -// -// Distributed under the Boost Software License, Version 1.0. -// See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// +// Copyright 2008, 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt #include @@ -40,8 +36,7 @@ public: bool try_lock() { - int r = __atomic_test_and_set( &v_, __ATOMIC_ACQUIRE ); - return r == 0; + return __atomic_load_n( &v_, __ATOMIC_RELAXED ) == 0 && __atomic_test_and_set( &v_, __ATOMIC_ACQUIRE ) == 0; } void lock()