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