xref: /dpdk/lib/eal/windows/eal_thread.c (revision b53d106d34b5c638f5a2cbdfee0da5bd42d4383f)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4 
5 #include <io.h>
6 
7 #include <rte_atomic.h>
8 #include <rte_debug.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>
15 
16 #include "eal_private.h"
17 #include "eal_windows.h"
18 
19 /*
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.
23  */
24 int
25 rte_eal_remote_launch(lcore_function_t *f, void *arg, unsigned int worker_id)
26 {
27 	int n;
28 	char c = 0;
29 	int m2w = lcore_config[worker_id].pipe_main2worker[1];
30 	int w2m = lcore_config[worker_id].pipe_worker2main[0];
31 
32 	/* Check if the worker is in 'WAIT' state. Use acquire order
33 	 * since 'state' variable is used as the guard variable.
34 	 */
35 	if (__atomic_load_n(&lcore_config[worker_id].state,
36 					__ATOMIC_ACQUIRE) != WAIT)
37 		return -EBUSY;
38 
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.
43 	 */
44 	__atomic_store_n(&lcore_config[worker_id].f, f, __ATOMIC_RELEASE);
45 
46 	/* send message */
47 	n = 0;
48 	while (n == 0 || (n < 0 && errno == EINTR))
49 		n = _write(m2w, &c, 1);
50 	if (n < 0)
51 		rte_panic("cannot write on configuration pipe\n");
52 
53 	/* wait ack */
54 	do {
55 		n = _read(w2m, &c, 1);
56 	} while (n < 0 && errno == EINTR);
57 
58 	if (n <= 0)
59 		rte_panic("cannot read on configuration pipe\n");
60 
61 	return 0;
62 }
63 
64 /* main loop of threads */
65 void *
66 eal_thread_loop(void *arg __rte_unused)
67 {
68 	char c;
69 	int n, ret;
70 	unsigned int lcore_id;
71 	pthread_t thread_id;
72 	int m2w, w2m;
73 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
74 
75 	thread_id = pthread_self();
76 
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)
80 			break;
81 	}
82 	if (lcore_id == RTE_MAX_LCORE)
83 		rte_panic("cannot retrieve lcore id\n");
84 
85 	m2w = lcore_config[lcore_id].pipe_main2worker[0];
86 	w2m = lcore_config[lcore_id].pipe_worker2main[1];
87 
88 	__rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
89 
90 	RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s])\n",
91 		lcore_id, (uintptr_t)thread_id, cpuset);
92 
93 	/* read on our pipe to get commands */
94 	while (1) {
95 		lcore_function_t *f;
96 		void *fct_arg;
97 
98 		/* wait command */
99 		do {
100 			n = _read(m2w, &c, 1);
101 		} while (n < 0 && errno == EINTR);
102 
103 		if (n <= 0)
104 			rte_panic("cannot read on configuration pipe\n");
105 
106 		/* Set the state to 'RUNNING'. Use release order
107 		 * since 'state' variable is used as the guard variable.
108 		 */
109 		__atomic_store_n(&lcore_config[lcore_id].state, RUNNING,
110 					__ATOMIC_RELEASE);
111 
112 		/* send ack */
113 		n = 0;
114 		while (n == 0 || (n < 0 && errno == EINTR))
115 			n = _write(w2m, &c, 1);
116 		if (n < 0)
117 			rte_panic("cannot write on configuration pipe\n");
118 
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.
123 		 */
124 		while ((f = __atomic_load_n(&lcore_config[lcore_id].f,
125 			__ATOMIC_ACQUIRE)) == NULL)
126 			rte_pause();
127 
128 		/* call the function and store the return value */
129 		fct_arg = lcore_config[lcore_id].arg;
130 		ret = f(fct_arg);
131 		lcore_config[lcore_id].ret = ret;
132 		lcore_config[lcore_id].f = NULL;
133 		lcore_config[lcore_id].arg = NULL;
134 
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.
139 		 */
140 		__atomic_store_n(&lcore_config[lcore_id].state, WAIT,
141 					__ATOMIC_RELEASE);
142 	}
143 }
144 
145 /* function to create threads */
146 int
147 eal_thread_create(pthread_t *thread)
148 {
149 	HANDLE th;
150 
151 	th = CreateThread(NULL, 0,
152 		(LPTHREAD_START_ROUTINE)(ULONG_PTR)eal_thread_loop,
153 						NULL, 0, (LPDWORD)thread);
154 	if (!th)
155 		return -1;
156 
157 	SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
158 	SetThreadPriority(th, THREAD_PRIORITY_NORMAL);
159 
160 	return 0;
161 }
162 
163 /* get current thread ID */
164 int
165 rte_sys_gettid(void)
166 {
167 	return GetCurrentThreadId();
168 }
169 
170 int
171 rte_thread_setname(__rte_unused pthread_t id, __rte_unused const char *name)
172 {
173 	/* TODO */
174 	/* This is a stub, not the expected result */
175 	return 0;
176 }
177