xref: /dpdk/lib/eal/linux/eal_thread.c (revision f6c6c686f134dac6a0b4540b1a6b85719991b6dc)
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 <pthread.h>
11 #include <sched.h>
12 #include <sys/queue.h>
13 #include <sys/syscall.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 worker lcore identified by worker_id to call a
30  * function f with argument arg. Once the execution is done, the
31  * remote lcore switches to WAIT state.
32  */
33 int
34 rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned int worker_id)
35 {
36 	int n;
37 	char c = 0;
38 	int m2w = lcore_config[worker_id].pipe_main2worker[1];
39 	int w2m = lcore_config[worker_id].pipe_worker2main[0];
40 	int rc = -EBUSY;
41 
42 	if (lcore_config[worker_id].state != WAIT)
43 		goto finish;
44 
45 	lcore_config[worker_id].f = f;
46 	lcore_config[worker_id].arg = arg;
47 
48 	/* send message */
49 	n = 0;
50 	while (n == 0 || (n < 0 && errno == EINTR))
51 		n = write(m2w, &c, 1);
52 	if (n < 0)
53 		rte_panic("cannot write on configuration pipe\n");
54 
55 	/* wait ack */
56 	do {
57 		n = read(w2m, &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, worker_id, rc);
66 	return rc;
67 }
68 
69 /* main loop of threads */
70 __rte_noreturn void *
71 eal_thread_loop(__rte_unused void *arg)
72 {
73 	char c;
74 	int n, ret;
75 	unsigned lcore_id;
76 	pthread_t thread_id;
77 	int m2w, w2m;
78 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
79 
80 	thread_id = pthread_self();
81 
82 	/* retrieve our lcore_id from the configuration structure */
83 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
84 		if (thread_id == lcore_config[lcore_id].thread_id)
85 			break;
86 	}
87 	if (lcore_id == RTE_MAX_LCORE)
88 		rte_panic("cannot retrieve lcore id\n");
89 
90 	m2w = lcore_config[lcore_id].pipe_main2worker[0];
91 	w2m = lcore_config[lcore_id].pipe_worker2main[1];
92 
93 	__rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
94 
95 	ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
96 	RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
97 		lcore_id, (uintptr_t)thread_id, cpuset, ret == 0 ? "" : "...");
98 
99 	rte_eal_trace_thread_lcore_ready(lcore_id, cpuset);
100 
101 	/* read on our pipe to get commands */
102 	while (1) {
103 		void *fct_arg;
104 
105 		/* wait command */
106 		do {
107 			n = read(m2w, &c, 1);
108 		} while (n < 0 && errno == EINTR);
109 
110 		if (n <= 0)
111 			rte_panic("cannot read on configuration pipe\n");
112 
113 		lcore_config[lcore_id].state = RUNNING;
114 
115 		/* send ack */
116 		n = 0;
117 		while (n == 0 || (n < 0 && errno == EINTR))
118 			n = write(w2m, &c, 1);
119 		if (n < 0)
120 			rte_panic("cannot write on configuration pipe\n");
121 
122 		if (lcore_config[lcore_id].f == NULL)
123 			rte_panic("NULL function pointer\n");
124 
125 		/* call the function and store the return value */
126 		fct_arg = lcore_config[lcore_id].arg;
127 		ret = lcore_config[lcore_id].f(fct_arg);
128 		lcore_config[lcore_id].ret = ret;
129 		lcore_config[lcore_id].f = NULL;
130 		lcore_config[lcore_id].arg = NULL;
131 		rte_wmb();
132 
133 		lcore_config[lcore_id].state = WAIT;
134 	}
135 
136 	/* never reached */
137 	/* pthread_exit(NULL); */
138 	/* return NULL; */
139 }
140 
141 /* require calling thread tid by gettid() */
142 int rte_sys_gettid(void)
143 {
144 	return (int)syscall(SYS_gettid);
145 }
146 
147 int rte_thread_setname(pthread_t id, const char *name)
148 {
149 	int ret = ENOSYS;
150 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
151 #if __GLIBC_PREREQ(2, 12)
152 	char truncated[16];
153 
154 	strlcpy(truncated, name, sizeof(truncated));
155 	ret = pthread_setname_np(id, truncated);
156 #endif
157 #endif
158 	RTE_SET_USED(id);
159 	RTE_SET_USED(name);
160 	return -ret;
161 }
162 
163 int rte_thread_getname(pthread_t id, char *name, size_t len)
164 {
165 	int ret = ENOSYS;
166 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
167 #if __GLIBC_PREREQ(2, 12)
168 	ret = pthread_getname_np(id, name, len);
169 #endif
170 #endif
171 	RTE_SET_USED(id);
172 	RTE_SET_USED(name);
173 	RTE_SET_USED(len);
174 	return -ret;
175 
176 }
177