[libcxx-commits] [PATCH] D113069: [libcxx][SystemZ][z/OS] Update libcxx/src/random_shuffle.cpp to accommodate POSIX(OFF)

Daniel McIntosh via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 2 16:23:58 PDT 2021


DanielMcIntosh-IBM created this revision.
DanielMcIntosh-IBM added reviewers: jroelofs, EricWF, Quuxplusone, ldionne.
DanielMcIntosh-IBM requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Check that the threading API is enabled in the __rs_default constructor/
destructor in order to prevent the 2 argument form of std::random_shuffle from
calling mutex functions when they're disabled.

We might be able to use `__libcpp_has_spawned_other_threads` instead of
`__libcpp_are_threads_enabled` here, but there is a large amount of code that
runs between the mutex lock and unlock. Assuming that we never encounter a
situation where __rs_default::__rs_default() is run twice (once single-threaded
and once multi-threaded) before we run __rs_default::~__rs_default() once is
therefore a risky proposition.

Depends on D110349 <https://reviews.llvm.org/D110349>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D113069

Files:
  libcxx/src/random_shuffle.cpp


Index: libcxx/src/random_shuffle.cpp
===================================================================
--- libcxx/src/random_shuffle.cpp
+++ libcxx/src/random_shuffle.cpp
@@ -25,9 +25,10 @@
 __rs_default::__rs_default()
 {
 #ifndef _LIBCPP_HAS_NO_THREADS
+  if (__libcpp_are_threads_enabled())
     __libcpp_mutex_lock(&__rs_mut);
 #endif
-    __c_ = 1;
+  __c_ = 1;
 }
 
 __rs_default::__rs_default(const __rs_default&)
@@ -38,10 +39,10 @@
 __rs_default::~__rs_default()
 {
 #ifndef _LIBCPP_HAS_NO_THREADS
-    if (--__c_ == 0)
-       __libcpp_mutex_unlock(&__rs_mut);
+  if (--__c_ == 0 && __libcpp_are_threads_enabled())
+    __libcpp_mutex_unlock(&__rs_mut);
 #else
-    --__c_;
+  --__c_;
 #endif
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113069.384270.patch
Type: text/x-patch
Size: 710 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211102/82ff690e/attachment.bin>


More information about the libcxx-commits mailing list