[libc] move fork into threads folder
authorMichael Jones <michaelrj@google.com>
Fri, 11 Nov 2022 21:50:01 +0000 (13:50 -0800)
committerMichael Jones <michaelrj@google.com>
Sat, 12 Nov 2022 00:01:39 +0000 (16:01 -0800)
Fork, as a thread function, should go in the threads folder.
Additionally, it depends on the thread mutex, and it was causing build
issues for targets where we don't support threads.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D137867

libc/src/__support/CMakeLists.txt
libc/src/__support/fork_callbacks.cpp [deleted file]
libc/src/__support/fork_callbacks.h [deleted file]
libc/src/__support/threads/CMakeLists.txt
libc/src/__support/threads/fork_callbacks.cpp [new file with mode: 0644]
libc/src/__support/threads/fork_callbacks.h [new file with mode: 0644]
libc/src/pthread/CMakeLists.txt
libc/src/pthread/pthread_atfork.cpp
libc/src/unistd/linux/CMakeLists.txt
libc/src/unistd/linux/fork.cpp

index aebc9b4a2f89fc75325ec03b26ef61604220e2b5..92a13d51a591c6e8fe871cbc8a27e90026d4c310 100644 (file)
@@ -85,15 +85,6 @@ add_header_library(
     libc.src.errno.errno
 )
 
-add_object_library(
-  fork_callbacks
-  SRCS
-    fork_callbacks.cpp
-  HDRS
-    fork_callbacks.h
-  DEPENDS
-    .threads.mutex
-)
 
 add_header_library(
   integer_operations
diff --git a/libc/src/__support/fork_callbacks.cpp b/libc/src/__support/fork_callbacks.cpp
deleted file mode 100644 (file)
index 7ac4c48..0000000
+++ /dev/null
@@ -1,90 +0,0 @@
-//===-- Implementation of at-fork callback helpers  -----------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "fork_callbacks.h"
-
-#include "src/__support/threads/mutex.h"
-
-#include <stddef.h> // For size_t
-
-namespace __llvm_libc {
-
-namespace {
-
-struct ForkCallbackTriple {
-  ForkCallback *prepare = nullptr;
-  ForkCallback *parent = nullptr;
-  ForkCallback *child = nullptr;
-  constexpr ForkCallbackTriple() = default;
-};
-
-class AtForkCallbackManager {
-  static constexpr size_t CALLBACK_SIZE = 32;
-  // TODO: Replace this with block store when integration tests
-  // can use allocators.
-  ForkCallbackTriple list[CALLBACK_SIZE];
-  Mutex mtx;
-  size_t next_index;
-
-public:
-  constexpr AtForkCallbackManager() : mtx(false, false, false), next_index(0) {}
-
-  bool register_triple(const ForkCallbackTriple &triple) {
-    MutexLock lock(&mtx);
-    if (next_index >= CALLBACK_SIZE)
-      return false;
-    list[next_index] = triple;
-    ++next_index;
-    return true;
-  }
-
-  void invoke_prepare() {
-    MutexLock lock(&mtx);
-    for (size_t i = 0; i < next_index; ++i) {
-      auto prepare = list[i].prepare;
-      if (prepare)
-        prepare();
-    }
-  }
-
-  void invoke_parent() {
-    MutexLock lock(&mtx);
-    for (size_t i = 0; i < next_index; ++i) {
-      auto parent = list[i].parent;
-      if (parent)
-        parent();
-    }
-  }
-
-  void invoke_child() {
-    MutexLock lock(&mtx);
-    for (size_t i = 0; i < next_index; ++i) {
-      auto child = list[i].child;
-      if (child)
-        child();
-    }
-  }
-};
-
-AtForkCallbackManager cb_manager;
-
-} // Anonymous namespace
-
-bool register_atfork_callbacks(ForkCallback *prepare_cb,
-                               ForkCallback *parent_cb,
-                               ForkCallback *child_cb) {
-  return cb_manager.register_triple({prepare_cb, parent_cb, child_cb});
-}
-
-void invoke_child_callbacks() { cb_manager.invoke_child(); }
-
-void invoke_prepare_callbacks() { cb_manager.invoke_prepare(); }
-
-void invoke_parent_callbacks() { cb_manager.invoke_parent(); }
-
-} // namespace __llvm_libc
diff --git a/libc/src/__support/fork_callbacks.h b/libc/src/__support/fork_callbacks.h
deleted file mode 100644 (file)
index ab43436..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-//===-- At-fork callback helpers  -------------------------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-namespace __llvm_libc {
-
-using ForkCallback = void(void);
-
-bool register_atfork_callbacks(ForkCallback *prepare_cd,
-                               ForkCallback *parent_cb, ForkCallback *child_cb);
-void invoke_prepare_callbacks();
-void invoke_parent_callbacks();
-void invoke_child_callbacks();
-
-} // namespace __llvm_libc
index 5ef1cd3a5f16e453691b55898be9543fc3bd6ec0..e71f436fe175167f12aece158882739343eb538a 100644 (file)
@@ -16,6 +16,16 @@ if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.mutex)
     DEPENDS
       .${LIBC_TARGET_OS}.mutex
   )
