eal: relocate per thread symbols to common
[dpdk.git] / lib / librte_eal / freebsd / eal_thread.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <errno.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <unistd.h>
10 #include <sched.h>
11 #include <pthread_np.h>
12 #include <sys/queue.h>
13 #include <sys/thr.h>
14
15 #include <rte_debug.h>
16 #include <rte_atomic.h>
17 #include <rte_launch.h>
18 #include <rte_log.h>
19 #include <rte_memory.h>
20 #include <rte_per_lcore.h>
21 #include <rte_eal.h>
22 #include <rte_lcore.h>
23 #include <rte_eal_trace.h>
24
25 #include "eal_private.h"
26 #include "eal_thread.h"
27
28 /*
29  * Send a message to a slave lcore identified by slave_id to call a
30  * function f with argument arg. Once the execution is done, the
31  * remote lcore switch in FINISHED state.
32  */
33 int
34 rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned slave_id)
35 {
36         int n;
37         char c = 0;
38         int m2s = lcore_config[slave_id].pipe_master2slave[1];
39         int s2m = lcore_config[slave_id].pipe_slave2master[0];
40         int rc = -EBUSY;
41
42         if (lcore_config[slave_id].state != WAIT)
43                 goto finish;
44
45         lcore_config[slave_id].f = f;
46         lcore_config[slave_id].arg = arg;
47
48         /* send message */
49         n = 0;
50         while (n == 0 || (n < 0 && errno == EINTR))
51                 n = write(m2s, &c, 1);
52         if (n < 0)
53                 rte_panic("cannot write on configuration pipe\n");
54
55         /* wait ack */
56         do {
57                 n = read(s2m, &c, 1);
58         } while (n < 0 && errno == EINTR);
59
60         if (n <= 0)
61                 rte_panic("cannot read on configuration pipe\n");
62
63         rc = 0;
64 finish:
65         rte_eal_trace_thread_remote_launch(f, arg, slave_id, rc);
66         return rc;
67 }
68
69 /* set affinity for current thread */
70 static int
71 eal_thread_set_affinity(void)
72 {
73         unsigned lcore_id = rte_lcore_id();
74
75         /* acquire system unique id  */
76         rte_gettid();
77
78         /* update EAL thread core affinity */
79         return rte_thread_set_affinity(&lcore_config[lcore_id].cpuset);
80 }
81
82 void eal_thread_init_master(unsigned lcore_id)
83 {
84         /* set the lcore ID in per-lcore memory area */
85         RTE_PER_LCORE(_lcore_id) = lcore_id;
86
87         /* set CPU affinity */
88         if (eal_thread_set_affinity() < 0)
89                 rte_panic("cannot set affinity\n");
90 }
91
92 /* main loop of threads */
93 __rte_noreturn void *
94 eal_thread_loop(__rte_unused void *arg)
95 {
96         char c;
97         int n, ret;
98         unsigned lcore_id;
99         pthread_t thread_id;
100         int m2s, s2m;
101         char cpuset[RTE_CPU_AFFINITY_STR_LEN];
102
103         thread_id = pthread_self();
104
105         /* retrieve our lcore_id from the configuration structure */
106         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
107                 if (thread_id == lcore_config[lcore_id].thread_id)
108                         break;
109         }
110         if (lcore_id == RTE_MAX_LCORE)
111                 rte_panic("cannot retrieve lcore id\n");
112
113         m2s = lcore_config[lcore_id].pipe_master2slave[0];
114         s2m = lcore_config[lcore_id].pipe_slave2master[1];
115
116         /* set the lcore ID in per-lcore memory area */
117         RTE_PER_LCORE(_lcore_id) = lcore_id;
118
119         /* set CPU affinity */
120         if (eal_thread_set_affinity() < 0)
121                 rte_panic("cannot set affinity\n");
122
123         ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
124
125         RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
126                 lcore_id, thread_id, cpuset, ret == 0 ? "" : "...");
127
128         __rte_trace_mem_per_thread_alloc();
129         rte_eal_trace_thread_lcore_ready(lcore_id, cpuset);
130
131         /* read on our pipe to get commands */
132         while (1) {
133                 void *fct_arg;
134
135                 /* wait command */
136                 do {
137                         n = read(m2s, &c, 1);
138                 } while (n < 0 && errno == EINTR);
139
140                 if (n <= 0)
141                         rte_panic("cannot read on configuration pipe\n");
142
143                 lcore_config[lcore_id].state = RUNNING;
144
145                 /* send ack */
146                 n = 0;
147                 while (n == 0 || (n < 0 && errno == EINTR))
148                         n = write(s2m, &c, 1);
149                 if (n < 0)
150                         rte_panic("cannot write on configuration pipe\n");
151
152                 if (lcore_config[lcore_id].f == NULL)
153                         rte_panic("NULL function pointer\n");
154
155                 /* call the function and store the return value */
156                 fct_arg = lcore_config[lcore_id].arg;
157                 ret = lcore_config[lcore_id].f(fct_arg);
158                 lcore_config[lcore_id].ret = ret;
159                 rte_wmb();
160                 lcore_config[lcore_id].state = FINISHED;
161         }
162
163         /* never reached */
164         /* pthread_exit(NULL); */
165         /* return NULL; */
166 }
167
168 /* require calling thread tid by gettid() */
169 int rte_sys_gettid(void)
170 {
171         long lwpid;
172         thr_self(&lwpid);
173         return (int)lwpid;
174 }
175
176 int rte_thread_setname(pthread_t id, const char *name)
177 {
178         /* this BSD function returns no error */
179         pthread_set_name_np(id, name);
180         return 0;
181 }
182
183 int rte_thread_getname(pthread_t id, char *name, size_t len)
184 {
185         RTE_SET_USED(id);
186         RTE_SET_USED(name);
187         RTE_SET_USED(len);
188
189         return -ENOTSUP;
190 }