quic_thread_assist.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #ifndef OSSL_QUIC_THREAD_ASSIST_H
  10. # define OSSL_QUIC_THREAD_ASSIST_H
  11. # include <openssl/ssl.h>
  12. # include "internal/thread.h"
  13. # include "internal/time.h"
  14. # if defined(OPENSSL_NO_QUIC) || defined(OPENSSL_NO_THREAD_POOL)
  15. # define OPENSSL_NO_QUIC_THREAD_ASSIST
  16. # endif
  17. # ifndef OPENSSL_NO_QUIC_THREAD_ASSIST
  18. /*
  19. * QUIC Thread Assisted Functionality
  20. * ==================================
  21. *
  22. * Where OS threading support is available, QUIC can optionally support a thread
  23. * assisted mode of operation. The purpose of this mode of operation is to
  24. * ensure that assorted timeout events which QUIC expects to be handled in a
  25. * timely manner can be handled without the application needing to ensure that
  26. * SSL_tick() is called on time. This is not needed if the application always
  27. * has a call blocking to SSL_read() or SSL_write() (or another I/O function) on
  28. * a QUIC SSL object, but if the application goes for long periods of time
  29. * without making any such call to a QUIC SSL object, libssl cannot ordinarily
  30. * guarantee that QUIC timeout events will be serviced in a timely fashion.
  31. * Thread assisted mode is therefore of use to applications which do not always
  32. * have an ongoing call to an I/O function on a QUIC SSL object but also do not
  33. * want to have to arrange periodic ticking.
  34. *
  35. * A consequence of this is that the intrusiveness of thread assisted mode upon
  36. * the general architecture of our QUIC engine is actually fairly limited and
  37. * amounts to an automatic ticking of the QUIC engine when timeouts expire,
  38. * synchronised correctly with an application's own threads using locking.
  39. */
  40. typedef struct quic_thread_assist_st {
  41. QUIC_CHANNEL *ch;
  42. CRYPTO_CONDVAR *cv;
  43. CRYPTO_THREAD *t;
  44. int teardown, joined;
  45. OSSL_TIME (*now_cb)(void *arg);
  46. void *now_cb_arg;
  47. } QUIC_THREAD_ASSIST;
  48. /*
  49. * Initialise the thread assist object. The channel must have a valid mutex
  50. * configured on it which will be retrieved automatically. It is assumed that
  51. * the mutex is currently held when this function is called. This function does
  52. * not affect the state of the mutex.
  53. */
  54. int ossl_quic_thread_assist_init_start(QUIC_THREAD_ASSIST *qta,
  55. QUIC_CHANNEL *ch,
  56. OSSL_TIME (*now_cb)(void *arg),
  57. void *now_cb_arg);
  58. /*
  59. * Request the thread assist helper to begin stopping the assist thread. This
  60. * returns before the teardown is complete. Idempotent; multiple calls to this
  61. * function are inconsequential.
  62. *
  63. * Precondition: channel mutex must be held (unchecked)
  64. */
  65. int ossl_quic_thread_assist_stop_async(QUIC_THREAD_ASSIST *qta);
  66. /*
  67. * Wait until the thread assist helper is torn down. This automatically implies
  68. * the effects of ossl_quic_thread_assist_stop_async(). Returns immediately
  69. * if the teardown has already completed.
  70. *
  71. * Precondition: channel mutex must be held (unchecked)
  72. */
  73. int ossl_quic_thread_assist_wait_stopped(QUIC_THREAD_ASSIST *qta);
  74. /*
  75. * Deallocates state associated with the thread assist helper.
  76. * ossl_quic_thread_assist_wait_stopped() must have returned successfully before
  77. * calling this. It does not matter whether the channel mutex is held or not.
  78. *
  79. * Precondition: ossl_quic_thread_assist_wait_stopped() has returned 1
  80. * (asserted)
  81. */
  82. int ossl_quic_thread_assist_cleanup(QUIC_THREAD_ASSIST *qta);
  83. /*
  84. * Must be called to notify the assist thread if the channel deadline changes.
  85. *
  86. * Precondition: channel mutex must be held (unchecked)
  87. */
  88. int ossl_quic_thread_assist_notify_deadline_changed(QUIC_THREAD_ASSIST *qta);
  89. # endif
  90. #endif