1 | //===-- Implementation of libc_errno --------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/__support/libc_errno.h" |
10 | #include "src/__support/macros/attributes.h" |
11 | #include "src/__support/macros/config.h" |
12 | |
13 | namespace LIBC_NAMESPACE_DECL { |
14 | |
15 | #if LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM_INLINE |
16 | |
17 | #if LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_UNDEFINED |
18 | |
19 | void Errno::operator=(int) {} |
20 | Errno::operator int() { return 0; } |
21 | |
22 | #elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_THREAD_LOCAL |
23 | |
24 | namespace { |
25 | LIBC_THREAD_LOCAL int thread_errno; |
26 | } |
27 | |
28 | extern "C" int *__llvm_libc_errno() noexcept { return &thread_errno; } |
29 | |
30 | void Errno::operator=(int a) { thread_errno = a; } |
31 | Errno::operator int() { return thread_errno; } |
32 | |
33 | #elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_SHARED |
34 | |
35 | namespace { |
36 | int shared_errno; |
37 | } |
38 | |
39 | extern "C" int *__llvm_libc_errno() noexcept { return &shared_errno; } |
40 | |
41 | void Errno::operator=(int a) { shared_errno = a; } |
42 | Errno::operator int() { return shared_errno; } |
43 | |
44 | #elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_EXTERNAL |
45 | |
46 | void Errno::operator=(int a) { *__llvm_libc_errno() = a; } |
47 | Errno::operator int() { return *__llvm_libc_errno(); } |
48 | |
49 | #elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_SYSTEM |
50 | |
51 | void Errno::operator=(int a) { errno = a; } |
52 | Errno::operator int() { return errno; } |
53 | |
54 | #endif |
55 | |
56 | // Define the global `libc_errno` instance. |
57 | Errno libc_errno; |
58 | |
59 | #endif // LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM_INLINE |
60 | |
61 | } // namespace LIBC_NAMESPACE_DECL |
62 | |