Bug 71312 - mutexes for shared_ptr atomics should be padded to cacheline size
Summary: mutexes for shared_ptr atomics should be padded to cacheline size
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: 10.0
Assignee: Jonathan Wakely
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-05-27 14:32 UTC by Jonathan Wakely
Modified: 2019-04-29 13:27 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-03-11 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2016-05-27 14:32:12 UTC
src/c++11/shared_ptr.cc has:

    __gnu_cxx::__mutex&
    get_mutex(unsigned char i)
    {
      static __gnu_cxx::__mutex m[mask + 1];
      return m[i];
    }

This should be:

    __gnu_cxx::__mutex&
    get_mutex(unsigned char i)
    {
      struct M {
	alignas(C) __gnu_cxx::__mutex mx;
      };
      static M m[mask + 1];
      return m[i].mx;
    }

where C == std::hardware_destructive_interference_size
Comment 1 Jonathan Wakely 2019-03-11 21:45:21 UTC
A slightly simpler fix:

--- a/libstdc++-v3/src/c++11/shared_ptr.cc
+++ b/libstdc++-v3/src/c++11/shared_ptr.cc
@@ -34,7 +34,9 @@ namespace __gnu_internal _GLIBCXX_VISIBILITY(hidden)
   __gnu_cxx::__mutex&
   get_mutex(unsigned char i)
   {
-    static __gnu_cxx::__mutex m[mask + 1];
+    // increase alignment to ensure each lock is on a separate cache line
+    struct alignas(64) M : __gnu_cxx::__mutex { };
+    static M m[mask + 1];
     return m[i];
   }
 }
Comment 2 Jonathan Wakely 2019-04-29 12:56:01 UTC
Author: redi
Date: Mon Apr 29 12:55:29 2019
New Revision: 270649

URL: https://gcc.gnu.org/viewcvs?rev=270649&root=gcc&view=rev
Log:
PR libstdc++/71312 Increase alignment of pooled mutexes

	PR libstdc++/71312
	* src/c++11/shared_ptr.cc (get_mutex): Align pool mutexes to 64 bytes.

Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/src/c++11/shared_ptr.cc
Comment 3 Jonathan Wakely 2019-04-29 13:27:36 UTC
Fixed on trunk.