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>
24 #include "eal_private.h"
25 #include "eal_thread.h"
27 RTE_DEFINE_PER_LCORE(unsigned, _lcore_id) = LCORE_ID_ANY;
28 RTE_DEFINE_PER_LCORE(unsigned, _socket_id) = (unsigned)SOCKET_ID_ANY;
29 RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
32 * Send a message to a slave lcore identified by slave_id to call a
33 * function f with argument arg. Once the execution is done, the
34 * remote lcore switch in FINISHED state.
37 rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned slave_id)
41 int m2s = lcore_config[slave_id].pipe_master2slave[1];
42 int s2m = lcore_config[slave_id].pipe_slave2master[0];
44 if (lcore_config[slave_id].state != WAIT)
47 lcore_config[slave_id].f = f;
48 lcore_config[slave_id].arg = arg;
52 while (n == 0 || (n < 0 && errno == EINTR))
53 n = write(m2s, &c, 1);
55 rte_panic("cannot write on configuration pipe\n");
60 } while (n < 0 && errno == EINTR);
63 rte_panic("cannot read on configuration pipe\n");
68 /* set affinity for current thread */
70 eal_thread_set_affinity(void)
72 unsigned lcore_id = rte_lcore_id();
74 /* acquire system unique id */
77 /* update EAL thread core affinity */
78 return rte_thread_set_affinity(&lcore_config[lcore_id].cpuset);
81 void eal_thread_init_master(unsigned lcore_id)
83 /* set the lcore ID in per-lcore memory area */
84 RTE_PER_LCORE(_lcore_id) = lcore_id;
86 /* set CPU affinity */
87 if (eal_thread_set_affinity() < 0)
88 rte_panic("cannot set affinity\n");
91 /* main loop of threads */
92 __attribute__((noreturn)) void *
93 eal_thread_loop(__attribute__((unused)) void *arg)
100 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
102 thread_id = pthread_self();
104 /* retrieve our lcore_id from the configuration structure */
105 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
106 if (thread_id == lcore_config[lcore_id].thread_id)
109 if (lcore_id == RTE_MAX_LCORE)
110 rte_panic("cannot retrieve lcore id\n");
112 m2s = lcore_config[lcore_id].pipe_master2slave[0];
113 s2m = lcore_config[lcore_id].pipe_slave2master[1];
115 /* set the lcore ID in per-lcore memory area */
116 RTE_PER_LCORE(_lcore_id) = lcore_id;
118 /* set CPU affinity */
119 if (eal_thread_set_affinity() < 0)
120 rte_panic("cannot set affinity\n");
122 ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
124 RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
125 lcore_id, thread_id, cpuset, ret == 0 ? "" : "...");
127 /* read on our pipe to get commands */
133 n = read(m2s, &c, 1);
134 } while (n < 0 && errno == EINTR);
137 rte_panic("cannot read on configuration pipe\n");
139 lcore_config[lcore_id].state = RUNNING;
143 while (n == 0 || (n < 0 && errno == EINTR))
144 n = write(s2m, &c, 1);
146 rte_panic("cannot write on configuration pipe\n");
148 if (lcore_config[lcore_id].f == NULL)
149 rte_panic("NULL function pointer\n");
151 /* call the function and store the return value */
152 fct_arg = lcore_config[lcore_id].arg;
153 ret = lcore_config[lcore_id].f(fct_arg);
154 lcore_config[lcore_id].ret = ret;
156 lcore_config[lcore_id].state = FINISHED;
160 /* pthread_exit(NULL); */
164 /* require calling thread tid by gettid() */
165 int rte_sys_gettid(void)
172 int rte_thread_setname(pthread_t id, const char *name)
174 /* this BSD function returns no error */
175 pthread_set_name_np(id, name);