[libc-commits] [PATCH] D148294: [LIBC] Handle multiple calls to `detach` more gracefully

Noah Goldstein via Phabricator via libc-commits libc-commits at lists.llvm.org
Thu Apr 13 20:05:23 PDT 2023


goldstein.w.n created this revision.
goldstein.w.n added reviewers: michaelrj, sivachandra.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added projects: libc-project, All.
goldstein.w.n requested review of this revision.

Just return an error for incorrect use rather than almost certainly
creating an inf loop.

Its UB either way, but there is no reason to make the bugs more costly
than they need to be.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148294

Files:
  libc/src/__support/threads/linux/thread.cpp
  libc/src/pthread/pthread_detach.cpp


Index: libc/src/pthread/pthread_detach.cpp
===================================================================
--- libc/src/pthread/pthread_detach.cpp
+++ libc/src/pthread/pthread_detach.cpp
@@ -20,8 +20,7 @@
 
 LLVM_LIBC_FUNCTION(int, pthread_detach, (pthread_t th)) {
   auto *thread = reinterpret_cast<Thread *>(&th);
-  thread->detach();
-  return 0;
+  return thread->detach();
 }
 
 } // namespace __llvm_libc
Index: libc/src/__support/threads/linux/thread.cpp
===================================================================
--- libc/src/__support/threads/linux/thread.cpp
+++ libc/src/__support/threads/linux/thread.cpp
@@ -343,18 +343,10 @@
   uint32_t joinable_state = uint32_t(DetachState::JOINABLE);
   if (attrib->detach_state.compare_exchange_strong(
           joinable_state, uint32_t(DetachState::DETACHED))) {
-    return int(DetachType::SIMPLE);
+    return 0;
   }
 
-  // If the thread was already detached, then the detach method should not
-  // be called at all. If the thread is exiting, then we wait for it to exit
-  // and free up resources.
-  // TODO: Should we handle this less destructively? Maybe just return EINVAL?
-  wait();
-
-  cleanup_thread_resources(attrib);
-
-  return int(DetachType::CLEANUP);
+  return EINVAL;
 }
 
 void Thread::wait() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148294.513420.patch
Type: text/x-patch
Size: 1287 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230414/6b04c1af/attachment.bin>


More information about the libc-commits mailing list