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 worker lcore identified by worker_id to call a
30 * function f with argument arg. Once the execution is done, the
31 * remote lcore switches to WAIT state.
34 rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned worker_id)
38 int m2w = lcore_config[worker_id].pipe_main2worker[1];
39 int w2m = lcore_config[worker_id].pipe_worker2main[0];
42 /* Check if the worker is in 'WAIT' state. Use acquire order
43 * since 'state' variable is used as the guard variable.
45 if (__atomic_load_n(&lcore_config[worker_id].state,
46 __ATOMIC_ACQUIRE) != WAIT)
49 lcore_config[worker_id].arg = arg;
50 /* Ensure that all the memory operations are completed
51 * before the worker thread starts running the function.
52 * Use worker thread function as the guard variable.
54 __atomic_store_n(&lcore_config[worker_id].f, f, __ATOMIC_RELEASE);
58 while (n == 0 || (n < 0 && errno == EINTR))
59 n = write(m2w, &c, 1);
61 rte_panic("cannot write on configuration pipe\n");
66 } while (n < 0 && errno == EINTR);
69 rte_panic("cannot read on configuration pipe\n");
73 rte_eal_trace_thread_remote_launch(f, arg, worker_id, rc);
77 /* main loop of threads */
79 eal_thread_loop(__rte_unused void *arg)
86 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
88 thread_id = pthread_self();
90 /* retrieve our lcore_id from the configuration structure */
91 RTE_LCORE_FOREACH_WORKER(lcore_id) {
92 if (thread_id == lcore_config[lcore_id].thread_id)
95 if (lcore_id == RTE_MAX_LCORE)
96 rte_panic("cannot retrieve lcore id\n");
98 m2w = lcore_config[lcore_id].pipe_main2worker[0];
99 w2m = lcore_config[lcore_id].pipe_worker2main[1];
101 __rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
103 ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
104 RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
105 lcore_id, thread_id, cpuset, ret == 0 ? "" : "...");
107 rte_eal_trace_thread_lcore_ready(lcore_id, cpuset);
109 /* read on our pipe to get commands */
116 n = read(m2w, &c, 1);
117 } while (n < 0 && errno == EINTR);
120 rte_panic("cannot read on configuration pipe\n");
122 /* Set the state to 'RUNNING'. Use release order
123 * since 'state' variable is used as the guard variable.
125 __atomic_store_n(&lcore_config[lcore_id].state, RUNNING,
130 while (n == 0 || (n < 0 && errno == EINTR))
131 n = write(w2m, &c, 1);
133 rte_panic("cannot write on configuration pipe\n");
135 /* Load 'f' with acquire order to ensure that
136 * the memory operations from the main thread
137 * are accessed only after update to 'f' is visible.
138 * Wait till the update to 'f' is visible to the worker.
140 while ((f = __atomic_load_n(&lcore_config[lcore_id].f,
141 __ATOMIC_ACQUIRE)) == NULL)
144 /* call the function and store the return value */
145 fct_arg = lcore_config[lcore_id].arg;
147 lcore_config[lcore_id].ret = ret;
148 lcore_config[lcore_id].f = NULL;
149 lcore_config[lcore_id].arg = NULL;
151 /* Store the state with release order to ensure that
152 * the memory operations from the worker thread
153 * are completed before the state is updated.
154 * Use 'state' as the guard variable.
156 __atomic_store_n(&lcore_config[lcore_id].state, WAIT,
161 /* pthread_exit(NULL); */
165 /* require calling thread tid by gettid() */
166 int rte_sys_gettid(void)
173 int rte_thread_setname(pthread_t id, const char *name)
175 /* this BSD function returns no error */
176 pthread_set_name_np(id, name);
180 int rte_thread_getname(pthread_t id, char *name, size_t len)