2
0

quic_thread_assist.c 4.0 KB

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