QUIC Thread Assisted Mode: Support Windows XP
[openssl.git] / ssl / quic / quic_thread_assist.c
1 /*
2  * Copyright 2023 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) && !defined(OPENSSL_NO_THREAD_POOL)
18
19 /* Main loop for the QUIC assist thread. */
20 static unsigned int assist_thread_main(void *arg)
21 {
22     QUIC_THREAD_ASSIST *qta = arg;
23     CRYPTO_MUTEX *m = ossl_quic_channel_get_mutex(qta->ch);
24     QUIC_REACTOR *rtor;
25
26     ossl_crypto_mutex_lock(m);
27
28     rtor = ossl_quic_channel_get_reactor(qta->ch);
29
30     for (;;) {
31         if (qta->teardown)
32             break;
33
34         ossl_crypto_condvar_wait_timeout(qta->cv, m,
35                                          ossl_quic_reactor_get_tick_deadline(rtor));
36
37         /*
38          * We have now been woken up. This can be for one of the following
39          * reasons:
40          *
41          *   - We have been asked to teardown (qta->teardown is set);
42          *   - The tick deadline has passed.
43          *   - The tick deadline has changed.
44          *
45          * For robustness, this loop also handles spurious wakeups correctly
46          * (which does not require any extra code).
47          */
48         if (qta->teardown)
49             break;
50
51         ossl_quic_reactor_tick(rtor, QUIC_REACTOR_TICK_FLAG_CHANNEL_ONLY);
52     }
53
54     ossl_crypto_mutex_unlock(m);
55     return 1;
56 }
57
58 int ossl_quic_thread_assist_init_start(QUIC_THREAD_ASSIST *qta,
59                                        QUIC_CHANNEL *ch)
60 {
61     CRYPTO_MUTEX *mutex = ossl_quic_channel_get_mutex(ch);
62
63     if (mutex == NULL)
64         return 0;
65
66     qta->ch         = ch;
67     qta->teardown   = 0;
68     qta->joined     = 0;
69
70     qta->cv = ossl_crypto_condvar_new();
71     if (qta->cv == NULL)
72         return 0;
73
74     qta->t = ossl_crypto_thread_native_start(assist_thread_main,
75                                              qta, /*joinable=*/1);
76     if (qta->t == NULL) {
77         ossl_crypto_condvar_free(qta->cv);
78         return 0;
79     }
80
81     return 1;
82 }
83
84 int ossl_quic_thread_assist_stop_async(QUIC_THREAD_ASSIST *qta)
85 {
86     if (!qta->teardown) {
87         qta->teardown = 1;
88         ossl_crypto_condvar_signal(qta->cv);
89     }
90
91     return 1;
92 }
93
94 int ossl_quic_thread_assist_wait_stopped(QUIC_THREAD_ASSIST *qta)
95 {
96     CRYPTO_THREAD_RETVAL rv;
97     CRYPTO_MUTEX *m = ossl_quic_channel_get_mutex(qta->ch);
98
99     if (qta->joined)
100         return 1;
101
102     if (!ossl_quic_thread_assist_stop_async(qta))
103         return 0;
104
105     ossl_crypto_mutex_unlock(m);
106
107     if (!ossl_crypto_thread_native_join(qta->t, &rv)) {
108         ossl_crypto_mutex_lock(m);
109         return 0;
110     }
111
112     qta->joined = 1;
113
114     ossl_crypto_mutex_lock(m);
115     return 1;
116 }
117
118 int ossl_quic_thread_assist_cleanup(QUIC_THREAD_ASSIST *qta)
119 {
120     if (!ossl_assert(qta->joined))
121         return 0;
122
123     ossl_crypto_condvar_free(&qta->cv);
124     ossl_crypto_thread_native_clean(qta->t);
125
126     qta->ch     = NULL;
127     qta->t      = NULL;
128     return 1;
129 }
130
131 int ossl_quic_thread_assist_notify_deadline_changed(QUIC_THREAD_ASSIST *qta)
132 {
133     if (qta->teardown)
134         return 0;
135
136     ossl_crypto_condvar_signal(qta->cv);
137     return 1;
138 }
139
140 #endif