xref: /aosp_15_r20/external/llvm-libc/src/pthread/pthread_getname_np.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Linux implementation of the pthread_setname_np function -----------===//
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 "pthread_getname_np.h"
10 
11 #include "src/__support/CPP/span.h"
12 #include "src/__support/CPP/stringstream.h"
13 #include "src/__support/common.h"
14 #include "src/__support/macros/config.h"
15 #include "src/__support/threads/thread.h"
16 
17 #include <pthread.h>
18 #include <stddef.h>
19 
20 namespace LIBC_NAMESPACE_DECL {
21 
22 static_assert(sizeof(pthread_t) == sizeof(LIBC_NAMESPACE::Thread),
23               "Mismatch between pthread_t and internal Thread.");
24 
25 LLVM_LIBC_FUNCTION(int, pthread_getname_np,
26                    (pthread_t th, char *buf, size_t len)) {
27   auto *thread = reinterpret_cast<LIBC_NAMESPACE::Thread *>(&th);
28   cpp::span<char> name_buf(buf, len);
29   cpp::StringStream name_stream(name_buf);
30   return thread->get_name(name_stream);
31 }
32 
33 } // namespace LIBC_NAMESPACE_DECL
34