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