1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <pthread_np.h>
12 #include <sys/queue.h>
15 #include <rte_debug.h>
16 #include <rte_atomic.h>
17 #include <rte_launch.h>
19 #include <rte_memory.h>
20 #include <rte_per_lcore.h>
22 #include <rte_lcore.h>
23 #include <rte_trace.h>
24 #include <rte_trace_point.h>
26 #include "eal_private.h"
27 #include "eal_thread.h"
29 RTE_DEFINE_PER_LCORE(unsigned, _lcore_id) = LCORE_ID_ANY;
30 RTE_DEFINE_PER_LCORE(unsigned, _socket_id) = (unsigned)SOCKET_ID_ANY;
31 RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
34 * Send a message to a slave lcore identified by slave_id to call a
35 * function f with argument arg. Once the execution is done, the
36 * remote lcore switch in FINISHED state.
39 rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned slave_id)
43 int m2s = lcore_config[slave_id].pipe_master2slave[1];
44 int s2m = lcore_config[slave_id].pipe_slave2master[0];
46 if (lcore_config[slave_id].state != WAIT)
49 lcore_config[slave_id].f = f;
50 lcore_config[slave_id].arg = arg;
54 while (n == 0 || (n < 0 && errno == EINTR))
55 n = write(m2s, &c, 1);
57 rte_panic("cannot write on configuration pipe\n");
62 } while (n < 0 && errno == EINTR);
65 rte_panic("cannot read on configuration pipe\n");
70 /* set affinity for current thread */
72 eal_thread_set_affinity(void)
74 unsigned lcore_id = rte_lcore_id();
76 /* acquire system unique id */
79 /* update EAL thread core affinity */
80 return rte_thread_set_affinity(&lcore_config[lcore_id].cpuset);
83 void eal_thread_init_master(unsigned lcore_id)
85 /* set the lcore ID in per-lcore memory area */
86 RTE_PER_LCORE(_lcore_id) = lcore_id;
88 /* set CPU affinity */
89 if (eal_thread_set_affinity() < 0)
90 rte_panic("cannot set affinity\n");
93 /* main loop of threads */
95 eal_thread_loop(__rte_unused void *arg)
102 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
104 thread_id = pthread_self();
106 /* retrieve our lcore_id from the configuration structure */
107 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
108 if (thread_id == lcore_config[lcore_id].thread_id)
111 if (lcore_id == RTE_MAX_LCORE)
112 rte_panic("cannot retrieve lcore id\n");
114 m2s = lcore_config[lcore_id].pipe_master2slave[0];
115 s2m = lcore_config[lcore_id].pipe_slave2master[1];
117 /* set the lcore ID in per-lcore memory area */
118 RTE_PER_LCORE(_lcore_id) = lcore_id;
120 /* set CPU affinity */
121 if (eal_thread_set_affinity() < 0)
122 rte_panic("cannot set affinity\n");
124 ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
126 RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
127 lcore_id, thread_id, cpuset, ret == 0 ? "" : "...");
129 __rte_trace_mem_per_thread_alloc();
131 /* read on our pipe to get commands */
137 n = read(m2s, &c, 1);
138 } while (n < 0 && errno == EINTR);
141 rte_panic("cannot read on configuration pipe\n");
143 lcore_config[lcore_id].state = RUNNING;
147 while (n == 0 || (n < 0 && errno == EINTR))
148 n = write(s2m, &c, 1);
150 rte_panic("cannot write on configuration pipe\n");
152 if (lcore_config[lcore_id].f == NULL)
153 rte_panic("NULL function pointer\n");
155 /* call the function and store the return value */
156 fct_arg = lcore_config[lcore_id].arg;
157 ret = lcore_config[lcore_id].f(fct_arg);
158 lcore_config[lcore_id].ret = ret;
160 lcore_config[lcore_id].state = FINISHED;
164 /* pthread_exit(NULL); */
168 /* require calling thread tid by gettid() */
169 int rte_sys_gettid(void)
176 int rte_thread_setname(pthread_t id, const char *name)
178 /* this BSD function returns no error */
179 pthread_set_name_np(id, name);
183 int rte_thread_getname(pthread_t id, char *name, size_t len)