Path: blob/main/crypto/openssl/ssl/quic/quic_thread_assist.c
48261 views
/*1* Copyright 2023-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <openssl/macros.h>10#include "quic_local.h"11#include "internal/time.h"12#include "internal/thread.h"13#include "internal/thread_arch.h"14#include "internal/quic_thread_assist.h"1516#if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)1718/* Main loop for the QUIC assist thread. */19static unsigned int assist_thread_main(void *arg)20{21QUIC_THREAD_ASSIST *qta = arg;22CRYPTO_MUTEX *m = ossl_quic_channel_get_mutex(qta->ch);23QUIC_REACTOR *rtor;24QUIC_ENGINE *eng = ossl_quic_channel_get0_engine(qta->ch);2526ossl_crypto_mutex_lock(m);2728rtor = ossl_quic_channel_get_reactor(qta->ch);2930for (;;) {31OSSL_TIME deadline;3233if (qta->teardown)34break;3536deadline = ossl_quic_reactor_get_tick_deadline(rtor);37/*38* ossl_crypto_condvar_wait_timeout needs to use real time for the39* deadline40*/41deadline = ossl_quic_engine_make_real_time(eng, deadline);42ossl_crypto_condvar_wait_timeout(qta->cv, m, deadline);4344/*45* We have now been woken up. This can be for one of the following46* reasons:47*48* - We have been asked to teardown (qta->teardown is set);49* - The tick deadline has passed.50* - The tick deadline has changed.51*52* For robustness, this loop also handles spurious wakeups correctly53* (which does not require any extra code).54*/55if (qta->teardown)56break;5758ossl_quic_reactor_tick(rtor, QUIC_REACTOR_TICK_FLAG_CHANNEL_ONLY);59}6061ossl_crypto_mutex_unlock(m);62return 1;63}6465int ossl_quic_thread_assist_init_start(QUIC_THREAD_ASSIST *qta,66QUIC_CHANNEL *ch)67{68CRYPTO_MUTEX *mutex = ossl_quic_channel_get_mutex(ch);6970if (mutex == NULL)71return 0;7273qta->ch = ch;74qta->teardown = 0;75qta->joined = 0;7677qta->cv = ossl_crypto_condvar_new();78if (qta->cv == NULL)79return 0;8081qta->t = ossl_crypto_thread_native_start(assist_thread_main,82qta, /*joinable=*/1);83if (qta->t == NULL) {84ossl_crypto_condvar_free(&qta->cv);85return 0;86}8788return 1;89}9091int ossl_quic_thread_assist_stop_async(QUIC_THREAD_ASSIST *qta)92{93if (!qta->teardown) {94qta->teardown = 1;95ossl_crypto_condvar_signal(qta->cv);96}9798return 1;99}100101int ossl_quic_thread_assist_wait_stopped(QUIC_THREAD_ASSIST *qta)102{103CRYPTO_THREAD_RETVAL rv;104CRYPTO_MUTEX *m = ossl_quic_channel_get_mutex(qta->ch);105106if (qta->joined)107return 1;108109if (!ossl_quic_thread_assist_stop_async(qta))110return 0;111112ossl_crypto_mutex_unlock(m);113114if (!ossl_crypto_thread_native_join(qta->t, &rv)) {115ossl_crypto_mutex_lock(m);116return 0;117}118119qta->joined = 1;120121ossl_crypto_mutex_lock(m);122return 1;123}124125int ossl_quic_thread_assist_cleanup(QUIC_THREAD_ASSIST *qta)126{127if (!ossl_assert(qta->joined))128return 0;129130ossl_crypto_condvar_free(&qta->cv);131ossl_crypto_thread_native_clean(qta->t);132133qta->ch = NULL;134qta->t = NULL;135return 1;136}137138int ossl_quic_thread_assist_notify_deadline_changed(QUIC_THREAD_ASSIST *qta)139{140if (qta->teardown)141return 0;142143ossl_crypto_condvar_signal(qta->cv);144return 1;145}146147#endif148149150