<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - std::mutex should be trivially destructible"
   href="https://llvm.org/bugs/show_bug.cgi?id=27658">27658</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>std::mutex should be trivially destructible
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>eric@efcs.ca
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Currently std::mutex has a constexpr constructor, but a non-trivial
destruction.

The constexpr constructor is required to ensure the construction of a mutex
with static storage duration happens at compile time, during constant
initialization, and not during dynamic initialization.
This means that static mutex's are always initialized and can be used safely
during dynamic initialization without the "static initialization order fiasco". 

A trivial destructor is important for similar reasons. If a mutex is used
during dynamic initialization it might also be used during program termination.
If a static mutex has a non-trivial destructor it will be invoked during
termination. This can introduce the "static deinitialization order fiasco".

Additionally, function-local statics emit a guard variable around non-trivially
destructible types. This results in horrible codegen and adds a runtime cost to
every call to that function. non-local static's also result in slightly worse
codegen but it's not as big of a problem.

Example codegen can be found here: <a href="https://goo.gl/3CSzbM">https://goo.gl/3CSzbM</a>

For these reasons I believe we should change 'std::mutex' to be trivially
destructible (when possible). This means *NOT* invoking
"pthread_mutex_destroy(...)" in the destructor.

I believe is a safe change on some pthread implementations. The main purpose of
"pthread_mutex_destroy" is to set the lock to an invalid value, allowing
use-after-free to be diagnosed. AFAIK mutex's initialized with
"PTHREAD_MUTEX_INITIALIZER" own no resources and so omitting the call will not
cause leaks.

On other pthread implementations this change will not be possible. 

Note that std::mutex::~mutex is defined in the dylib, so such a change would
have to ensure we continue to define it there.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>