<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Jun 13, 2014 at 12:42 PM, Chandler Carruth <span dir="ltr"><<a href="mailto:chandlerc@google.com" target="_blank">chandlerc@google.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div><div class="h5">
<br><div class="gmail_quote">On Fri, Jun 13, 2014 at 5:29 PM, Zachary Turner <span dir="ltr"><<a href="mailto:zturner@google.com" target="_blank">zturner@google.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">Currently recursive_mutex::~recursive_mutex() asserts if pthread_mutex_destroy fails, and one of the reasons this can fail is if the mutex is locked by another thread. This leads to the assert being triggered in the following sample program:<div>
<br></div><div><div>#include <mutex></div><div>#include <stdlib.h></div><div>#include <stdio.h></div><div><br></div><div>std::recursive_mutex& getMutex() {</div><div> static std::recursive_mutex mutex;</div>
<div> return mutex;</div><div>}</div><div><br></div><div>void* ThreadFunc(void* arg) {</div><div> std::lock_guard<std::recursive_mutex> Lock(getMutex());</div><div> exit(1);</div><div> return nullptr;</div><div>
}</div><div><br></div><div><br></div><div>int main(int argc, char** argv) {</div><div> pthread_t thread;</div><div> pthread_attr_t attr;</div><div><br></div><div> pthread_attr_init(&attr);</div><div> pthread_create(&thread, &attr, ThreadFunc, nullptr);</div>
</div><div> pthread_join(thread, nullptr);</div><div><br></div><div> return 0;</div><div>}</div><div><br></div><div><br></div><div>Is this assertion bogus? I'm trying to replace a mutex in LLVM with recursive_mutex but am blocked by this because the builder on FreeBSD actually aborts here due to the assertion. </div>
<div><br></div><div>What's the best way to go about resolving this? Even if we decide that the assertion should be removed, it's not going to stop the builders from failing every time until they get a new build of libc++, and I don't think (but maybe I'm wrong?) the builders build libc++, but rather just use the one that's already installed.</div>
</div></blockquote></div><br></div></div>The C++ spec says that it is UB to destroy a mutex which is locked. I find this completely ridiculous bordering on insanity, but there it is. libc++ is correct here (unless I can get this restriction removed).</div>
</div></blockquote><div><br></div><div>It is very reasonable for this to be UB. Imagine a mutex where memory is allocated in pthread_mutex_init and free'd in pthread_mutex_destroy. Unlocks which race with the destruction may end up partying on free'd memory.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">
<div class="gmail_extra"><br></div><div class="gmail_extra">It's very easy to support this -- just don't call pthread_mutex_destroy(), the function has no purpose really.</div></div></blockquote><div><br></div><div>
This is not true. Some implementations, like FreeBSD's libthr, free memory in their pthread_mutex_destroy. Not calling pthread_mutex_destroy will lead to leaks.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div></div>