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