[libcxx-commits] [PATCH] D105758: Hold mutex lock while notify_all is called at notify_all_at_thread_exit

Vinícius dos Santos Oliveira via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jul 10 05:20:00 PDT 2021


vinipsmaker created this revision.
vinipsmaker added a project: libc++.
vinipsmaker requested review of this revision.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Releasing the mutex before the call to notify_all is an optimization.
This optimization cannot be used here. The thread waiting on the
condition might destroy the associated resources -- mutex + condition
variable -- and the notifier thread will access an destroyed variable

- the condition variable. In fact, notify_all_at_thread_exit is meant

exactly to join on detached threads, and the waiting thread doesn't
expect for the notifier thread to access any further shared resources,
making this scenario very likely to happen. The waiting thread might
awake spuriously on the release of the mutex lock. The reorder is
necessary to prevent this race.

Further details can be found at
https://cplusplus.github.io/LWG/issue3343.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105758

Files:
  libcxx/src/thread.cpp


Index: libcxx/src/thread.cpp
===================================================================
--- libcxx/src/thread.cpp
+++ libcxx/src/thread.cpp
@@ -158,8 +158,8 @@
     for (_Notify::iterator i = notify_.begin(), e = notify_.end();
             i != e; ++i)
     {
-        i->second->unlock();
         i->first->notify_all();
+        i->second->unlock();
     }
     for (_AsyncStates::iterator i = async_states_.begin(), e = async_states_.end();
             i != e; ++i)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105758.357705.patch
Type: text/x-patch
Size: 481 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210710/ba3ae5f6/attachment-0001.bin>


More information about the libcxx-commits mailing list