[libc-commits] [libc] [libc] Support configurable errno modes (PR #98287)

Roland McGrath via libc-commits libc-commits at lists.llvm.org
Thu Jul 11 00:28:24 PDT 2024


================
@@ -9,44 +9,85 @@
 #include "libc_errno.h"
 #include "src/__support/CPP/atomic.h"
 
-#ifdef LIBC_TARGET_ARCH_IS_GPU
-// LIBC_THREAD_LOCAL on GPU currently does nothing. So essentially this is just
-// a global errno for gpu to use for now.
+#define LIBC_ERRNO_MODE_UNDEFINED 1
+#define LIBC_ERRNO_MODE_THREAD_LOCAL 2
+#define LIBC_ERRNO_MODE_SHARED 3
+#define LIBC_ERRNO_MODE_EXTERNAL 4
+#define LIBC_ERRNO_MODE_SYSTEM 5
+
+#ifndef LIBC_ERRNO_MODE
+#if defined(LIBC_FULL_BUILD) || !defined(LIBC_COPT_PUBLIC_PACKAGING)
+#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_THREAD_LOCAL
+#else
+#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_SYSTEM
+#endif
+#endif // LIBC_ERRNO_MODE
+
+#if LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_UNDEFINED &&                            \
+    LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_THREAD_LOCAL &&                         \
+    LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SHARED &&                               \
+    LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_EXTERNAL &&                             \
+    LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM
+#error LIBC_ERRNO_MODE must be one of the following values: \
+LIBC_ERRNO_MODE_UNDEFINED, \
+LIBC_ERRNO_MODE_THREAD_LOCAL, \
+LIBC_ERRNO_MODE_SHARED, \
+LIBC_ERRNO_MODE_EXTERNAL, \
+LIBC_ERRNO_MODE_SYSTEM
+#endif
+
+namespace LIBC_NAMESPACE {
+
+// Define the global `libc_errno` instance.
+Errno libc_errno;
+
+#if LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_UNDEFINED
+
+void Errno::operator=(int) {}
+Errno::operator int() { return 0; }
+
+#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_THREAD_LOCAL
+
+namespace {
+LIBC_THREAD_LOCAL int thread_errno;
+}
+
 extern "C" {
-LIBC_THREAD_LOCAL LIBC_NAMESPACE::cpp::Atomic<int> __llvmlibc_errno;
+int *__llvm_libc_errno() { return &thread_errno; }
 }
 
-void LIBC_NAMESPACE::Errno::operator=(int a) {
-  __llvmlibc_errno.store(a, cpp::MemoryOrder::RELAXED);
+void Errno::operator=(int a) { __libc_errno = a; }
+Errno::operator int() { return __libc_errno; }
+
+#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_GLOBAL
+
+namespace {
+cpp::Atomic<int> global_errno;
 }
-LIBC_NAMESPACE::Errno::operator int() {
-  return __llvmlibc_errno.load(cpp::MemoryOrder::RELAXED);
+
+extern "C" {
+int *__llvm_libc_errno() { return &global_errno; }
----------------
frobtech wrote:

Hmm, this is kind of a problem.  You can't really make the user access via the function call (i.e. the `errno` macro--the only API) be atomic.  The `errno` macro has to be a "modifiable lvalue" (equivalent to C++ lvalue reference) of type `int`.  If `cpp::Atomic<T>` lets you convert its address to `T*` implicitly somehow that's highly questionable.

So in reality I don't think `SHARED_ATOMIC` is actually an option here, unlike the general case a la `rand`.  There is no way in the C API/ABI to force the user to use an accessor other than normal pointer dereference.  (A C++-only definition of `errno` could be like `libc_errno` and turn loads and stores into library calls, but there's no way to do that in C.)


https://github.com/llvm/llvm-project/pull/98287


More information about the libc-commits mailing list