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 "libc_errno.h"
10
11#ifdef LIBC_TARGET_ARCH_IS_GPU
12// LIBC_THREAD_LOCAL on GPU currently does nothing. So essentially this is just
13// a global errno for gpu to use for now.
14extern "C" {
15LIBC_THREAD_LOCAL int __llvmlibc_gpu_errno;
16}
17
18void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_gpu_errno = a; }
19LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_gpu_errno; }
20
21#elif !defined(LIBC_COPT_PUBLIC_PACKAGING)
22// This mode is for unit testing. We just use our internal errno.
23LIBC_THREAD_LOCAL int __llvmlibc_internal_errno;
24
25void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_internal_errno = a; }
26LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_internal_errno; }
27
28#elif defined(LIBC_FULL_BUILD)
29// This mode is for public libc archive, hermetic, and integration tests.
30// In full build mode, we provide the errno storage ourselves.
31extern "C" {
32LIBC_THREAD_LOCAL int __llvmlibc_errno;
33}
34
35void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_errno = a; }
36LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_errno; }
37
38#else
39// In overlay mode, we simply use the system errno.
40#include <errno.h>
41
42void LIBC_NAMESPACE::Errno::operator=(int a) { errno = a; }
43LIBC_NAMESPACE::Errno::operator int() { return errno; }
44
45#endif // LIBC_FULL_BUILD
46
47namespace LIBC_NAMESPACE {
48// Define the global `libc_errno` instance.
49Errno libc_errno;
50} // namespace LIBC_NAMESPACE
51

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