aboutsummaryrefslogtreecommitdiff
path: root/libc/src
diff options
context:
space:
mode:
authorJoseph Huber <huberjn@outlook.com>2024-07-01 06:30:15 -0500
committerGitHub <noreply@github.com>2024-07-01 06:30:15 -0500
commit3c64a98180148340ed72aa2c19054ddfbcfa72e1 (patch)
treebe0765f69c6801679790fb2592faa982261bb289 /libc/src
parentec0e6ef09bdbae42872af1145f9c58c641d0ab8a (diff)
downloadllvm-3c64a98180148340ed72aa2c19054ddfbcfa72e1.zip
llvm-3c64a98180148340ed72aa2c19054ddfbcfa72e1.tar.gz
llvm-3c64a98180148340ed72aa2c19054ddfbcfa72e1.tar.bz2
[libc] Partially implement 'errno' on the GPU (#97107)
Summary: The `errno` variable is expected to be `thread_local` by the standard. However, the GPU targets do not support `thread_local` and implementing that would be a large endeavor. Because of that, we previously didn't provide the `errno` symbol at all. However, to build some programs we at least need to be able to link against `errno`. Many things that would normally set `errno` completely ignore it currently (i.e. stdio) but some programs still need to be able to link against correct C programs. For this purpose this patch exports the `errno` symbol as a simple global. Internally, this will be updated atomically so it's at least not racy. Externally, this will be on the user. I've updated the documentation to state as such. This is required to get `libc++` to build.
Diffstat (limited to 'libc/src')
-rw-r--r--libc/src/errno/libc_errno.cpp13
1 files changed, 9 insertions, 4 deletions
diff --git a/libc/src/errno/libc_errno.cpp b/libc/src/errno/libc_errno.cpp
index 64f9f52..bd1438c 100644
--- a/libc/src/errno/libc_errno.cpp
+++ b/libc/src/errno/libc_errno.cpp
@@ -7,16 +7,21 @@
//===----------------------------------------------------------------------===//
#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
+// LIBC_THREAD_LOCAL on GPU currently does nothing. So essentially this is just
// a global errno for gpu to use for now.
extern "C" {
-LIBC_THREAD_LOCAL int __llvmlibc_gpu_errno;
+LIBC_THREAD_LOCAL LIBC_NAMESPACE::cpp::Atomic<int> __llvmlibc_errno;
}
-void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_gpu_errno = a; }
-LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_gpu_errno; }
+void LIBC_NAMESPACE::Errno::operator=(int a) {
+ __llvmlibc_errno.store(a, cpp::MemoryOrder::RELAXED);
+}
+LIBC_NAMESPACE::Errno::operator int() {
+ return __llvmlibc_errno.load(cpp::MemoryOrder::RELAXED);
+}
#elif !defined(LIBC_COPT_PUBLIC_PACKAGING)
// This mode is for unit testing. We just use our internal errno.