|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 | 8 |
|
9 | 9 | #include "libc_errno.h"
|
10 |
| -#include "src/__support/CPP/atomic.h" |
| 10 | +#include "src/errno/errno.h" |
11 | 11 | #include "src/__support/macros/config.h"
|
12 | 12 |
|
13 |
| -#ifdef LIBC_TARGET_ARCH_IS_GPU |
14 |
| -// LIBC_THREAD_LOCAL on GPU currently does nothing. So essentially this is just |
15 |
| -// a global errno for gpu to use for now. |
16 |
| -extern "C" { |
17 |
| -LIBC_THREAD_LOCAL LIBC_NAMESPACE::cpp::Atomic<int> __llvmlibc_errno; |
18 |
| -} |
| 13 | +// libc never stores a value; `errno` macro uses get link-time failure. |
| 14 | +#define LIBC_ERRNO_MODE_UNDEFINED 1 |
| 15 | +// libc maintains per-thread state (requires C++ `thread_local` support). |
| 16 | +#define LIBC_ERRNO_MODE_THREAD_LOCAL 2 |
| 17 | +// libc maintains shared state used by all threads, contrary to standard C |
| 18 | +// semantics unless always single-threaded; nothing prevents data races. |
| 19 | +#define LIBC_ERRNO_MODE_SHARED 3 |
| 20 | +// libc doesn't maintain any internal state, instead the embedder must define |
| 21 | +// `int *__llvm_libc_errno(void);` C function. |
| 22 | +#define LIBC_ERRNO_MODE_EXTERNAL 4 |
| 23 | +// libc uses system `<errno.h>` `errno` macro directly in the overlay mode; in |
| 24 | +// fullbuild mode, effectively the same as `LIBC_ERRNO_MODE_EXTERNAL`. |
| 25 | +#define LIBC_ERRNO_MODE_SYSTEM 5 |
| 26 | + |
| 27 | +#ifndef LIBC_ERRNO_MODE |
| 28 | +#if defined(LIBC_FULL_BUILD) || !defined(LIBC_COPT_PUBLIC_PACKAGING) |
| 29 | +#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_THREAD_LOCAL |
| 30 | +#else |
| 31 | +#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_SYSTEM |
| 32 | +#endif |
| 33 | +#endif // LIBC_ERRNO_MODE |
| 34 | + |
| 35 | +#if LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_UNDEFINED && \ |
| 36 | + LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_THREAD_LOCAL && \ |
| 37 | + LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SHARED && \ |
| 38 | + LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_EXTERNAL && \ |
| 39 | + LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM |
| 40 | +#error LIBC_ERRNO_MODE must be one of the following values: \ |
| 41 | +LIBC_ERRNO_MODE_UNDEFINED, \ |
| 42 | +LIBC_ERRNO_MODE_THREAD_LOCAL, \ |
| 43 | +LIBC_ERRNO_MODE_SHARED, \ |
| 44 | +LIBC_ERRNO_MODE_EXTERNAL, \ |
| 45 | +LIBC_ERRNO_MODE_SYSTEM |
| 46 | +#endif |
| 47 | + |
| 48 | +namespace LIBC_NAMESPACE_DECL { |
| 49 | + |
| 50 | +// Define the global `libc_errno` instance. |
| 51 | +Errno libc_errno; |
| 52 | + |
| 53 | +#if LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_UNDEFINED |
| 54 | + |
| 55 | +void Errno::operator=(int) {} |
| 56 | +Errno::operator int() { return 0; } |
19 | 57 |
|
20 |
| -void LIBC_NAMESPACE::Errno::operator=(int a) { |
21 |
| - __llvmlibc_errno.store(a, cpp::MemoryOrder::RELAXED); |
| 58 | +#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_THREAD_LOCAL |
| 59 | + |
| 60 | +namespace { |
| 61 | +LIBC_THREAD_LOCAL int thread_errno; |
22 | 62 | }
|
23 |
| -LIBC_NAMESPACE::Errno::operator int() { |
24 |
| - return __llvmlibc_errno.load(cpp::MemoryOrder::RELAXED); |
| 63 | + |
| 64 | +extern "C" { |
| 65 | +int *__llvm_libc_errno() { return &thread_errno; } |
25 | 66 | }
|
26 | 67 |
|
27 |
| -#elif !defined(LIBC_COPT_PUBLIC_PACKAGING) |
28 |
| -// This mode is for unit testing. We just use our internal errno. |
29 |
| -LIBC_THREAD_LOCAL int __llvmlibc_internal_errno; |
| 68 | +void Errno::operator=(int a) { thread_errno = a; } |
| 69 | +Errno::operator int() { return thread_errno; } |
30 | 70 |
|
31 |
| -void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_internal_errno = a; } |
32 |
| -LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_internal_errno; } |
| 71 | +#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_SHARED |
| 72 | + |
| 73 | +namespace { |
| 74 | +int shared_errno; |
| 75 | +} |
33 | 76 |
|
34 |
| -#elif defined(LIBC_FULL_BUILD) |
35 |
| -// This mode is for public libc archive, hermetic, and integration tests. |
36 |
| -// In full build mode, we provide the errno storage ourselves. |
37 | 77 | extern "C" {
|
38 |
| -LIBC_THREAD_LOCAL int __llvmlibc_errno; |
| 78 | +int *__llvm_libc_errno() { return &shared_errno; } |
39 | 79 | }
|
40 | 80 |
|
41 |
| -void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_errno = a; } |
42 |
| -LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_errno; } |
| 81 | +void Errno::operator=(int a) { shared_errno = a; } |
| 82 | +Errno::operator int() { return shared_errno; } |
43 | 83 |
|
44 |
| -#else |
45 |
| -void LIBC_NAMESPACE::Errno::operator=(int a) { errno = a; } |
46 |
| -LIBC_NAMESPACE::Errno::operator int() { return errno; } |
| 84 | +#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_EXTERNAL |
47 | 85 |
|
48 |
| -#endif // LIBC_FULL_BUILD |
| 86 | +void Errno::operator=(int a) { *__llvm_libc_errno() = a; } |
| 87 | +Errno::operator int() { return *__llvm_libc_errno(); } |
| 88 | + |
| 89 | +#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_SYSTEM |
| 90 | + |
| 91 | +void Errno::operator=(int a) { errno = a; } |
| 92 | +Errno::operator int() { return errno; } |
| 93 | + |
| 94 | +#endif |
49 | 95 |
|
50 |
| -namespace LIBC_NAMESPACE_DECL { |
51 |
| -// Define the global `libc_errno` instance. |
52 |
| -Errno libc_errno; |
53 | 96 | } // namespace LIBC_NAMESPACE_DECL
|
0 commit comments