1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
15 #include <rte_lcore.h>
16 #include <rte_memory.h>
19 #include "eal_thread.h"
21 RTE_DECLARE_PER_LCORE(unsigned , _socket_id);
23 unsigned rte_socket_id(void)
25 return RTE_PER_LCORE(_socket_id);
29 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role)
31 struct rte_config *cfg = rte_eal_get_configuration();
33 if (lcore_id >= RTE_MAX_LCORE)
36 if (cfg->lcore_role[lcore_id] == role)
42 int eal_cpuset_socket_id(rte_cpuset_t *cpusetp)
45 int socket_id = SOCKET_ID_ANY;
52 if (!CPU_ISSET(cpu, cpusetp))
55 if (socket_id == SOCKET_ID_ANY)
56 socket_id = eal_cpu_socket_id(cpu);
58 sid = eal_cpu_socket_id(cpu);
59 if (socket_id != sid) {
60 socket_id = SOCKET_ID_ANY;
64 } while (++cpu < RTE_MAX_LCORE);
70 rte_thread_set_affinity(rte_cpuset_t *cpusetp)
78 s = pthread_setaffinity_np(tid, sizeof(rte_cpuset_t), cpusetp);
80 RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
84 /* store socket_id in TLS for quick access */
85 RTE_PER_LCORE(_socket_id) =
86 eal_cpuset_socket_id(cpusetp);
88 /* store cpuset in TLS for quick access */
89 memmove(&RTE_PER_LCORE(_cpuset), cpusetp,
90 sizeof(rte_cpuset_t));
92 lcore_id = rte_lcore_id();
93 if (lcore_id != (unsigned)LCORE_ID_ANY) {
94 /* EAL thread will update lcore_config */
95 lcore_config[lcore_id].socket_id = RTE_PER_LCORE(_socket_id);
96 memmove(&lcore_config[lcore_id].cpuset, cpusetp,
97 sizeof(rte_cpuset_t));
104 rte_thread_get_affinity(rte_cpuset_t *cpusetp)
107 memmove(cpusetp, &RTE_PER_LCORE(_cpuset),
108 sizeof(rte_cpuset_t));
112 eal_thread_dump_affinity(char *str, unsigned size)
117 unsigned int out = 0;
119 rte_thread_get_affinity(&cpuset);
121 for (cpu = 0; cpu < RTE_MAX_LCORE; cpu++) {
122 if (!CPU_ISSET(cpu, &cpuset))
125 ret = snprintf(str + out,
126 size - out, "%u,", cpu);
127 if (ret < 0 || (unsigned)ret >= size - out) {
128 /* string will be truncated */
138 /* remove the last separator */
146 struct rte_thread_ctrl_params {
147 void *(*start_routine)(void *);
149 pthread_barrier_t configured;
152 static void *rte_thread_init(void *arg)
154 struct rte_thread_ctrl_params *params = arg;
155 void *(*start_routine)(void *) = params->start_routine;
156 void *routine_arg = params->arg;
158 pthread_barrier_wait(¶ms->configured);
160 return start_routine(routine_arg);
163 __rte_experimental int
164 rte_ctrl_thread_create(pthread_t *thread, const char *name,
165 const pthread_attr_t *attr,
166 void *(*start_routine)(void *), void *arg)
168 struct rte_thread_ctrl_params params = {
169 .start_routine = start_routine,
174 pthread_barrier_init(¶ms.configured, NULL, 2);
176 ret = pthread_create(thread, attr, rte_thread_init, (void *)¶ms);
181 ret = rte_thread_setname(*thread, name);
186 pthread_barrier_wait(¶ms.configured);
191 pthread_cancel(*thread);
192 pthread_join(*thread, NULL);