[libcxx-commits] [PATCH] D112567: [libcxxabi][SystemZ][z/OS] Update libcxxabi/src/fallback_malloc.cpp to support POSIX(OFF)

Daniel McIntosh via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Oct 26 11:41:49 PDT 2021


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

Modify `mutexor` so we don't perform syncronization in `fallback_malloc()` or
`fallback_free()` when we're in a single-threaded application in order to
prevent calls to mutex functions when the threading API is disabled.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112567

Files:
  libcxxabi/src/fallback_malloc.cpp


Index: libcxxabi/src/fallback_malloc.cpp
===================================================================
--- libcxxabi/src/fallback_malloc.cpp
+++ libcxxabi/src/fallback_malloc.cpp
@@ -41,14 +41,28 @@
 
 class mutexor {
 public:
-#ifndef _LIBCXXABI_HAS_NO_THREADS
-  mutexor(std::__libcpp_mutex_t* m) : mtx_(m) {
-    std::__libcpp_mutex_lock(mtx_);
-  }
-  ~mutexor() { std::__libcpp_mutex_unlock(mtx_); }
-#else
+#ifdef _LIBCXXABI_HAS_NO_THREADS
   mutexor(void*) {}
   ~mutexor() {}
+#else
+  mutexor(std::__libcpp_mutex_t* m) {
+    // Neither fallback_malloc nor fallback_free spawn new threads. Thus, there
+    // is no danger of one thread entering them without aquiring the mutex,
+    // followed by another thread entering (and aquiring the mutex) before the
+    // first thread leaves.
+    // We can't acquire the mutex with threads disabled, but since we don't
+    // need to unless there is more than one thread anyways, we only bother to
+    // aquire the mutex when multi-threaded.
+    if (std::__libcpp_has_spawned_other_threads()) {
+      mtx_ = m;
+      std::__libcpp_mutex_lock(mtx_);
+    } else
+      mtx_ = nullptr;
+  }
+  ~mutexor() {
+    if (mtx_ != nullptr)
+      std::__libcpp_mutex_unlock(mtx_);
+  }
 #endif
 private:
   mutexor(const mutexor& rhs);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112567.382412.patch
Type: text/x-patch
Size: 1294 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211026/d6481db6/attachment-0001.bin>


More information about the libcxx-commits mailing list