Bug Summary

File:lwan-thread.c
Warning:line 701, column 9
Assigned value is garbage or undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name lwan-thread.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -fno-plt -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib/clang/10.0.1 -include /home/buildbot/lwan-worker/clang-analyze/build/lwan-build-config.h -D _FILE_OFFSET_BITS=64 -D _TIME_BITS=64 -I /home/buildbot/lwan-worker/clang-analyze/build/src/lib/missing -I /usr/include/luajit-2.0 -I /usr/include/valgrind -I /home/buildbot/lwan-worker/clang-analyze/build/src/lib -I /home/buildbot/lwan-worker/clang-analyze/build -internal-isystem /usr/local/include -internal-isystem /usr/lib/clang/10.0.1/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-unused-parameter -Wno-free-nonheap-object -std=gnu99 -fdebug-compilation-dir /home/buildbot/lwan-worker/clang-analyze/build/src/lib -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -analyzer-output=html -faddrsig -o /home/buildbot/lwan-worker/clang-analyze/CLANG/2020-09-27-210350-2340170-1 -x c /home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c
1/*
2 * lwan - simple web server
3 * Copyright (c) 2012, 2013 Leandro A. F. Pereira <leandro@hardinfo.org>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 * USA.
19 */
20
21#define _GNU_SOURCE
22#include <assert.h>
23#include <errno(*__errno_location ()).h>
24#include <fcntl.h>
25#include <pthread.h>
26#include <sched.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/epoll.h>
30#include <sys/ioctl.h>
31#include <sys/socket.h>
32#include <unistd.h>
33
34#if defined(HAVE_EVENTFD)
35#include <sys/eventfd.h>
36#endif
37
38#include "lwan-private.h"
39#include "lwan-tq.h"
40#include "list.h"
41
42static void lwan_strbuf_free_defer(void *data)
43{
44 lwan_strbuf_free((struct lwan_strbuf *)data);
45}
46
47static void graceful_close(struct lwan *l,
48 struct lwan_connection *conn,
49 char buffer[static DEFAULT_BUFFER_SIZE4096])
50{
51 int fd = lwan_connection_get_fd(l, conn);
52
53 while (TIOCOUTQ0x5411) {
54 /* This ioctl isn't probably doing what it says on the tin; the details
55 * are subtle, but it seems to do the trick to allow gracefully closing
56 * the connection in some cases with minimal system calls. */
57 int bytes_waiting;
58 int r = ioctl(fd, TIOCOUTQ0x5411, &bytes_waiting);
59
60 if (!r && !bytes_waiting) /* See note about close(2) below. */
61 return;
62 if (r < 0 && errno(*__errno_location ()) == EINTR4)
63 continue;
64
65 break;
66 }
67
68 if (UNLIKELY(shutdown(fd, SHUT_WR) < 0)__builtin_expect(((shutdown(fd, SHUT_WR) < 0)), (0))) {
69 if (UNLIKELY(errno == ENOTCONN)__builtin_expect((((*__errno_location ()) == 107)), (0)))
70 return;
71 }
72
73 for (int tries = 0; tries < 20; tries++) {
74 ssize_t r = read(fd, buffer, DEFAULT_BUFFER_SIZE4096);
75
76 if (!r)
77 break;
78
79 if (r < 0) {
80 switch (errno(*__errno_location ())) {
81 case EAGAIN11:
82 coro_yield(conn->coro, CONN_CORO_WANT_READ);
83 /* Fallthrough */
84 case EINTR4:
85 continue;
86 default:
87 return;
88 }
89 }
90
91 coro_yield(conn->coro, CONN_CORO_YIELD);
92 }
93
94 /* close(2) will be called when the coroutine yields with CONN_CORO_ABORT */
95}
96
97__attribute__((noreturn)) static int process_request_coro(struct coro *coro,
98 void *data)
99{
100 /* NOTE: This function should not return; coro_yield should be used
101 * instead. This ensures the storage for `strbuf` is alive when the
102 * coroutine ends and lwan_strbuf_free() is called. */
103 struct lwan_connection *conn = data;
104 struct lwan *lwan = conn->thread->lwan;
105 int fd = lwan_connection_get_fd(lwan, conn);
106 enum lwan_request_flags flags = lwan->config.request_flags;
107 struct lwan_strbuf strbuf = LWAN_STRBUF_STATIC_INIT(struct lwan_strbuf) { .buffer = "" };
108 char request_buffer[DEFAULT_BUFFER_SIZE4096];
109 struct lwan_value buffer = {.value = request_buffer, .len = 0};
110 char *next_request = NULL((void*)0);
111 char *header_start[N_HEADER_START64];
112 struct lwan_proxy proxy;
113 const int error_when_n_packets = lwan_calculate_n_packets(DEFAULT_BUFFER_SIZE4096);
114
115 coro_defer(coro, lwan_strbuf_free_defer, &strbuf);
116
117 const size_t init_gen = 1; /* 1 call to coro_defer() */
118 assert(init_gen == coro_deferred_get_generation(coro))((void) sizeof ((init_gen == coro_deferred_get_generation(coro
)) ? 1 : 0), __extension__ ({ if (init_gen == coro_deferred_get_generation
(coro)) ; else __assert_fail ("init_gen == coro_deferred_get_generation(coro)"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 118, __extension__ __PRETTY_FUNCTION__); }))
;
119
120 while (true1) {
121 struct lwan_request_parser_helper helper = {
122 .buffer = &buffer,
123 .next_request = next_request,
124 .error_when_n_packets = error_when_n_packets,
125 .header_start = header_start,
126 };
127 struct lwan_request request = {.conn = conn,
128 .global_response_headers = &lwan->headers,
129 .fd = fd,
130 .response = {.buffer = &strbuf},
131 .flags = flags,
132 .proxy = &proxy,
133 .helper = &helper};
134
135 lwan_process_request(lwan, &request);
136
137 /* Run the deferred instructions now (except those used to initialize
138 * the coroutine), so that if the connection is gracefully closed,
139 * the storage for ``helper'' is still there. */
140 coro_deferred_run(coro, init_gen);
141
142 if (UNLIKELY(!(conn->flags & CONN_IS_KEEP_ALIVE))__builtin_expect(((!(conn->flags & CONN_IS_KEEP_ALIVE)
)), (0))
) {
143 graceful_close(lwan, conn, request_buffer);
144 break;
145 }
146
147 if (next_request && *next_request) {
148 conn->flags |= CONN_CORK;
149
150 if (!(conn->flags & CONN_EVENTS_WRITE))
151 coro_yield(coro, CONN_CORO_WANT_WRITE);
152 } else {
153 conn->flags &= ~CONN_CORK;
154 coro_yield(coro, CONN_CORO_WANT_READ);
155 }
156
157 lwan_strbuf_reset(&strbuf);
158
159 /* Only allow flags from config. */
160 flags = request.flags & (REQUEST_PROXIED | REQUEST_ALLOW_CORS);
161 next_request = helper.next_request;
162 }
163
164 coro_yield(coro, CONN_CORO_ABORT);
165 __builtin_unreachable();
166}
167
168static ALWAYS_INLINEinline __attribute__((always_inline)) uint32_t
169conn_flags_to_epoll_events(enum lwan_connection_flags flags)
170{
171 static const uint32_t map[CONN_EVENTS_MASK + 1] = {
172 [0 /* Suspended (timer or await) */] = EPOLLRDHUPEPOLLRDHUP,
173 [CONN_EVENTS_WRITE] = EPOLLOUTEPOLLOUT | EPOLLRDHUPEPOLLRDHUP,
174 [CONN_EVENTS_READ] = EPOLLINEPOLLIN | EPOLLRDHUPEPOLLRDHUP,
175 [CONN_EVENTS_READ_WRITE] = EPOLLINEPOLLIN | EPOLLOUTEPOLLOUT | EPOLLRDHUPEPOLLRDHUP,
176 };
177
178 return map[flags & CONN_EVENTS_MASK];
179}
180
181static void update_epoll_flags(int fd,
182 struct lwan_connection *conn,
183 int epoll_fd,
184 enum lwan_connection_coro_yield yield_result)
185{
186 static const enum lwan_connection_flags or_mask[CONN_CORO_MAX] = {
187 [CONN_CORO_YIELD] = 0,
188
189 [CONN_CORO_WANT_READ_WRITE] = CONN_EVENTS_READ_WRITE,
190 [CONN_CORO_WANT_READ] = CONN_EVENTS_READ,
191 [CONN_CORO_WANT_WRITE] = CONN_EVENTS_WRITE,
192
193 /* While the coro is suspended, we're not interested in either EPOLLIN
194 * or EPOLLOUT events. We still want to track this fd in epoll, though,
195 * so unset both so that only EPOLLRDHUP (plus the implicitly-set ones)
196 * are set. */
197 [CONN_CORO_SUSPEND_TIMER] = CONN_SUSPENDED_TIMER,
198 [CONN_CORO_SUSPEND_ASYNC_AWAIT] = CONN_SUSPENDED_ASYNC_AWAIT,
199
200 /* Ideally, when suspending a coroutine, the current flags&CONN_EVENTS_MASK
201 * would have to be stored and restored -- however, resuming as if the
202 * client coroutine is interested in a write event always guarantees that
203 * they'll be resumed as they're TCP sockets. There's a good chance that
204 * trying to read from a socket after resuming a coroutine will succeed,
205 * but if it doesn't because read() returns -EAGAIN, the I/O wrappers will
206 * yield with CONN_CORO_WANT_READ anyway. */
207 [CONN_CORO_RESUME] = CONN_EVENTS_WRITE,
208 };
209 static const enum lwan_connection_flags and_mask[CONN_CORO_MAX] = {
210 [CONN_CORO_YIELD] = ~0,
211
212 [CONN_CORO_WANT_READ_WRITE] = ~0,
213 [CONN_CORO_WANT_READ] = ~CONN_EVENTS_WRITE,
214 [CONN_CORO_WANT_WRITE] = ~CONN_EVENTS_READ,
215
216 [CONN_CORO_SUSPEND_TIMER] = ~(CONN_EVENTS_READ_WRITE | CONN_SUSPENDED_ASYNC_AWAIT),
217 [CONN_CORO_SUSPEND_ASYNC_AWAIT] = ~(CONN_EVENTS_READ_WRITE | CONN_SUSPENDED_TIMER),
218 [CONN_CORO_RESUME] = ~CONN_SUSPENDED,
219 };
220 enum lwan_connection_flags prev_flags = conn->flags;
221
222 conn->flags |= or_mask[yield_result];
223 conn->flags &= and_mask[yield_result];
224
225 if (conn->flags == prev_flags)
226 return;
227
228 struct epoll_event event = {
229 .events = conn_flags_to_epoll_events(conn->flags),
230 .data.ptr = conn,
231 };
232
233 if (UNLIKELY(epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &event) < 0)__builtin_expect(((epoll_ctl(epoll_fd, 3, fd, &event) <
0)), (0))
)
234 lwan_status_perror("epoll_ctl")lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 234, __FUNCTION__, "epoll_ctl")
;
235}
236
237static void clear_async_await_flag(void *data)
238{
239 struct lwan_connection *async_fd_conn = data;
240
241 async_fd_conn->flags &= ~CONN_ASYNC_AWAIT;
242}
243
244static enum lwan_connection_coro_yield
245resume_async(struct timeout_queue *tq,
246 enum lwan_connection_coro_yield yield_result,
247 int64_t from_coro,
248 struct lwan_connection *conn,
249 int epoll_fd)
250{
251 static const enum lwan_connection_flags to_connection_flags[] = {
252 [CONN_CORO_ASYNC_AWAIT_READ] = CONN_EVENTS_READ,
253 [CONN_CORO_ASYNC_AWAIT_WRITE] = CONN_EVENTS_WRITE,
254 [CONN_CORO_ASYNC_AWAIT_READ_WRITE] = CONN_EVENTS_READ_WRITE,
255 };
256 int await_fd = (int)((uint64_t)from_coro >> 32);
257 enum lwan_connection_flags flags;
258 int op;
259
260 assert(await_fd >= 0)((void) sizeof ((await_fd >= 0) ? 1 : 0), __extension__ ({
if (await_fd >= 0) ; else __assert_fail ("await_fd >= 0"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 260, __extension__ __PRETTY_FUNCTION__); }))
;
261 assert(yield_result >= CONN_CORO_ASYNC_AWAIT_READ &&((void) sizeof ((yield_result >= CONN_CORO_ASYNC_AWAIT_READ
&& yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE
) ? 1 : 0), __extension__ ({ if (yield_result >= CONN_CORO_ASYNC_AWAIT_READ
&& yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE
) ; else __assert_fail ("yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 262, __extension__ __PRETTY_FUNCTION__); }))
262 yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE)((void) sizeof ((yield_result >= CONN_CORO_ASYNC_AWAIT_READ
&& yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE
) ? 1 : 0), __extension__ ({ if (yield_result >= CONN_CORO_ASYNC_AWAIT_READ
&& yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE
) ; else __assert_fail ("yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 262, __extension__ __PRETTY_FUNCTION__); }))
;
263
264 flags = to_connection_flags[yield_result];
265
266 struct lwan_connection *await_fd_conn = &tq->lwan->conns[await_fd];
267 if (LIKELY(await_fd_conn->flags & CONN_ASYNC_AWAIT)__builtin_expect((!!(await_fd_conn->flags & CONN_ASYNC_AWAIT
)), (1))
) {
268 if (LIKELY((await_fd_conn->flags & CONN_EVENTS_MASK) == flags)__builtin_expect((!!((await_fd_conn->flags & CONN_EVENTS_MASK
) == flags)), (1))
)
269 return CONN_CORO_SUSPEND_ASYNC_AWAIT;
270
271 op = EPOLL_CTL_MOD3;
272 } else {
273 op = EPOLL_CTL_ADD1;
274 flags |= CONN_ASYNC_AWAIT;
275 coro_defer(conn->coro, clear_async_await_flag, await_fd_conn);
276 }
277
278 struct epoll_event event = {.events = conn_flags_to_epoll_events(flags),
279 .data.ptr = conn};
280 if (LIKELY(!epoll_ctl(epoll_fd, op, await_fd, &event))__builtin_expect((!!(!epoll_ctl(epoll_fd, op, await_fd, &
event))), (1))
) {
281 await_fd_conn->flags &= ~CONN_EVENTS_MASK;
282 await_fd_conn->flags |= flags;
283 return CONN_CORO_SUSPEND_ASYNC_AWAIT;
284 }
285
286 return CONN_CORO_ABORT;
287}
288
289static ALWAYS_INLINEinline __attribute__((always_inline)) void resume_coro(struct timeout_queue *tq,
290 struct lwan_connection *conn,
291 int epoll_fd)
292{
293 assert(conn->coro)((void) sizeof ((conn->coro) ? 1 : 0), __extension__ ({ if
(conn->coro) ; else __assert_fail ("conn->coro", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 293, __extension__ __PRETTY_FUNCTION__); }))
;
294
295 int64_t from_coro = coro_resume(conn->coro);
296 enum lwan_connection_coro_yield yield_result = from_coro & 0xffffffff;
297
298 if (UNLIKELY(yield_result >= CONN_CORO_ASYNC)__builtin_expect(((yield_result >= CONN_CORO_ASYNC)), (0)))
299 yield_result = resume_async(tq, yield_result, from_coro, conn, epoll_fd);
300
301 if (UNLIKELY(yield_result == CONN_CORO_ABORT)__builtin_expect(((yield_result == CONN_CORO_ABORT)), (0)))
302 return timeout_queue_expire(tq, conn);
303
304 return update_epoll_flags(lwan_connection_get_fd(tq->lwan, conn), conn,
305 epoll_fd, yield_result);
306}
307
308static void update_date_cache(struct lwan_thread *thread)
309{
310 time_t now = time(NULL((void*)0));
311
312 lwan_format_rfc_time(now, thread->date.date);
313 lwan_format_rfc_time(now + (time_t)thread->lwan->config.expires,
314 thread->date.expires);
315}
316
317static ALWAYS_INLINEinline __attribute__((always_inline)) void spawn_coro(struct lwan_connection *conn,
318 struct coro_switcher *switcher,
319 struct timeout_queue *tq)
320{
321 struct lwan_thread *t = conn->thread;
322
323 assert(!conn->coro)((void) sizeof ((!conn->coro) ? 1 : 0), __extension__ ({ if
(!conn->coro) ; else __assert_fail ("!conn->coro", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 323, __extension__ __PRETTY_FUNCTION__); }))
;
324 assert(t)((void) sizeof ((t) ? 1 : 0), __extension__ ({ if (t) ; else __assert_fail
("t", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 324, __extension__ __PRETTY_FUNCTION__); }))
;
325 assert((uintptr_t)t >= (uintptr_t)tq->lwan->thread.threads)((void) sizeof (((uintptr_t)t >= (uintptr_t)tq->lwan->
thread.threads) ? 1 : 0), __extension__ ({ if ((uintptr_t)t >=
(uintptr_t)tq->lwan->thread.threads) ; else __assert_fail
("(uintptr_t)t >= (uintptr_t)tq->lwan->thread.threads"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 325, __extension__ __PRETTY_FUNCTION__); }))
;
326 assert((uintptr_t)t <((void) sizeof (((uintptr_t)t < (uintptr_t)(tq->lwan->
thread.threads + tq->lwan->thread.count)) ? 1 : 0), __extension__
({ if ((uintptr_t)t < (uintptr_t)(tq->lwan->thread.
threads + tq->lwan->thread.count)) ; else __assert_fail
("(uintptr_t)t < (uintptr_t)(tq->lwan->thread.threads + tq->lwan->thread.count)"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 327, __extension__ __PRETTY_FUNCTION__); }))
327 (uintptr_t)(tq->lwan->thread.threads + tq->lwan->thread.count))((void) sizeof (((uintptr_t)t < (uintptr_t)(tq->lwan->
thread.threads + tq->lwan->thread.count)) ? 1 : 0), __extension__
({ if ((uintptr_t)t < (uintptr_t)(tq->lwan->thread.
threads + tq->lwan->thread.count)) ; else __assert_fail
("(uintptr_t)t < (uintptr_t)(tq->lwan->thread.threads + tq->lwan->thread.count)"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 327, __extension__ __PRETTY_FUNCTION__); }))
;
328
329 *conn = (struct lwan_connection) {
330 .coro = coro_new(switcher, process_request_coro, conn),
331 .flags = CONN_EVENTS_READ,
332 .time_to_expire = tq->current_time + tq->move_to_last_bump,
333 .thread = t,
334 };
335 if (UNLIKELY(!conn->coro)__builtin_expect(((!conn->coro)), (0))) {
336 conn->flags = 0;
337 lwan_status_error("Could not create coroutine")lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 337, __FUNCTION__, "Could not create coroutine")
;
338 return;
339 }
340
341 timeout_queue_insert(tq, conn);
342}
343
344static void accept_nudge(int pipe_fd,
345 struct lwan_thread *t,
346 struct lwan_connection *conns,
347 struct timeout_queue *tq,
348 struct coro_switcher *switcher,
349 int epoll_fd)
350{
351 uint64_t event;
352 int new_fd;
353
354 /* Errors are ignored here as pipe_fd serves just as a way to wake the
355 * thread from epoll_wait(). It's fine to consume the queue at this
356 * point, regardless of the error type. */
357 (void)read(pipe_fd, &event, sizeof(event));
358
359 while (spsc_queue_pop(&t->pending_fds, &new_fd)) {
360 struct lwan_connection *conn = &conns[new_fd];
361 struct epoll_event ev = {
362 .data.ptr = conn,
363 .events = conn_flags_to_epoll_events(CONN_EVENTS_READ),
364 };
365
366 if (LIKELY(!epoll_ctl(epoll_fd, EPOLL_CTL_ADD, new_fd, &ev))__builtin_expect((!!(!epoll_ctl(epoll_fd, 1, new_fd, &ev)
)), (1))
)
367 spawn_coro(conn, switcher, tq);
368 }
369
370 timeouts_add(t->wheel, &tq->timeout, 1000);
371}
372
373static bool_Bool process_pending_timers(struct timeout_queue *tq,
374 struct lwan_thread *t,
375 int epoll_fd)
376{
377 struct timeout *timeout;
378 bool_Bool should_expire_timers = false0;
379
380 while ((timeout = timeouts_get(t->wheel))) {
381 struct lwan_request *request;
382
383 if (timeout == &tq->timeout) {
384 should_expire_timers = true1;
385 continue;
386 }
387
388 request = container_of(timeout, struct lwan_request, timeout)((struct lwan_request *) ((char *)(timeout) - __builtin_offsetof
(struct lwan_request, timeout)) + ((typeof(*(timeout)) *)0 !=
(typeof(((struct lwan_request *)0)->timeout) *)0))
;
389
390 update_epoll_flags(request->fd, request->conn, epoll_fd,
391 CONN_CORO_RESUME);
392 }
393
394 if (should_expire_timers) {
395 timeout_queue_expire_waiting(tq);
396
397 /* tq timeout expires every 1000ms if there are connections, so
398 * update the date cache at this point as well. */
399 update_date_cache(t);
400
401 if (!timeout_queue_empty(tq)) {
402 timeouts_add(t->wheel, &tq->timeout, 1000);
403 return true1;
404 }
405
406 timeouts_del(t->wheel, &tq->timeout);
407 }
408
409 return false0;
410}
411
412static int
413turn_timer_wheel(struct timeout_queue *tq, struct lwan_thread *t, int epoll_fd)
414{
415 timeout_t wheel_timeout;
416 struct timespec now;
417
418 if (UNLIKELY(clock_gettime(monotonic_clock_id, &now) < 0)__builtin_expect(((clock_gettime(monotonic_clock_id, &now
) < 0)), (0))
)
419 lwan_status_critical("Could not get monotonic time")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 419, __FUNCTION__, "Could not get monotonic time")
;
420
421 timeouts_update(t->wheel,
422 (timeout_t)(now.tv_sec * 1000 + now.tv_nsec / 1000000));
423
424 wheel_timeout = timeouts_timeout(t->wheel);
425 if (UNLIKELY((int64_t)wheel_timeout < 0)__builtin_expect((((int64_t)wheel_timeout < 0)), (0)))
426 goto infinite_timeout;
427
428 if (wheel_timeout == 0) {
429 if (!process_pending_timers(tq, t, epoll_fd))
430 goto infinite_timeout;
431
432 wheel_timeout = timeouts_timeout(t->wheel);
433 if (wheel_timeout == 0)
434 goto infinite_timeout;
435 }
436
437 return (int)wheel_timeout;
438
439infinite_timeout:
440 return -1;
441}
442
443static void *thread_io_loop(void *data)
444{
445 struct lwan_thread *t = data;
446 int epoll_fd = t->epoll_fd;
447 const int read_pipe_fd = t->pipe_fd[0];
448 const int max_events = LWAN_MIN((int)t->lwan->thread.max_fd, 1024)({ const __typeof__(((int)t->lwan->thread.max_fd) + 0) lwan_tmp_id6
= ((int)t->lwan->thread.max_fd); const __typeof__((1024
) + 0) lwan_tmp_id7 = (1024); lwan_tmp_id6 > lwan_tmp_id7 ?
lwan_tmp_id7 : lwan_tmp_id6; })
;
449 struct lwan *lwan = t->lwan;
450 struct epoll_event *events;
451 struct coro_switcher switcher;
452 struct timeout_queue tq;
453
454 lwan_status_debug("Worker thread #%zd starting",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 455, __FUNCTION__, "Worker thread #%zd starting", t - t->
lwan->thread.threads + 1)
455 t - t->lwan->thread.threads + 1)lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 455, __FUNCTION__, "Worker thread #%zd starting", t - t->
lwan->thread.threads + 1)
;
456 lwan_set_thread_name("worker");
457
458 events = calloc((size_t)max_events, sizeof(*events));
459 if (UNLIKELY(!events)__builtin_expect(((!events)), (0)))
460 lwan_status_critical("Could not allocate memory for events")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 460, __FUNCTION__, "Could not allocate memory for events")
;
461
462 update_date_cache(t);
463
464 timeout_queue_init(&tq, lwan);
465
466 pthread_barrier_wait(&lwan->thread.barrier);
467
468 for (;;) {
469 int timeout = turn_timer_wheel(&tq, t, epoll_fd);
470 int n_fds = epoll_wait(epoll_fd, events, max_events, timeout);
471
472 if (UNLIKELY(n_fds < 0)__builtin_expect(((n_fds < 0)), (0))) {
473 if (errno(*__errno_location ()) == EBADF9 || errno(*__errno_location ()) == EINVAL22)
474 break;
475 continue;
476 }
477
478 for (struct epoll_event *event = events; n_fds--; event++) {
479 struct lwan_connection *conn;
480
481 if (UNLIKELY(!event->data.ptr)__builtin_expect(((!event->data.ptr)), (0))) {
482 accept_nudge(read_pipe_fd, t, lwan->conns, &tq, &switcher,
483 epoll_fd);
484 continue;
485 }
486
487 conn = event->data.ptr;
488
489 if (UNLIKELY(event->events & (EPOLLRDHUP | EPOLLHUP))__builtin_expect(((event->events & (EPOLLRDHUP | EPOLLHUP
))), (0))
) {
490 timeout_queue_expire(&tq, conn);
491 continue;
492 }
493
494 resume_coro(&tq, conn, epoll_fd);
495 timeout_queue_move_to_last(&tq, conn);
496 }
497 }
498
499 pthread_barrier_wait(&lwan->thread.barrier);
500
501 timeout_queue_expire_all(&tq);
502 free(events);
503
504 return NULL((void*)0);
505}
506
507static void create_thread(struct lwan *l, struct lwan_thread *thread,
508 const size_t n_queue_fds)
509{
510 int ignore;
511 pthread_attr_t attr;
512
513 memset(thread, 0, sizeof(*thread));
514 thread->lwan = l;
515
516 thread->wheel = timeouts_open(&ignore);
517 if (!thread->wheel)
518 lwan_status_critical("Could not create timer wheel")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 518, __FUNCTION__, "Could not create timer wheel")
;
519
520 if ((thread->epoll_fd = epoll_create1(EPOLL_CLOEXECEPOLL_CLOEXEC)) < 0)
521 lwan_status_critical_perror("epoll_create")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 521, __FUNCTION__, "epoll_create")
;
522
523 if (pthread_attr_init(&attr))
524 lwan_status_critical_perror("pthread_attr_init")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 524, __FUNCTION__, "pthread_attr_init")
;
525
526 if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEMPTHREAD_SCOPE_SYSTEM))
527 lwan_status_critical_perror("pthread_attr_setscope")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 527, __FUNCTION__, "pthread_attr_setscope")
;
528
529 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLEPTHREAD_CREATE_JOINABLE))
530 lwan_status_critical_perror("pthread_attr_setdetachstate")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 530, __FUNCTION__, "pthread_attr_setdetachstate")
;
531
532#if defined(HAVE_EVENTFD)
533 int efd = eventfd(0, EFD_NONBLOCKEFD_NONBLOCK | EFD_SEMAPHOREEFD_SEMAPHORE | EFD_CLOEXECEFD_CLOEXEC);
534 if (efd < 0)
535 lwan_status_critical_perror("eventfd")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 535, __FUNCTION__, "eventfd")
;
536
537 thread->pipe_fd[0] = thread->pipe_fd[1] = efd;
538#else
539 if (pipe2(thread->pipe_fd, O_NONBLOCK04000 | O_CLOEXEC02000000) < 0)
540 lwan_status_critical_perror("pipe")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 540, __FUNCTION__, "pipe")
;
541#endif
542
543 struct epoll_event event = { .events = EPOLLINEPOLLIN, .data.ptr = NULL((void*)0) };
544 if (epoll_ctl(thread->epoll_fd, EPOLL_CTL_ADD1, thread->pipe_fd[0], &event) < 0)
545 lwan_status_critical_perror("epoll_ctl")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 545, __FUNCTION__, "epoll_ctl")
;
546
547 if (pthread_create(&thread->self, &attr, thread_io_loop, thread))
548 lwan_status_critical_perror("pthread_create")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 548, __FUNCTION__, "pthread_create")
;
549
550 if (pthread_attr_destroy(&attr))
551 lwan_status_critical_perror("pthread_attr_destroy")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 551, __FUNCTION__, "pthread_attr_destroy")
;
552
553 if (spsc_queue_init(&thread->pending_fds, n_queue_fds) < 0) {
554 lwan_status_critical("Could not initialize pending fd "lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 555, __FUNCTION__, "Could not initialize pending fd " "queue width %zu elements"
, n_queue_fds)
555 "queue width %zu elements", n_queue_fds)lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 555, __FUNCTION__, "Could not initialize pending fd " "queue width %zu elements"
, n_queue_fds)
;
556 }
557}
558
559void lwan_thread_nudge(struct lwan_thread *t)
560{
561 uint64_t event = 1;
562
563 if (UNLIKELY(write(t->pipe_fd[1], &event, sizeof(event)) < 0)__builtin_expect(((write(t->pipe_fd[1], &event, sizeof
(event)) < 0)), (0))
)
564 lwan_status_perror("write")lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 564, __FUNCTION__, "write")
;
565}
566
567void lwan_thread_add_client(struct lwan_thread *t, int fd)
568{
569 for (int i = 0; i < 10; i++) {
570 bool_Bool pushed = spsc_queue_push(&t->pending_fds, fd);
571
572 if (LIKELY(pushed)__builtin_expect((!!(pushed)), (1)))
573 return;
574
575 /* Queue is full; nudge the thread to consume it. */
576 lwan_thread_nudge(t);
577 }
578
579 lwan_status_error("Dropping connection %d", fd)lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 579, __FUNCTION__, "Dropping connection %d", fd)
;
580 /* FIXME: send "busy" response now, even without receiving request? */
581 close(fd);
582}
583
584#if defined(__linux__1) && defined(__x86_64__1)
585static bool_Bool read_cpu_topology(struct lwan *l, uint32_t siblings[])
586{
587 char path[PATH_MAX4096];
588
589 for (uint32_t i = 0; i < l->available_cpus; i++)
590 siblings[i] = 0xbebacafe;
591
592 for (unsigned int i = 0; i < l->available_cpus; i++) {
593 FILE *sib;
594 uint32_t id, sibling;
595 char separator;
596
597 snprintf(path, sizeof(path),
598 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
599 i);
600
601 sib = fopen(path, "re");
602 if (!sib) {
603 lwan_status_warning("Could not open `%s` to determine CPU topology",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 604, __FUNCTION__, "Could not open `%s` to determine CPU topology"
, path)
604 path)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 604, __FUNCTION__, "Could not open `%s` to determine CPU topology"
, path)
;
605 return false0;
606 }
607
608 switch (fscanf(sib, "%u%c%u", &id, &separator, &sibling)) {
609 case 2: /* No SMT */
610 siblings[i] = id;
611 break;
612 case 3: /* SMT */
613 if (!(separator == ',' || separator == '-')) {
614 lwan_status_critical("Expecting either ',' or '-' for sibling separator")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 614, __FUNCTION__, "Expecting either ',' or '-' for sibling separator"
)
;
615 __builtin_unreachable();
616 }
617
618 siblings[i] = sibling;
619 break;
620 default:
621 lwan_status_critical("%s has invalid format", path)lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 621, __FUNCTION__, "%s has invalid format", path)
;
622 __builtin_unreachable();
623 }
624
625 fclose(sib);
626 }
627
628 /* Perform a sanity check here, as some systems seem to filter out the
629 * result of sysconf() to obtain the number of configured and online
630 * CPUs but don't bother changing what's available through sysfs as far
631 * as the CPU topology information goes. It's better to fall back to a
632 * possibly non-optimal setup than just crash during startup while
633 * trying to perform an out-of-bounds array access. */
634 for (unsigned int i = 0; i < l->available_cpus; i++) {
635 if (siblings[i] == 0xbebacafe) {
636 lwan_status_warning("Could not determine sibling for CPU %d", i)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 636, __FUNCTION__, "Could not determine sibling for CPU %d"
, i)
;
637 return false0;
638 }
639
640 if (siblings[i] >= l->available_cpus) {
641 lwan_status_warning("CPU information topology says CPU %d exists, "lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 644, __FUNCTION__, "CPU information topology says CPU %d exists, "
"but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?"
, siblings[i], l->available_cpus, l->online_cpus)
642 "but max available CPUs is %d (online CPUs: %d). "lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 644, __FUNCTION__, "CPU information topology says CPU %d exists, "
"but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?"
, siblings[i], l->available_cpus, l->online_cpus)
643 "Is Lwan running in a (broken) container?",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 644, __FUNCTION__, "CPU information topology says CPU %d exists, "
"but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?"
, siblings[i], l->available_cpus, l->online_cpus)
644 siblings[i], l->available_cpus, l->online_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 644, __FUNCTION__, "CPU information topology says CPU %d exists, "
"but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?"
, siblings[i], l->available_cpus, l->online_cpus)
;
645 return false0;
646 }
647 }
648
649 return true1;
650}
651
652static void
653siblings_to_schedtbl(struct lwan *l, uint32_t siblings[], uint32_t schedtbl[])
654{
655 int *seen = alloca(l->available_cpus * sizeof(int))__builtin_alloca (l->available_cpus * sizeof(int));
656 unsigned int n_schedtbl = 0;
657
658 for (uint32_t i = 0; i < l->available_cpus; i++)
659 seen[i] = -1;
660
661 for (uint32_t i = 0; i < l->available_cpus; i++) {
662 if (seen[siblings[i]] < 0) {
663 seen[siblings[i]] = (int)i;
664 } else {
665 schedtbl[n_schedtbl++] = (uint32_t)seen[siblings[i]];
666 schedtbl[n_schedtbl++] = i;
667 }
668 }
669
670 if (n_schedtbl != l->available_cpus)
671 memcpy(schedtbl, seen, l->available_cpus * sizeof(int));
672}
673
674static bool_Bool
675topology_to_schedtbl(struct lwan *l, uint32_t schedtbl[], uint32_t n_threads)
676{
677 uint32_t *siblings = alloca(l->available_cpus * sizeof(uint32_t))__builtin_alloca (l->available_cpus * sizeof(uint32_t));
678
679 if (read_cpu_topology(l, siblings)) {
11
Assuming the condition is true
12
Taking true branch
680 uint32_t *affinity = alloca(l->available_cpus * sizeof(uint32_t))__builtin_alloca (l->available_cpus * sizeof(uint32_t));
681
682 siblings_to_schedtbl(l, siblings, affinity);
683
684 for (uint32_t i = 0; i < n_threads; i++)
13
Assuming 'i' is >= 'n_threads'
14
Loop condition is false. Execution continues on line 686
685 schedtbl[i] = affinity[i % l->available_cpus];
686 return true1;
687 }
688
689 for (uint32_t i = 0; i < n_threads; i++)
690 schedtbl[i] = (i / 2) % l->thread.count;
691 return false0;
692}
693
694static void
695adjust_threads_affinity(struct lwan *l, uint32_t *schedtbl, uint32_t mask)
696{
697 for (uint32_t i = 0; i < l->thread.count; i++) {
18
Assuming 'i' is < field 'count'
19
Loop condition is true. Entering loop body
698 cpu_set_t set;
699
700 CPU_ZERO(&set)do __builtin_memset (&set, '\0', sizeof (cpu_set_t)); while
(0)
;
20
Loop condition is false. Exiting loop
701 CPU_SET(schedtbl[i & mask], &set)(__extension__ ({ size_t __cpu = (schedtbl[i & mask]); __cpu
/ 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&set)
->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask
) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }))
;
21
Assigned value is garbage or undefined
702
703 if (pthread_setaffinity_np(l->thread.threads[i].self, sizeof(set),
704 &set))
705 lwan_status_warning("Could not set affinity for thread %d", i)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 705, __FUNCTION__, "Could not set affinity for thread %d", i
)
;
706 }
707}
708#elif defined(__x86_64__1)
709static bool_Bool
710topology_to_schedtbl(struct lwan *l, uint32_t schedtbl[], uint32_t n_threads)
711{
712 for (uint32_t i = 0; i < n_threads; i++)
713 schedtbl[i] = (i / 2) % l->thread.count;
714 return false0;
715}
716
717static void
718adjust_threads_affinity(struct lwan *l, uint32_t *schedtbl, uint32_t n)
719{
720}
721#endif
722
723void lwan_thread_init(struct lwan *l)
724{
725 if (pthread_barrier_init(&l->thread.barrier, NULL((void*)0),
1
Assuming the condition is false
2
Taking false branch
726 (unsigned)l->thread.count + 1))
727 lwan_status_critical("Could not create barrier")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 727, __FUNCTION__, "Could not create barrier")
;
728
729 lwan_status_debug("Initializing threads")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 729, __FUNCTION__, "Initializing threads")
;
730
731 l->thread.threads =
732 calloc((size_t)l->thread.count, sizeof(struct lwan_thread));
733 if (!l->thread.threads)
3
Assuming field 'threads' is non-null
4
Taking false branch
734 lwan_status_critical("Could not allocate memory for threads")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 734, __FUNCTION__, "Could not allocate memory for threads")
;
735
736 const size_t n_queue_fds = LWAN_MIN(l->thread.max_fd / l->thread.count,({ const __typeof__((l->thread.max_fd / l->thread.count
) + 0) lwan_tmp_id8 = (l->thread.max_fd / l->thread.count
); const __typeof__(((size_t)(2 * lwan_socket_get_backlog_size
())) + 0) lwan_tmp_id9 = ((size_t)(2 * lwan_socket_get_backlog_size
())); lwan_tmp_id8 > lwan_tmp_id9 ? lwan_tmp_id9 : lwan_tmp_id8
; })
5
Assuming 'lwan_tmp_id4' is <= 'lwan_tmp_id5'
6
'?' condition is false
737 (size_t)(2 * lwan_socket_get_backlog_size()))({ const __typeof__((l->thread.max_fd / l->thread.count
) + 0) lwan_tmp_id8 = (l->thread.max_fd / l->thread.count
); const __typeof__(((size_t)(2 * lwan_socket_get_backlog_size
())) + 0) lwan_tmp_id9 = ((size_t)(2 * lwan_socket_get_backlog_size
())); lwan_tmp_id8 > lwan_tmp_id9 ? lwan_tmp_id9 : lwan_tmp_id8
; })
;
738 lwan_status_debug("Pending client file descriptor queue has %zu items", n_queue_fds)lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 738, __FUNCTION__, "Pending client file descriptor queue has %zu items"
, n_queue_fds)
;
739 for (unsigned int i = 0; i
6.1
'i' is < field 'count'
< l->thread.count
; i++)
7
Loop condition is true. Entering loop body
8
Assuming 'i' is >= field 'count'
9
Loop condition is false. Execution continues on line 742
740 create_thread(l, &l->thread.threads[i], n_queue_fds);
741
742 const unsigned int total_conns = l->thread.max_fd * l->thread.count;
743#ifdef __x86_64__1
744 static_assert(sizeof(struct lwan_connection) == 32,extern int (*__Static_assert_function (void)) [!!sizeof (struct
{ int __error_if_negative: (sizeof(struct lwan_connection) ==
32) ? 2 : -1; })]
745 "Two connections per cache line")extern int (*__Static_assert_function (void)) [!!sizeof (struct
{ int __error_if_negative: (sizeof(struct lwan_connection) ==
32) ? 2 : -1; })]
;
746
747 lwan_status_debug("%d CPUs of %d are online. "lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 749, __FUNCTION__, "%d CPUs of %d are online. " "Reading topology to pre-schedule clients"
, l->online_cpus, l->available_cpus)
748 "Reading topology to pre-schedule clients",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 749, __FUNCTION__, "%d CPUs of %d are online. " "Reading topology to pre-schedule clients"
, l->online_cpus, l->available_cpus)
749 l->online_cpus, l->available_cpus)lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 749, __FUNCTION__, "%d CPUs of %d are online. " "Reading topology to pre-schedule clients"
, l->online_cpus, l->available_cpus)
;
750
751 /*
752 * Pre-schedule each file descriptor, to reduce some operations in the
753 * fast path.
754 *
755 * Since struct lwan_connection is guaranteed to be 32-byte long, two of
756 * them can fill up a cache line. Assume siblings share cache lines and
757 * use the CPU topology to group two connections per cache line in such
758 * a way that false sharing is avoided.
759 */
760 uint32_t n_threads = (uint32_t)lwan_nextpow2((size_t)((l->thread.count - 1) * 2));
761 uint32_t *schedtbl = alloca(n_threads * sizeof(uint32_t))__builtin_alloca (n_threads * sizeof(uint32_t));
762
763 bool_Bool adj_affinity = topology_to_schedtbl(l, schedtbl, n_threads);
10
Calling 'topology_to_schedtbl'
15
Returning from 'topology_to_schedtbl'
764
765 n_threads--; /* Transform count into mask for AND below */
766
767 if (adj_affinity
15.1
'adj_affinity' is true
)
16
Taking true branch
768 adjust_threads_affinity(l, schedtbl, n_threads);
17
Calling 'adjust_threads_affinity'
769
770 for (unsigned int i = 0; i < total_conns; i++)
771 l->conns[i].thread = &l->thread.threads[schedtbl[i & n_threads]];
772#else
773 for (unsigned int i = 0; i < total_conns; i++)
774 l->conns[i].thread = &l->thread.threads[i % l->thread.count];
775#endif
776
777 pthread_barrier_wait(&l->thread.barrier);
778
779 lwan_status_debug("Worker threads created and ready to serve")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 779, __FUNCTION__, "Worker threads created and ready to serve"
)
;
780}
781
782void lwan_thread_shutdown(struct lwan *l)
783{
784 lwan_status_debug("Shutting down threads")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c"
, 784, __FUNCTION__, "Shutting down threads")
;
785
786 for (unsigned int i = 0; i < l->thread.count; i++) {
787 struct lwan_thread *t = &l->thread.threads[i];
788
789 close(t->epoll_fd);
790 lwan_thread_nudge(t);
791 }
792
793 pthread_barrier_wait(&l->thread.barrier);
794 pthread_barrier_destroy(&l->thread.barrier);
795
796 for (unsigned int i = 0; i < l->thread.count; i++) {
797 struct lwan_thread *t = &l->thread.threads[i];
798
799 close(t->pipe_fd[0]);
800#if !defined(HAVE_EVENTFD)
801 close(t->pipe_fd[1]);
802#endif
803
804 pthread_join(l->thread.threads[i].self, NULL((void*)0));
805 spsc_queue_free(&t->pending_fds);
806 timeouts_close(t->wheel);
807 }
808
809 free(l->thread.threads);
810}