summaryrefslogtreecommitdiff
path: root/ssl
diff options
context:
space:
mode:
authorHugo Landau <hlandau@openssl.org>2023-02-21 10:18:59 +0000
committerHugo Landau <hlandau@openssl.org>2023-03-30 11:14:08 +0100
commit9f7acf071c363ed8cb5012e122e1e60447b45c78 (patch)
tree1b5c2a20cd4263b6b23bb8bec5c3ff0790e27192 /ssl
parent4a530180e5b9921bc3d1b5228d9be96f2a0b4b07 (diff)
downloadopenssl-new-9f7acf071c363ed8cb5012e122e1e60447b45c78.tar.gz
QUIC Thread Assist Core
Reviewed-by: Tomas Mraz <tomas@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> (Merged from https://github.com/openssl/openssl/pull/20348)
Diffstat (limited to 'ssl')
-rw-r--r--ssl/build.info9
-rw-r--r--ssl/quic/build.info1
-rw-r--r--ssl/quic/quic_thread_assist.c150
3 files changed, 160 insertions, 0 deletions
diff --git a/ssl/build.info b/ssl/build.info
index 00e9e8fd7d..4b43a4ce01 100644
--- a/ssl/build.info
+++ b/ssl/build.info
@@ -33,4 +33,13 @@ ENDIF
IF[{- !$disabled{quic} -}]
SOURCE[../libssl]=priority_queue.c event_queue.c
+
+ IF[{- !$disabled{'thread-pool'} -}]
+ SHARED_SOURCE[../libssl]=\
+ ../crypto/thread/arch/thread_posix.c \
+ ../crypto/thread/arch/thread_win.c \
+ ../crypto/thread/arch/thread_none.c \
+ ../crypto/thread/arch.c
+ ENDIF
+
ENDIF
diff --git a/ssl/quic/build.info b/ssl/quic/build.info
index 79aa00dbba..edd2c76d1e 100644
--- a/ssl/quic/build.info
+++ b/ssl/quic/build.info
@@ -12,3 +12,4 @@ SOURCE[$LIBSSL]=quic_reactor.c
SOURCE[$LIBSSL]=quic_channel.c
SOURCE[$LIBSSL]=quic_tserver.c
SOURCE[$LIBSSL]=quic_tls.c
+SOURCE[$LIBSSL]=quic_thread_assist.c
diff --git a/ssl/quic/quic_thread_assist.c b/ssl/quic/quic_thread_assist.c
new file mode 100644
index 0000000000..6b2c219982
--- /dev/null
+++ b/ssl/quic/quic_thread_assist.c
@@ -0,0 +1,150 @@
+/*
+ * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License"). You may not use
+ * this file except in compliance with the License. You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <openssl/macros.h>
+#include "quic_local.h"
+#include "internal/time.h"
+#include "internal/thread.h"
+#include "internal/thread_arch.h"
+#include "internal/quic_thread_assist.h"
+
+#if !defined(OPENSSL_NO_QUIC) && !defined(OPENSSL_NO_THREAD_POOL)
+
+/* Main loop for the QUIC assist thread. */
+static unsigned int assist_thread_main(void *arg)
+{
+ QUIC_THREAD_ASSIST *qta = arg;
+ CRYPTO_MUTEX *m = ossl_quic_channel_get_mutex(qta->ch);
+ QUIC_REACTOR *rtor;
+
+ if (!CRYPTO_THREAD_write_lock(m))
+ return 0;
+
+ rtor = ossl_quic_channel_get_reactor(qta->ch);
+
+ for (;;) {
+ if (qta->teardown)
+ break;
+
+ ossl_crypto_condvar_wait_timeout(qta->cv, m,
+ ossl_quic_reactor_get_tick_deadline(rtor));
+
+ /*
+ * We have now been woken up. This can be for one of the following
+ * reasons:
+ *
+ * - We have been asked to teardown (qta->teardown is set);
+ * - The tick deadline has passed.
+ * - The tick deadline has changed.
+ *
+ * For robustness, this loop also handles spurious wakeups correctly
+ * (which does not require any extra code).
+ */
+ if (qta->teardown)
+ break;
+
+ ossl_quic_reactor_tick(rtor);
+ }
+
+ CRYPTO_THREAD_unlock(m);
+ return 1;
+}
+
+int ossl_quic_thread_assist_init_start(QUIC_THREAD_ASSIST *qta,
+ QUIC_CHANNEL *ch)
+{
+ CRYPTO_MUTEX *mutex = ossl_quic_channel_get_mutex(ch);
+
+ if (mutex == NULL)
+ return 0;
+
+ qta->ch = ch;
+ qta->teardown = 0;
+ qta->joined = 0;
+
+ qta->cv = ossl_crypto_condvar_new();
+ if (qta->cv == NULL)
+ return 0;
+
+ qta->t = ossl_crypto_thread_native_start(assist_thread_main,
+ qta, /*joinable=*/1);
+ if (qta->t == NULL) {
+ ossl_crypto_condvar_free(qta->cv);
+ return 0;
+ }
+
+ return 1;
+}
+
+int ossl_quic_thread_assist_stop_async(QUIC_THREAD_ASSIST *qta)
+{
+ if (!qta->teardown) {
+ qta->teardown = 1;
+ ossl_crypto_condvar_broadcast(qta->cv);
+ }
+
+ return 1;
+}
+
+static void ignore_res(int x) {}
+
+int ossl_quic_thread_assist_wait_stopped(QUIC_THREAD_ASSIST *qta)
+{
+ CRYPTO_THREAD_RETVAL rv;
+ CRYPTO_RWLOCK *m = ossl_quic_channel_get_mutex(qta->ch);
+
+ if (qta->joined)
+ return 1;
+
+ if (!ossl_quic_thread_assist_stop_async(qta))
+ return 0;
+
+ CRYPTO_THREAD_unlock(m);
+
+ if (!ossl_crypto_thread_native_join(qta->t, &rv)) {
+ ignore_res(CRYPTO_THREAD_write_lock(m)); /* best effort */
+ return 0;
+ }
+
+ qta->joined = 1;
+
+ if (!CRYPTO_THREAD_write_lock(m))
+ return 0;
+
+ return 1;
+}
+
+int ossl_quic_thread_assist_cleanup(QUIC_THREAD_ASSIST *qta)
+{
+ if (!ossl_assert(qta->joined))
+ return 0;
+
+ ossl_crypto_condvar_free(qta->cv);
+ ossl_crypto_thread_native_clean(qta->t);
+
+ qta->ch = NULL;
+ qta->cv = NULL;
+ qta->t = NULL;
+ return 1;
+}
+
+int ossl_quic_thread_assist_notify_deadline_changed(QUIC_THREAD_ASSIST *qta)
+{
+ if (qta->teardown)
+ return 0;
+
+ /*
+ * Wake-one would be better here but as there is only one listening thread
+ * this does not actually matter.
+ */
+ ossl_crypto_condvar_broadcast(qta->cv);
+ return 1;
+}
+
+#endif