1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
| | // SPDX-License-Identifier: GPL-2.0-or-later
/* PASST - Plug A Simple Socket Transport
* for qemu/UNIX domain socket mode
*
* PASTA - Pack A Subtle Tap Abstraction
* for network namespace/tap device mode
*
* threading.c - Basic threading infrastructure
*
* Copyright Red Hat
* Author: Laurent Vivier <lvivier@redhat.com>
*/
#include <errno.h>
#include <pthread.h>
#include <stdatomic.h>
#include "util.h"
#include "passt.h"
#include "threading.h"
/**
* struct threading_context - Per-thread execution context
* @epollfd: Epoll file descriptor for this thread
* @pthread: Pthread identifier
* @worker: Worker function to execute in this thread
* @opaque: Opaque data passed to worker function
* @quit: Flag to signal thread to exit
*/
struct threading_context {
pthread_t pthread;
void (*worker)(void *, int, struct epoll_event *);
void *opaque;
int epollfd;
_Atomic bool quit;
};
/*
* index 0 is reserved for main process
* new threads start at index 1
*/
struct threading_context threads[MAX_NUM_THREADS + 1];
/**
* threading_init() - Initialize threading infrastructure
*/
void threading_init(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(threads); i++) {
threads[i].epollfd = epoll_create1(EPOLL_CLOEXEC);
if (threads[i].epollfd == -1)
die_perror("Failed to create epoll for threads #%d",
i);
}
}
/**
* threading_worker_set() - Set worker function for a thread
* @threadid: Thread index
* @worker: Worker function to assign to the thread
* @is_valid: Validation function to select a thread
* @opaque: Opaque data to pass to worker function
*
* Return: 0 on success, -1 if thread index is invalid
*/
int threading_worker_set(unsigned int threadid,
void (*worker)(void *, int, struct epoll_event *),
void *opaque)
{
struct threading_context *tc;
if (threadid >= ARRAY_SIZE(threads))
return -1;
tc = &threads[threadid];
tc->worker = worker;
tc->opaque = opaque;
return 0;
}
/**
* threading_epollfd() - Get epoll file descriptor for a thread
* @threadid: Thread index
*
* Return: epoll file descriptor for the specified thread, -1 if index invalid
*/
int threading_epollfd(unsigned int threadid)
{
if (threadid >= ARRAY_SIZE(threads))
return -1;
return threads[threadid].epollfd;
}
/**
* threading_worker() - Main worker thread function
* @opaque: Pointer to threading_context for this thread
*
* Return: NULL on thread exit
*
* #syscalls poll write futex
*/
static void *threading_worker(void *opaque)
{
struct threading_context *tc = opaque;
while (!tc->quit) {
struct epoll_event events[NUM_EPOLL_EVENTS];
int nfds;
/* NOLINTBEGIN(bugprone-branch-clone): intervals can be the same */
/* cppcheck-suppress [duplicateValueTernary, unmatchedSuppression] */
nfds = epoll_wait(tc->epollfd, events, NUM_EPOLL_EVENTS,
TIMER_INTERVAL);
/* NOLINTEND(bugprone-branch-clone) */
if (nfds == -1 && errno != EINTR)
die_perror("epoll_wait() failed in thread loop");
tc->worker(tc->opaque, nfds, events);
}
return NULL;
}
/**
* threading_start_thread() - Start a worker thread
* @threadid: Thread index to start
*
* #syscalls rt_sigaction rt_sigprocmask mprotect getrandom brk clone3 rseq set_robust_list clock_nanosleep
*/
void threading_start_thread(unsigned int threadid)
{
struct threading_context *tc;
int ret;
if (threadid >= ARRAY_SIZE(threads))
die_perror("Invalid thread index %u, max is %u\n",
threadid, MAX_NUM_THREADS);
tc = &threads[threadid];
if (!tc->worker)
die_perror("No worker for thread #%u\n", threadid);
if (tc->pthread)
return;
/* thread #0 is the main process */
if (threadid == THREADING_ID_DEFAULT) {
tc->pthread = -1; /* Mark thread is active */
tc->quit = false;
/* Thread #0 is main process, call is blocking */
threading_worker(tc); /* no return */
}
tc->quit = false;
ret = pthread_create(&tc->pthread, NULL, &threading_worker, tc);
if (ret != 0)
die_perror("pthread_create() failed: thread index %u ret %d",
threadid, ret);
}
/**
* threading_stop_thread() - Stop and join a worker thread
* @threadid: Thread index to stop
*/
void threading_stop_thread(unsigned int threadid)
{
if (threadid >= ARRAY_SIZE(threads))
die_perror("Invalid thread index %u, max is %u\n",
threadid, ARRAY_SIZE(threads));
if (threadid == THREADING_ID_DEFAULT) {
/* Thread #0 is main process, cannot be stopped */
return;
}
threads[threadid].quit = true;
pthread_join(threads[threadid].pthread, NULL);
threads[threadid].pthread = 0;
}
/**
* threading_is_active() - Check if a worker thread is active
* @threadid: Thread index to check
*
* Return: true if the thread at the given index has been created and is active,
* false otherwise
*/
bool threading_is_active(unsigned int threadid)
{
if (threadid >= ARRAY_SIZE(threads))
die_perror("Invalid thread index %u, max is %u\n",
threadid, ARRAY_SIZE(threads));
return !!threads[threadid].pthread;
}
|