1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
7 #include <rte_atomic.h>
9 #include <rte_launch.h>
10 #include <rte_lcore.h>
11 #include <rte_per_lcore.h>
12 #include <rte_common.h>
13 #include <rte_memory.h>
14 #include <eal_thread.h>
16 #include "eal_private.h"
17 #include "eal_windows.h"
20 * Send a message to a worker lcore identified by worker_id to call a
21 * function f with argument arg. Once the execution is done, the
22 * remote lcore switches to WAIT state.
25 rte_eal_remote_launch(lcore_function_t *f, void *arg, unsigned int worker_id)
29 int m2w = lcore_config[worker_id].pipe_main2worker[1];
30 int w2m = lcore_config[worker_id].pipe_worker2main[0];
32 /* Check if the worker is in 'WAIT' state. Use acquire order
33 * since 'state' variable is used as the guard variable.
35 if (__atomic_load_n(&lcore_config[worker_id].state,
36 __ATOMIC_ACQUIRE) != WAIT)
39 lcore_config[worker_id].arg = arg;
40 /* Ensure that all the memory operations are completed
41 * before the worker thread starts running the function.
42 * Use worker thread function as the guard variable.
44 __atomic_store_n(&lcore_config[worker_id].f, f, __ATOMIC_RELEASE);
48 while (n == 0 || (n < 0 && errno == EINTR))
49 n = _write(m2w, &c, 1);
51 rte_panic("cannot write on configuration pipe\n");
55 n = _read(w2m, &c, 1);
56 } while (n < 0 && errno == EINTR);
59 rte_panic("cannot read on configuration pipe\n");
64 /* main loop of threads */
66 eal_thread_loop(void *arg __rte_unused)
70 unsigned int lcore_id;
73 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
75 thread_id = pthread_self();
77 /* retrieve our lcore_id from the configuration structure */
78 RTE_LCORE_FOREACH_WORKER(lcore_id) {
79 if (thread_id == lcore_config[lcore_id].thread_id)
82 if (lcore_id == RTE_MAX_LCORE)
83 rte_panic("cannot retrieve lcore id\n");
85 m2w = lcore_config[lcore_id].pipe_main2worker[0];
86 w2m = lcore_config[lcore_id].pipe_worker2main[1];
88 __rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
90 RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s])\n",
91 lcore_id, (uintptr_t)thread_id, cpuset);
93 /* read on our pipe to get commands */
100 n = _read(m2w, &c, 1);
101 } while (n < 0 && errno == EINTR);
104 rte_panic("cannot read on configuration pipe\n");
106 /* Set the state to 'RUNNING'. Use release order
107 * since 'state' variable is used as the guard variable.
109 __atomic_store_n(&lcore_config[lcore_id].state, RUNNING,
114 while (n == 0 || (n < 0 && errno == EINTR))
115 n = _write(w2m, &c, 1);
117 rte_panic("cannot write on configuration pipe\n");
119 /* Load 'f' with acquire order to ensure that
120 * the memory operations from the main thread
121 * are accessed only after update to 'f' is visible.
122 * Wait till the update to 'f' is visible to the worker.
124 while ((f = __atomic_load_n(&lcore_config[lcore_id].f,
125 __ATOMIC_ACQUIRE)) == NULL)
128 /* call the function and store the return value */
129 fct_arg = lcore_config[lcore_id].arg;
131 lcore_config[lcore_id].ret = ret;
132 lcore_config[lcore_id].f = NULL;
133 lcore_config[lcore_id].arg = NULL;
135 /* Store the state with release order to ensure that
136 * the memory operations from the worker thread
137 * are completed before the state is updated.
138 * Use 'state' as the guard variable.
140 __atomic_store_n(&lcore_config[lcore_id].state, WAIT,
145 /* function to create threads */
147 eal_thread_create(pthread_t *thread)
151 th = CreateThread(NULL, 0,
152 (LPTHREAD_START_ROUTINE)(ULONG_PTR)eal_thread_loop,
153 NULL, 0, (LPDWORD)thread);
157 SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
158 SetThreadPriority(th, THREAD_PRIORITY_NORMAL);
163 /* get current thread ID */
167 return GetCurrentThreadId();
171 rte_thread_setname(__rte_unused pthread_t id, __rte_unused const char *name)
174 /* This is a stub, not the expected result */