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_eal_trace.h>
25 #include "eal_private.h"
26 #include "eal_thread.h"
29 * Send a message to a slave lcore identified by slave_id to call a
30 * function f with argument arg. Once the execution is done, the
31 * remote lcore switch in FINISHED state.
34 rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned slave_id)
38 int m2s = lcore_config[slave_id].pipe_master2slave[1];
39 int s2m = lcore_config[slave_id].pipe_slave2master[0];
42 if (lcore_config[slave_id].state != WAIT)
45 lcore_config[slave_id].f = f;
46 lcore_config[slave_id].arg = arg;
50 while (n == 0 || (n < 0 && errno == EINTR))
51 n = write(m2s, &c, 1);
53 rte_panic("cannot write on configuration pipe\n");
58 } while (n < 0 && errno == EINTR);
61 rte_panic("cannot read on configuration pipe\n");
65 rte_eal_trace_thread_remote_launch(f, arg, slave_id, rc);
69 /* set affinity for current thread */
71 eal_thread_set_affinity(void)
73 unsigned lcore_id = rte_lcore_id();
75 /* acquire system unique id */
78 /* update EAL thread core affinity */
79 return rte_thread_set_affinity(&lcore_config[lcore_id].cpuset);
82 void eal_thread_init_master(unsigned lcore_id)
84 /* set the lcore ID in per-lcore memory area */
85 RTE_PER_LCORE(_lcore_id) = lcore_id;
87 /* set CPU affinity */
88 if (eal_thread_set_affinity() < 0)
89 rte_panic("cannot set affinity\n");
92 /* main loop of threads */
94 eal_thread_loop(__rte_unused void *arg)
101 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
103 thread_id = pthread_self();
105 /* retrieve our lcore_id from the configuration structure */
106 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
107 if (thread_id == lcore_config[lcore_id].thread_id)
110 if (lcore_id == RTE_MAX_LCORE)
111 rte_panic("cannot retrieve lcore id\n");
113 m2s = lcore_config[lcore_id].pipe_master2slave[0];
114 s2m = lcore_config[lcore_id].pipe_slave2master[1];
116 /* set the lcore ID in per-lcore memory area */
117 RTE_PER_LCORE(_lcore_id) = lcore_id;
119 /* set CPU affinity */
120 if (eal_thread_set_affinity() < 0)
121 rte_panic("cannot set affinity\n");
123 ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
125 RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
126 lcore_id, thread_id, cpuset, ret == 0 ? "" : "...");
128 __rte_trace_mem_per_thread_alloc();
129 rte_eal_trace_thread_lcore_ready(lcore_id, cpuset);
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)