1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <sys/syscall.h>
12 #include <rte_debug.h>
13 #include <rte_launch.h>
16 #include <rte_lcore.h>
17 #include <rte_eal_trace.h>
19 #include "eal_private.h"
20 #include "eal_thread.h"
23 * Send a message to a worker lcore identified by worker_id to call a
24 * function f with argument arg. Once the execution is done, the
25 * remote lcore switches to WAIT state.
28 rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned int worker_id)
32 int m2w = lcore_config[worker_id].pipe_main2worker[1];
33 int w2m = lcore_config[worker_id].pipe_worker2main[0];
36 /* Check if the worker is in 'WAIT' state. Use acquire order
37 * since 'state' variable is used as the guard variable.
39 if (__atomic_load_n(&lcore_config[worker_id].state,
40 __ATOMIC_ACQUIRE) != WAIT)
43 lcore_config[worker_id].arg = arg;
44 /* Ensure that all the memory operations are completed
45 * before the worker thread starts running the function.
46 * Use worker thread function pointer as the guard variable.
48 __atomic_store_n(&lcore_config[worker_id].f, f, __ATOMIC_RELEASE);
52 while (n == 0 || (n < 0 && errno == EINTR))
53 n = write(m2w, &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");
67 rte_eal_trace_thread_remote_launch(f, arg, worker_id, rc);
71 /* main loop of threads */
73 eal_thread_loop(__rte_unused void *arg)
80 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
82 thread_id = pthread_self();
84 /* retrieve our lcore_id from the configuration structure */
85 RTE_LCORE_FOREACH_WORKER(lcore_id) {
86 if (thread_id == lcore_config[lcore_id].thread_id)
89 if (lcore_id == RTE_MAX_LCORE)
90 rte_panic("cannot retrieve lcore id\n");
92 m2w = lcore_config[lcore_id].pipe_main2worker[0];
93 w2m = lcore_config[lcore_id].pipe_worker2main[1];
95 __rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
97 ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
98 RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
99 lcore_id, (uintptr_t)thread_id, cpuset, ret == 0 ? "" : "...");
101 rte_eal_trace_thread_lcore_ready(lcore_id, cpuset);
103 /* read on our pipe to get commands */
110 n = read(m2w, &c, 1);
111 } while (n < 0 && errno == EINTR);
114 rte_panic("cannot read on configuration pipe\n");
116 /* Set the state to 'RUNNING'. Use release order
117 * since 'state' variable is used as the guard variable.
119 __atomic_store_n(&lcore_config[lcore_id].state, RUNNING,
124 while (n == 0 || (n < 0 && errno == EINTR))
125 n = write(w2m, &c, 1);
127 rte_panic("cannot write on configuration pipe\n");
129 /* Load 'f' with acquire order to ensure that
130 * the memory operations from the main thread
131 * are accessed only after update to 'f' is visible.
132 * Wait till the update to 'f' is visible to the worker.
134 while ((f = __atomic_load_n(&lcore_config[lcore_id].f,
135 __ATOMIC_ACQUIRE)) == NULL)
138 /* call the function and store the return value */
139 fct_arg = lcore_config[lcore_id].arg;
141 lcore_config[lcore_id].ret = ret;
142 lcore_config[lcore_id].f = NULL;
143 lcore_config[lcore_id].arg = NULL;
145 /* Store the state with release order to ensure that
146 * the memory operations from the worker thread
147 * are completed before the state is updated.
148 * Use 'state' as the guard variable.
150 __atomic_store_n(&lcore_config[lcore_id].state, WAIT,
155 /* pthread_exit(NULL); */
159 /* require calling thread tid by gettid() */
160 int rte_sys_gettid(void)
162 return (int)syscall(SYS_gettid);
165 int rte_thread_setname(pthread_t id, const char *name)
168 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
169 #if __GLIBC_PREREQ(2, 12)
172 strlcpy(truncated, name, sizeof(truncated));
173 ret = pthread_setname_np(id, truncated);
181 int rte_thread_getname(pthread_t id, char *name, size_t len)
184 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
185 #if __GLIBC_PREREQ(2, 12)
186 ret = pthread_getname_np(id, name, len);