xref: /dpdk/lib/eal/linux/eal_thread.c (revision 33b84a2efca7ac188def108ba8b981daa7572b9a)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <errno.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <pthread.h>
9 #include <sys/syscall.h>
10 
11 #include <rte_eal.h>
12 #include <rte_lcore.h>
13 #include <rte_log.h>
14 #include <rte_string_fns.h>
15 
16 /* require calling thread tid by gettid() */
17 int rte_sys_gettid(void)
18 {
19 	return (int)syscall(SYS_gettid);
20 }
21 
22 void rte_thread_set_name(rte_thread_t thread_id, const char *thread_name)
23 {
24 	int ret = ENOSYS;
25 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
26 #if __GLIBC_PREREQ(2, 12)
27 	char truncated[RTE_MAX_THREAD_NAME_LEN];
28 	const size_t truncatedsz = sizeof(truncated);
29 
30 	if (strlcpy(truncated, thread_name, truncatedsz) >= truncatedsz)
31 		RTE_LOG(DEBUG, EAL, "Truncated thread name\n");
32 
33 	ret = pthread_setname_np((pthread_t)thread_id.opaque_id, truncated);
34 #endif
35 #endif
36 	RTE_SET_USED(thread_id);
37 	RTE_SET_USED(thread_name);
38 
39 	if (ret != 0)
40 		RTE_LOG(DEBUG, EAL, "Failed to set thread name\n");
41 }
42 
43 int rte_thread_setname(pthread_t id, const char *name)
44 {
45 	int ret = ENOSYS;
46 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
47 #if __GLIBC_PREREQ(2, 12)
48 	char truncated[16];
49 
50 	strlcpy(truncated, name, sizeof(truncated));
51 	ret = pthread_setname_np(id, truncated);
52 #endif
53 #endif
54 	RTE_SET_USED(id);
55 	RTE_SET_USED(name);
56 	return -ret;
57 }
58