internal.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2019-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/configuration.h>
  10. #include <openssl/e_os2.h>
  11. #include <openssl/types.h>
  12. #include <openssl/crypto.h>
  13. #include <internal/thread.h>
  14. #include <internal/thread_arch.h>
  15. #if !defined(OPENSSL_NO_DEFAULT_THREAD_POOL)
  16. static ossl_inline uint64_t _ossl_get_avail_threads(OSSL_LIB_CTX_THREADS *tdata)
  17. {
  18. /* assumes that tdata->lock is taken */
  19. return tdata->max_threads - tdata->active_threads;
  20. }
  21. uint64_t ossl_get_avail_threads(OSSL_LIB_CTX *ctx)
  22. {
  23. uint64_t retval = 0;
  24. OSSL_LIB_CTX_THREADS *tdata = OSSL_LIB_CTX_GET_THREADS(ctx);
  25. if (tdata == NULL)
  26. return retval;
  27. ossl_crypto_mutex_lock(tdata->lock);
  28. retval = _ossl_get_avail_threads(tdata);
  29. ossl_crypto_mutex_unlock(tdata->lock);
  30. return retval;
  31. }
  32. void *ossl_crypto_thread_start(OSSL_LIB_CTX *ctx, CRYPTO_THREAD_ROUTINE start,
  33. void *data)
  34. {
  35. CRYPTO_THREAD *thread;
  36. OSSL_LIB_CTX_THREADS *tdata = OSSL_LIB_CTX_GET_THREADS(ctx);
  37. if (tdata == NULL)
  38. return NULL;
  39. ossl_crypto_mutex_lock(tdata->lock);
  40. if (tdata == NULL || tdata->max_threads == 0) {
  41. ossl_crypto_mutex_unlock(tdata->lock);
  42. return NULL;
  43. }
  44. while (_ossl_get_avail_threads(tdata) == 0)
  45. ossl_crypto_condvar_wait(tdata->cond_finished, tdata->lock);
  46. tdata->active_threads++;
  47. ossl_crypto_mutex_unlock(tdata->lock);
  48. thread = ossl_crypto_thread_native_start(start, data, 1);
  49. if (thread == NULL) {
  50. ossl_crypto_mutex_lock(tdata->lock);
  51. tdata->active_threads--;
  52. ossl_crypto_mutex_unlock(tdata->lock);
  53. goto fail;
  54. }
  55. thread->ctx = ctx;
  56. fail:
  57. return (void *) thread;
  58. }
  59. int ossl_crypto_thread_join(void *vhandle, CRYPTO_THREAD_RETVAL *retval)
  60. {
  61. CRYPTO_THREAD *handle = vhandle;
  62. OSSL_LIB_CTX_THREADS *tdata;
  63. if (vhandle == NULL)
  64. return 0;
  65. tdata = OSSL_LIB_CTX_GET_THREADS(handle->ctx);
  66. if (tdata == NULL)
  67. return 0;
  68. if (ossl_crypto_thread_native_join(handle, retval) == 0)
  69. return 0;
  70. ossl_crypto_mutex_lock(tdata->lock);
  71. tdata->active_threads--;
  72. ossl_crypto_condvar_signal(tdata->cond_finished);
  73. ossl_crypto_mutex_unlock(tdata->lock);
  74. return 1;
  75. }
  76. int ossl_crypto_thread_clean(void *vhandle)
  77. {
  78. CRYPTO_THREAD *handle = vhandle;
  79. return ossl_crypto_thread_native_clean(handle);
  80. }
  81. #else
  82. ossl_inline uint64_t ossl_get_avail_threads(OSSL_LIB_CTX *ctx)
  83. {
  84. return 0;
  85. }
  86. void *ossl_crypto_thread_start(OSSL_LIB_CTX *ctx, CRYPTO_THREAD_ROUTINE start,
  87. void *data)
  88. {
  89. return NULL;
  90. }
  91. int ossl_crypto_thread_join(void *vhandle, CRYPTO_THREAD_RETVAL *retval)
  92. {
  93. return 0;
  94. }
  95. int ossl_crypto_thread_clean(void *vhandle)
  96. {
  97. return 0;
  98. }
  99. #endif
  100. void *ossl_threads_ctx_new(OSSL_LIB_CTX *ctx)
  101. {
  102. struct openssl_threads_st *t = OPENSSL_zalloc(sizeof(*t));
  103. if (t == NULL)
  104. return NULL;
  105. t->lock = ossl_crypto_mutex_new();
  106. t->cond_finished = ossl_crypto_condvar_new();
  107. if (t->lock == NULL || t->cond_finished == NULL)
  108. goto fail;
  109. return t;
  110. fail:
  111. ossl_threads_ctx_free((void *)t);
  112. return NULL;
  113. }
  114. void ossl_threads_ctx_free(void *vdata)
  115. {
  116. OSSL_LIB_CTX_THREADS *t = (OSSL_LIB_CTX_THREADS *) vdata;
  117. if (t == NULL)
  118. return;
  119. ossl_crypto_mutex_free(&t->lock);
  120. ossl_crypto_condvar_free(&t->cond_finished);
  121. OPENSSL_free(t);
  122. }