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
13namespace 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
19void Errno::operator=(int) {}
20Errno::operator int() { return 0; }
21
22#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_THREAD_LOCAL
23
24namespace {
25LIBC_THREAD_LOCAL int thread_errno;
26}
27
28extern "C" int *__llvm_libc_errno() noexcept { return &thread_errno; }
29
30void Errno::operator=(int a) { thread_errno = a; }
31Errno::operator int() { return thread_errno; }
32
33#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_SHARED
34
35namespace {
36int shared_errno;
37}
38
39extern "C" int *__llvm_libc_errno() noexcept { return &shared_errno; }
40
41void Errno::operator=(int a) { shared_errno = a; }
42Errno::operator int() { return shared_errno; }
43
44#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_EXTERNAL
45
46void Errno::operator=(int a) { *__llvm_libc_errno() = a; }
47Errno::operator int() { return *__llvm_libc_errno(); }
48
49#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_SYSTEM
50
51void Errno::operator=(int a) { errno = a; }
52Errno::operator int() { return errno; }
53
54#endif
55
56// Define the global `libc_errno` instance.
57Errno libc_errno;
58
59#endif // LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM_INLINE
60
61} // namespace LIBC_NAMESPACE_DECL
62

source code of libc/src/errno/libc_errno.cpp