/src/openssl32/ssl/quic/quic_thread_assist.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2023-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <openssl/macros.h> |
11 | | #include "quic_local.h" |
12 | | #include "internal/time.h" |
13 | | #include "internal/thread.h" |
14 | | #include "internal/thread_arch.h" |
15 | | #include "internal/quic_thread_assist.h" |
16 | | |
17 | | #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST) |
18 | | |
19 | | /* Main loop for the QUIC assist thread. */ |
20 | | static unsigned int assist_thread_main(void *arg) |
21 | 0 | { |
22 | 0 | QUIC_THREAD_ASSIST *qta = arg; |
23 | 0 | CRYPTO_MUTEX *m = ossl_quic_channel_get_mutex(qta->ch); |
24 | 0 | QUIC_REACTOR *rtor; |
25 | |
|
26 | 0 | ossl_crypto_mutex_lock(m); |
27 | |
|
28 | 0 | rtor = ossl_quic_channel_get_reactor(qta->ch); |
29 | |
|
30 | 0 | for (;;) { |
31 | 0 | OSSL_TIME deadline; |
32 | |
|
33 | 0 | if (qta->teardown) |
34 | 0 | break; |
35 | | |
36 | 0 | deadline = ossl_quic_reactor_get_tick_deadline(rtor); |
37 | 0 | if (qta->now_cb != NULL |
38 | 0 | && !ossl_time_is_zero(deadline) |
39 | 0 | && !ossl_time_is_infinite(deadline)) { |
40 | | /* |
41 | | * ossl_crypto_condvar_wait_timeout needs to use real time for the |
42 | | * deadline |
43 | | */ |
44 | 0 | deadline = ossl_time_add(ossl_time_subtract(deadline, |
45 | 0 | qta->now_cb(qta->now_cb_arg)), |
46 | 0 | ossl_time_now()); |
47 | 0 | } |
48 | 0 | ossl_crypto_condvar_wait_timeout(qta->cv, m, deadline); |
49 | | |
50 | | /* |
51 | | * We have now been woken up. This can be for one of the following |
52 | | * reasons: |
53 | | * |
54 | | * - We have been asked to teardown (qta->teardown is set); |
55 | | * - The tick deadline has passed. |
56 | | * - The tick deadline has changed. |
57 | | * |
58 | | * For robustness, this loop also handles spurious wakeups correctly |
59 | | * (which does not require any extra code). |
60 | | */ |
61 | 0 | if (qta->teardown) |
62 | 0 | break; |
63 | | |
64 | 0 | ossl_quic_reactor_tick(rtor, QUIC_REACTOR_TICK_FLAG_CHANNEL_ONLY); |
65 | 0 | } |
66 | |
|
67 | 0 | ossl_crypto_mutex_unlock(m); |
68 | 0 | return 1; |
69 | 0 | } |
70 | | |
71 | | int ossl_quic_thread_assist_init_start(QUIC_THREAD_ASSIST *qta, |
72 | | QUIC_CHANNEL *ch, |
73 | | OSSL_TIME (*now_cb)(void *arg), |
74 | | void *now_cb_arg) |
75 | 0 | { |
76 | 0 | CRYPTO_MUTEX *mutex = ossl_quic_channel_get_mutex(ch); |
77 | |
|
78 | 0 | if (mutex == NULL) |
79 | 0 | return 0; |
80 | | |
81 | 0 | qta->ch = ch; |
82 | 0 | qta->teardown = 0; |
83 | 0 | qta->joined = 0; |
84 | 0 | qta->now_cb = now_cb; |
85 | 0 | qta->now_cb_arg = now_cb_arg; |
86 | |
|
87 | 0 | qta->cv = ossl_crypto_condvar_new(); |
88 | 0 | if (qta->cv == NULL) |
89 | 0 | return 0; |
90 | | |
91 | 0 | qta->t = ossl_crypto_thread_native_start(assist_thread_main, |
92 | 0 | qta, /*joinable=*/1); |
93 | 0 | if (qta->t == NULL) { |
94 | 0 | ossl_crypto_condvar_free(&qta->cv); |
95 | 0 | return 0; |
96 | 0 | } |
97 | | |
98 | 0 | return 1; |
99 | 0 | } |
100 | | |
101 | | int ossl_quic_thread_assist_stop_async(QUIC_THREAD_ASSIST *qta) |
102 | 0 | { |
103 | 0 | if (!qta->teardown) { |
104 | 0 | qta->teardown = 1; |
105 | 0 | ossl_crypto_condvar_signal(qta->cv); |
106 | 0 | } |
107 | |
|
108 | 0 | return 1; |
109 | 0 | } |
110 | | |
111 | | int ossl_quic_thread_assist_wait_stopped(QUIC_THREAD_ASSIST *qta) |
112 | 0 | { |
113 | 0 | CRYPTO_THREAD_RETVAL rv; |
114 | 0 | CRYPTO_MUTEX *m = ossl_quic_channel_get_mutex(qta->ch); |
115 | |
|
116 | 0 | if (qta->joined) |
117 | 0 | return 1; |
118 | | |
119 | 0 | if (!ossl_quic_thread_assist_stop_async(qta)) |
120 | 0 | return 0; |
121 | | |
122 | 0 | ossl_crypto_mutex_unlock(m); |
123 | |
|
124 | 0 | if (!ossl_crypto_thread_native_join(qta->t, &rv)) { |
125 | 0 | ossl_crypto_mutex_lock(m); |
126 | 0 | return 0; |
127 | 0 | } |
128 | | |
129 | 0 | qta->joined = 1; |
130 | |
|
131 | 0 | ossl_crypto_mutex_lock(m); |
132 | 0 | return 1; |
133 | 0 | } |
134 | | |
135 | | int ossl_quic_thread_assist_cleanup(QUIC_THREAD_ASSIST *qta) |
136 | 0 | { |
137 | 0 | if (!ossl_assert(qta->joined)) |
138 | 0 | return 0; |
139 | | |
140 | 0 | ossl_crypto_condvar_free(&qta->cv); |
141 | 0 | ossl_crypto_thread_native_clean(qta->t); |
142 | |
|
143 | 0 | qta->ch = NULL; |
144 | 0 | qta->t = NULL; |
145 | 0 | return 1; |
146 | 0 | } |
147 | | |
148 | | int ossl_quic_thread_assist_notify_deadline_changed(QUIC_THREAD_ASSIST *qta) |
149 | 0 | { |
150 | 0 | if (qta->teardown) |
151 | 0 | return 0; |
152 | | |
153 | 0 | ossl_crypto_condvar_signal(qta->cv); |
154 | 0 | return 1; |
155 | 0 | } |
156 | | |
157 | | #endif |