[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:47:51 PDT 2021


DanielMcIntosh-IBM updated this revision to Diff 382414.
DanielMcIntosh-IBM added a comment.

Update comment a bit to increase clarity


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D112567/new/

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 mutexes when the threading API is 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.382414.patch
Type: text/x-patch
Size: 1305 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211026/d0542539/attachment.bin>


More information about the libcxx-commits mailing list