[cfe-commits] [libcxx] r160967 - /libcxx/trunk/src/memory.cpp
Howard Hinnant
hhinnant at apple.com
Mon Jul 30 10:13:21 PDT 2012
Author: hhinnant
Date: Mon Jul 30 12:13:21 2012
New Revision: 160967
URL: http://llvm.org/viewvc/llvm-project?rev=160967&view=rev
Log:
Despite my pathological distrust of spin locks, the number just don't lie. I've put a small spin in __sp_mut::lock() on std::mutex::try_lock(), which is testing quite well. In my experience, putting in a yield for every failed iteration is also a major performance booster. This change makes one of the performance tests I was using (a highly contended one) run about 20 times faster.
Modified:
libcxx/trunk/src/memory.cpp
Modified: libcxx/trunk/src/memory.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/memory.cpp?rev=160967&r1=160966&r2=160967&view=diff
==============================================================================
--- libcxx/trunk/src/memory.cpp (original)
+++ libcxx/trunk/src/memory.cpp Mon Jul 30 12:13:21 2012
@@ -10,6 +10,7 @@
#define _LIBCPP_BUILDING_MEMORY
#include "memory"
#include "mutex"
+#include "thread"
_LIBCPP_BEGIN_NAMESPACE_STD
@@ -129,13 +130,23 @@
void
__sp_mut::lock() _NOEXCEPT
{
- reinterpret_cast<mutex*>(_)->lock();
+ mutex& m = *static_cast<mutex*>(_);
+ unsigned count = 0;
+ while (!m.try_lock())
+ {
+ if (++count > 16)
+ {
+ m.lock();
+ break;
+ }
+ this_thread::yield();
+ }
}
void
__sp_mut::unlock() _NOEXCEPT
{
- reinterpret_cast<mutex*>(_)->unlock();
+ static_cast<mutex*>(_)->unlock();
}
__sp_mut&
More information about the cfe-commits
mailing list