xref: /dpdk/lib/eal/windows/eal_thread.c (revision daa02b5cddbb8e11b31d41e2bf7bb1ae64dcae2f)
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 switch in FINISHED 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 	if (lcore_config[worker_id].state != WAIT)
33 		return -EBUSY;
34 
35 	lcore_config[worker_id].f = f;
36 	lcore_config[worker_id].arg = arg;
37 
38 	/* send message */
39 	n = 0;
40 	while (n == 0 || (n < 0 && errno == EINTR))
41 		n = _write(m2w, &c, 1);
42 	if (n < 0)
43 		rte_panic("cannot write on configuration pipe\n");
44 
45 	/* wait ack */
46 	do {
47 		n = _read(w2m, &c, 1);
48 	} while (n < 0 && errno == EINTR);
49 
50 	if (n <= 0)
51 		rte_panic("cannot read on configuration pipe\n");
52 
53 	return 0;
54 }
55 
56 /* main loop of threads */
57 void *
58 eal_thread_loop(void *arg __rte_unused)
59 {
60 	char c;
61 	int n, ret;
62 	unsigned int lcore_id;
63 	pthread_t thread_id;
64 	int m2w, w2m;
65 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
66 
67 	thread_id = pthread_self();
68 
69 	/* retrieve our lcore_id from the configuration structure */
70 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
71 		if (thread_id == lcore_config[lcore_id].thread_id)
72 			break;
73 	}
74 	if (lcore_id == RTE_MAX_LCORE)
75 		rte_panic("cannot retrieve lcore id\n");
76 
77 	m2w = lcore_config[lcore_id].pipe_main2worker[0];
78 	w2m = lcore_config[lcore_id].pipe_worker2main[1];
79 
80 	__rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
81 
82 	RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s])\n",
83 		lcore_id, (uintptr_t)thread_id, cpuset);
84 
85 	/* read on our pipe to get commands */
86 	while (1) {
87 		void *fct_arg;
88 
89 		/* wait command */
90 		do {
91 			n = _read(m2w, &c, 1);
92 		} while (n < 0 && errno == EINTR);
93 
94 		if (n <= 0)
95 			rte_panic("cannot read on configuration pipe\n");
96 
97 		lcore_config[lcore_id].state = RUNNING;
98 
99 		/* send ack */
100 		n = 0;
101 		while (n == 0 || (n < 0 && errno == EINTR))
102 			n = _write(w2m, &c, 1);
103 		if (n < 0)
104 			rte_panic("cannot write on configuration pipe\n");
105 
106 		if (lcore_config[lcore_id].f == NULL)
107 			rte_panic("NULL function pointer\n");
108 
109 		/* call the function and store the return value */
110 		fct_arg = lcore_config[lcore_id].arg;
111 		ret = lcore_config[lcore_id].f(fct_arg);
112 		lcore_config[lcore_id].ret = ret;
113 		rte_wmb();
114 
115 		/* when a service core returns, it should go directly to WAIT
116 		 * state, because the application will not lcore_wait() for it.
117 		 */
118 		if (lcore_config[lcore_id].core_role == ROLE_SERVICE)
119 			lcore_config[lcore_id].state = WAIT;
120 		else
121 			lcore_config[lcore_id].state = FINISHED;
122 	}
123 }
124 
125 /* function to create threads */
126 int
127 eal_thread_create(pthread_t *thread)
128 {
129 	HANDLE th;
130 
131 	th = CreateThread(NULL, 0,
132 		(LPTHREAD_START_ROUTINE)(ULONG_PTR)eal_thread_loop,
133 						NULL, 0, (LPDWORD)thread);
134 	if (!th)
135 		return -1;
136 
137 	SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
138 	SetThreadPriority(th, THREAD_PRIORITY_NORMAL);
139 
140 	return 0;
141 }
142 
143 /* get current thread ID */
144 int
145 rte_sys_gettid(void)
146 {
147 	return GetCurrentThreadId();
148 }
149 
150 int
151 rte_thread_setname(__rte_unused pthread_t id, __rte_unused const char *name)
152 {
153 	/* TODO */
154 	/* This is a stub, not the expected result */
155 	return 0;
156 }
157