+
+  add_object_library(
+    fork_callbacks
+    SRCS
+      fork_callbacks.cpp
+    HDRS
+      fork_callbacks.h
+    DEPENDS
+      .mutex
+  )
 endif()
 
 add_header_library(
diff --git a/libc/src/__support/threads/fork_callbacks.cpp b/libc/src/__support/threads/fork_callbacks.cpp
new file mode 100644 (file)
index 0000000..7ac4c48
--- /dev/null
@@ -0,0 +1,90 @@
+//===-- Implementation of at-fork callback helpers  -----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "fork_callbacks.h"
+
+#include "src/__support/threads/mutex.h"
+
+#include <stddef.h> // For size_t
+
+namespace __llvm_libc {
+
+namespace {
+
+struct ForkCallbackTriple {
+  ForkCallback *prepare = nullptr;
+  ForkCallback *parent = nullptr;
+  ForkCallback *child = nullptr;
+  constexpr ForkCallbackTriple() = default;
+};
+
+class AtForkCallbackManager {
+  static constexpr size_t CALLBACK_SIZE = 32;
+  // TODO: Replace this with block store when integration tests
+  // can use allocators.
+  ForkCallbackTriple list[CALLBACK_SIZE];
+  Mutex mtx;
+  size_t next_index;
+
+public:
+  constexpr AtForkCallbackManager() : mtx(false, false, false), next_index(0) {}
+
+  bool register_triple(const ForkCallbackTriple &triple) {
+    MutexLock lock(&mtx);
+    if (next_index >= CALLBACK_SIZE)
+      return false;
+    list[next_index] = triple;
+    ++next_index;
+    return true;
+  }
+
+  void invoke_prepare() {
+    MutexLock lock(&mtx);
+    for (size_t i = 0; i < next_index; ++i) {
+      auto prepare = list[i].prepare;
+      if (prepare)
+        prepare();
+    }
+  }
+
+  void invoke_parent() {
+    MutexLock lock(&mtx);
+    for (size_t i = 0; i < next_index; ++i) {
+      auto parent = list[i].parent;
+      if (parent)
+        parent();
+    }
+  }
+
+  void invoke_child() {
+    MutexLock lock(&mtx);
+    for (size_t i = 0; i < next_index; ++i) {
+      auto child = list[i].child;
+      if (child)
+        child();
+    }
+  }
+};
+
+AtForkCallbackManager cb_manager;
+
+} // Anonymous namespace
+
+bool register_atfork_callbacks(ForkCallback *prepare_cb,
+                               ForkCallback *parent_cb,
+                               ForkCallback *child_cb) {
+  return cb_manager.register_triple({prepare_cb, parent_cb, child_cb});
+}
+
+void invoke_child_callbacks() { cb_manager.invoke_child(); }
+
+void invoke_prepare_callbacks() { cb_manager.invoke_prepare(); }
+
+void invoke_parent_callbacks() { cb_manager.invoke_parent(); }
+
+} // namespace __llvm_libc
diff --git a/libc/src/__support/threads/fork_callbacks.h b/libc/src/__support/threads/fork_callbacks.h
new file mode 100644 (file)
index 0000000..566290a
--- /dev/null
@@ -0,0 +1,24 @@
+//===-- At-fork callback helpers  -------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SUPPORT_THREAD_FORK_CALLBACKS_H
+#define LLVM_LIBC_SRC_SUPPORT_THREAD_FORK_CALLBACKS_H
+
+namespace __llvm_libc {
+
+using ForkCallback = void(void);
+
+bool register_atfork_callbacks(ForkCallback *prepare_cd,
+                               ForkCallback *parent_cb, ForkCallback *child_cb);
+void invoke_prepare_callbacks();
+void invoke_parent_callbacks();
+void invoke_child_callbacks();
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_SUPPORT_THREAD_FORK_CALLBACKS_H
index f2d33b9b710193109adcb3367fa97918373f3b6a..ca5f45efc809b9b0b7da3336e9053772e761b014 100644 (file)
@@ -407,5 +407,5 @@ add_entrypoint_object(
   DEPENDS
     libc.include.errno
     libc.include.pthread
-    libc.src.__support.fork_callbacks
+    libc.src.__support.threads.fork_callbacks
 )
index bff80d5fd4563ffe922348797eda87423bc63927..7136bf35816cbff16f280932a9b78c087fe46fd7 100644 (file)
@@ -9,7 +9,7 @@
 #include "pthread_atfork.h"
 
 #include "src/__support/common.h"
-#include "src/__support/fork_callbacks.h"
+#include "src/__support/threads/fork_callbacks.h"
 
 #include <errno.h>
 #include <pthread.h> // For pthread_* type definitions.
index 47382d2490c1c7faae96e19fe9d11cadcc3f3cc0..e5ea13e7765561fea33e90d3c37f612d61e365b8 100644 (file)
@@ -100,7 +100,7 @@ add_entrypoint_object(
     libc.include.errno
     libc.include.unistd
     libc.include.sys_syscall
-    libc.src.__support.fork_callbacks
+    libc.src.__support.threads.fork_callbacks
     libc.src.__support.OSUtil.osutil
     libc.src.__support.threads.thread
     libc.src.errno.errno
index 5c12fafd425aec883a0a62265282f7908fb01a1b..469c72e4ec3eaa5bacc80312228823dd957d435d 100644 (file)
@@ -10,7 +10,7 @@
 
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
-#include "src/__support/fork_callbacks.h"
+#include "src/__support/threads/fork_callbacks.h"
 #include "src/__support/threads/thread.h" // For thread self object
 
 #include <errno.h>