e149199a6fa1f7ed2f657f267778a49c0e59efce
[dpdk.git] / lib / librte_eal / windows / eal_thread.c
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 RTE_DEFINE_PER_LCORE(unsigned int, _lcore_id) = LCORE_ID_ANY;
20 RTE_DEFINE_PER_LCORE(unsigned int, _socket_id) = (unsigned int)SOCKET_ID_ANY;
21 RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
22
23 /*
24  * Send a message to a slave lcore identified by slave_id to call a
25  * function f with argument arg. Once the execution is done, the
26  * remote lcore switch in FINISHED state.
27  */
28 int
29 rte_eal_remote_launch(lcore_function_t *f, void *arg, unsigned int slave_id)
30 {
31         int n;
32         char c = 0;
33         int m2s = lcore_config[slave_id].pipe_master2slave[1];
34         int s2m = lcore_config[slave_id].pipe_slave2master[0];
35
36         if (lcore_config[slave_id].state != WAIT)
37                 return -EBUSY;
38
39         lcore_config[slave_id].f = f;
40         lcore_config[slave_id].arg = arg;
41
42         /* send message */
43         n = 0;
44         while (n == 0 || (n < 0 && errno == EINTR))
45                 n = _write(m2s, &c, 1);
46         if (n < 0)
47                 rte_panic("cannot write on configuration pipe\n");
48
49         /* wait ack */
50         do {
51                 n = _read(s2m, &c, 1);
52         } while (n < 0 && errno == EINTR);
53
54         if (n <= 0)
55                 rte_panic("cannot read on configuration pipe\n");
56
57         return 0;
58 }
59
60 void
61 eal_thread_init_master(unsigned int lcore_id)
62 {
63         /* set the lcore ID in per-lcore memory area */
64         RTE_PER_LCORE(_lcore_id) = lcore_id;
65 }
66
67 static inline pthread_t
68 eal_thread_self(void)
69 {
70         return GetCurrentThreadId();
71 }
72
73 /* main loop of threads */
74 void *
75 eal_thread_loop(void *arg __rte_unused)
76 {
77         char c;
78         int n, ret;
79         unsigned int lcore_id;
80         pthread_t thread_id;
81         int m2s, s2m;
82         char cpuset[RTE_CPU_AFFINITY_STR_LEN];
83
84         thread_id = eal_thread_self();
85
86         /* retrieve our lcore_id from the configuration structure */
87         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
88                 if (thread_id == lcore_config[lcore_id].thread_id)
89                         break;
90         }
91         if (lcore_id == RTE_MAX_LCORE)
92                 rte_panic("cannot retrieve lcore id\n");
93
94         m2s = lcore_config[lcore_id].pipe_master2slave[0];
95         s2m = lcore_config[lcore_id].pipe_slave2master[1];
96
97         /* set the lcore ID in per-lcore memory area */
98         RTE_PER_LCORE(_lcore_id) = lcore_id;
99
100         RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s])\n",
101                 lcore_id, (uintptr_t)thread_id, cpuset);
102
103         /* read on our pipe to get commands */
104         while (1) {
105                 void *fct_arg;
106
107                 /* wait command */
108                 do {
109                         n = _read(m2s, &c, 1);
110                 } while (n < 0 && errno == EINTR);
111
112                 if (n <= 0)
113                         rte_panic("cannot read on configuration pipe\n");
114
115                 lcore_config[lcore_id].state = RUNNING;
116
117                 /* send ack */
118                 n = 0;
119                 while (n == 0 || (n < 0 && errno == EINTR))
120                         n = _write(s2m, &c, 1);
121                 if (n < 0)
122                         rte_panic("cannot write on configuration pipe\n");
123
124                 if (lcore_config[lcore_id].f == NULL)
125                         rte_panic("NULL function pointer\n");
126
127                 /* call the function and store the return value */
128                 fct_arg = lcore_config[lcore_id].arg;
129                 ret = lcore_config[lcore_id].f(fct_arg);
130                 lcore_config[lcore_id].ret = ret;
131                 rte_wmb();
132
133                 /* when a service core returns, it should go directly to WAIT
134                  * state, because the application will not lcore_wait() for it.
135                  */
136                 if (lcore_config[lcore_id].core_role == ROLE_SERVICE)
137                         lcore_config[lcore_id].state = WAIT;
138                 else
139                         lcore_config[lcore_id].state = FINISHED;
140         }
141 }
142
143 /* function to create threads */
144 int
145 eal_thread_create(pthread_t *thread)
146 {
147         HANDLE th;
148
149         th = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)eal_thread_loop,
150                                                 NULL, 0, (LPDWORD)thread);
151         if (!th)
152                 return -1;
153
154         SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
155         SetThreadPriority(th, THREAD_PRIORITY_TIME_CRITICAL);
156
157         return 0;
158 }
159
160 int
161 rte_thread_setname(__rte_unused pthread_t id, __rte_unused const char *name)
162 {
163         /* TODO */
164         /* This is a stub, not the expected result */
165         return 0;
166 }