2023-02-21 18:18:59 +08:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2023-06-30 23:34:48 +08:00
|
|
|
#if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
|
2023-02-21 18:18:59 +08:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2023-02-21 18:18:59 +08:00
|
|
|
ossl_crypto_mutex_lock(m);
|
2023-02-21 18:18:59 +08:00
|
|
|
|
|
|
|
rtor = ossl_quic_channel_get_reactor(qta->ch);
|
|
|
|
|
|
|
|
for (;;) {
|
2023-10-06 22:56:15 +08:00
|
|
|
OSSL_TIME deadline;
|
|
|
|
|
2023-02-21 18:18:59 +08:00
|
|
|
if (qta->teardown)
|
|
|
|
break;
|
|
|
|
|
2023-10-06 22:56:15 +08:00
|
|
|
deadline = ossl_quic_reactor_get_tick_deadline(rtor);
|
|
|
|
if (qta->now_cb != NULL
|
|
|
|
&& !ossl_time_is_zero(deadline)
|
|
|
|
&& !ossl_time_is_infinite(deadline)) {
|
|
|
|
/*
|
|
|
|
* ossl_crypto_condvar_wait_timeout needs to use real time for the
|
|
|
|
* deadline
|
|
|
|
*/
|
|
|
|
deadline = ossl_time_add(ossl_time_subtract(deadline,
|
|
|
|
qta->now_cb(qta->now_cb_arg)),
|
|
|
|
ossl_time_now());
|
|
|
|
}
|
|
|
|
ossl_crypto_condvar_wait_timeout(qta->cv, m, deadline);
|
2023-02-21 18:18:59 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2023-02-21 18:18:59 +08:00
|
|
|
ossl_quic_reactor_tick(rtor, QUIC_REACTOR_TICK_FLAG_CHANNEL_ONLY);
|
2023-02-21 18:18:59 +08:00
|
|
|
}
|
|
|
|
|
2023-02-21 18:18:59 +08:00
|
|
|
ossl_crypto_mutex_unlock(m);
|
2023-02-21 18:18:59 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ossl_quic_thread_assist_init_start(QUIC_THREAD_ASSIST *qta,
|
2023-10-06 22:56:15 +08:00
|
|
|
QUIC_CHANNEL *ch,
|
|
|
|
OSSL_TIME (*now_cb)(void *arg),
|
|
|
|
void *now_cb_arg)
|
2023-02-21 18:18:59 +08:00
|
|
|
{
|
|
|
|
CRYPTO_MUTEX *mutex = ossl_quic_channel_get_mutex(ch);
|
|
|
|
|
|
|
|
if (mutex == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
qta->ch = ch;
|
|
|
|
qta->teardown = 0;
|
|
|
|
qta->joined = 0;
|
2023-10-06 22:56:15 +08:00
|
|
|
qta->now_cb = now_cb;
|
|
|
|
qta->now_cb_arg = now_cb_arg;
|
2023-02-21 18:18:59 +08:00
|
|
|
|
|
|
|
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) {
|
2024-02-08 18:27:56 +08:00
|
|
|
ossl_crypto_condvar_free(&qta->cv);
|
2023-02-21 18:18:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ossl_quic_thread_assist_stop_async(QUIC_THREAD_ASSIST *qta)
|
|
|
|
{
|
|
|
|
if (!qta->teardown) {
|
|
|
|
qta->teardown = 1;
|
2023-02-23 03:15:16 +08:00
|
|
|
ossl_crypto_condvar_signal(qta->cv);
|
2023-02-21 18:18:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ossl_quic_thread_assist_wait_stopped(QUIC_THREAD_ASSIST *qta)
|
|
|
|
{
|
|
|
|
CRYPTO_THREAD_RETVAL rv;
|
2023-02-21 18:18:59 +08:00
|
|
|
CRYPTO_MUTEX *m = ossl_quic_channel_get_mutex(qta->ch);
|
2023-02-21 18:18:59 +08:00
|
|
|
|
|
|
|
if (qta->joined)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!ossl_quic_thread_assist_stop_async(qta))
|
|
|
|
return 0;
|
|
|
|
|
2023-02-21 18:18:59 +08:00
|
|
|
ossl_crypto_mutex_unlock(m);
|
2023-02-21 18:18:59 +08:00
|
|
|
|
|
|
|
if (!ossl_crypto_thread_native_join(qta->t, &rv)) {
|
2023-02-21 18:18:59 +08:00
|
|
|
ossl_crypto_mutex_lock(m);
|
2023-02-21 18:18:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
qta->joined = 1;
|
|
|
|
|
2023-02-21 18:18:59 +08:00
|
|
|
ossl_crypto_mutex_lock(m);
|
2023-02-21 18:18:59 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ossl_quic_thread_assist_cleanup(QUIC_THREAD_ASSIST *qta)
|
|
|
|
{
|
|
|
|
if (!ossl_assert(qta->joined))
|
|
|
|
return 0;
|
|
|
|
|
2023-02-23 00:08:26 +08:00
|
|
|
ossl_crypto_condvar_free(&qta->cv);
|
2023-02-21 18:18:59 +08:00
|
|
|
ossl_crypto_thread_native_clean(qta->t);
|
|
|
|
|
|
|
|
qta->ch = NULL;
|
|
|
|
qta->t = NULL;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ossl_quic_thread_assist_notify_deadline_changed(QUIC_THREAD_ASSIST *qta)
|
|
|
|
{
|
|
|
|
if (qta->teardown)
|
|
|
|
return 0;
|
|
|
|
|
2023-02-23 03:15:16 +08:00
|
|
|
ossl_crypto_condvar_signal(qta->cv);
|
2023-02-21 18:18:59 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|