crypto: thread: remove ossl_crypto_thread_native_terminate

Signed-off-by: Čestmír Kalina <ckalina@redhat.com>

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19473)
This commit is contained in:
Čestmír Kalina 2022-10-21 19:49:21 +02:00
parent 3a09dfb4f9
commit 4f32754f79
6 changed files with 2 additions and 136 deletions

View File

@ -54,16 +54,10 @@ int ossl_crypto_thread_native_join(CRYPTO_THREAD *thread, CRYPTO_THREAD_RETVAL *
return 0;
ossl_crypto_mutex_lock(thread->statelock);
req_state_mask = CRYPTO_THREAD_TERMINATED | CRYPTO_THREAD_FINISHED \
| CRYPTO_THREAD_JOINED;
req_state_mask = CRYPTO_THREAD_FINISHED | CRYPTO_THREAD_JOINED;
while (!CRYPTO_THREAD_GET_STATE(thread, req_state_mask))
ossl_crypto_condvar_wait(thread->condvar, thread->statelock);
if (CRYPTO_THREAD_GET_STATE(thread, CRYPTO_THREAD_TERMINATED)) {
ossl_crypto_mutex_unlock(thread->statelock);
return 0;
}
if (CRYPTO_THREAD_GET_STATE(thread, CRYPTO_THREAD_JOINED))
goto pass;
@ -121,7 +115,6 @@ int ossl_crypto_thread_native_clean(CRYPTO_THREAD *handle)
req_state_mask = 0;
req_state_mask |= CRYPTO_THREAD_FINISHED;
req_state_mask |= CRYPTO_THREAD_TERMINATED;
req_state_mask |= CRYPTO_THREAD_JOINED;
ossl_crypto_mutex_lock(handle->statelock);

View File

@ -21,11 +21,6 @@ int ossl_crypto_thread_native_perform_join(CRYPTO_THREAD *thread, CRYPTO_THREAD_
return 0;
}
int ossl_crypto_thread_native_terminate(CRYPTO_THREAD *thread)
{
return 0;
}
int ossl_crypto_thread_native_exit(void)
{
return 0;

View File

@ -85,52 +85,6 @@ int ossl_crypto_thread_native_perform_join(CRYPTO_THREAD *thread, CRYPTO_THREAD_
return 1;
}
int ossl_crypto_thread_native_terminate(CRYPTO_THREAD *thread)
{
void *res;
uint64_t mask;
pthread_t *handle;
mask = CRYPTO_THREAD_FINISHED;
mask |= CRYPTO_THREAD_TERMINATED;
mask |= CRYPTO_THREAD_JOINED;
if (thread == NULL)
return 0;
ossl_crypto_mutex_lock(thread->statelock);
if (thread->handle == NULL || CRYPTO_THREAD_GET_STATE(thread, mask))
goto terminated;
/* Do not fail when there's a join in progress. Do not block. */
if (CRYPTO_THREAD_GET_STATE(thread, CRYPTO_THREAD_JOIN_AWAIT))
goto fail;
ossl_crypto_mutex_unlock(thread->statelock);
handle = thread->handle;
if (pthread_cancel(*handle) != 0) {
ossl_crypto_mutex_lock(thread->statelock);
goto fail;
}
if (pthread_join(*handle, &res) != 0)
return 0;
if (res != PTHREAD_CANCELED)
return 0;
thread->handle = NULL;
OPENSSL_free(handle);
ossl_crypto_mutex_lock(thread->statelock);
terminated:
CRYPTO_THREAD_UNSET_ERROR(thread, CRYPTO_THREAD_TERMINATED);
CRYPTO_THREAD_SET_STATE(thread, CRYPTO_THREAD_TERMINATED);
ossl_crypto_mutex_unlock(thread->statelock);
return 1;
fail:
CRYPTO_THREAD_SET_ERROR(thread, CRYPTO_THREAD_TERMINATED);
ossl_crypto_mutex_unlock(thread->statelock);
return 0;
}
int ossl_crypto_thread_native_exit(void)
{
pthread_exit(NULL);

View File

@ -83,49 +83,6 @@ int ossl_crypto_thread_native_perform_join(CRYPTO_THREAD *thread, CRYPTO_THREAD_
return 1;
}
int ossl_crypto_thread_native_terminate(CRYPTO_THREAD *thread)
{
uint64_t mask;
HANDLE *handle;
mask = CRYPTO_THREAD_FINISHED;
mask |= CRYPTO_THREAD_TERMINATED;
mask |= CRYPTO_THREAD_JOINED;
if (thread == NULL)
return 1;
ossl_crypto_mutex_lock(thread->statelock);
if (thread->handle == NULL || CRYPTO_THREAD_GET_STATE(thread, mask))
goto terminated;
ossl_crypto_mutex_unlock(thread->statelock);
handle = thread->handle;
if (WaitForSingleObject(*handle, 0) != WAIT_OBJECT_0) {
if (TerminateThread(*handle, STILL_ACTIVE) == 0) {
ossl_crypto_mutex_lock(thread->statelock);
CRYPTO_THREAD_SET_ERROR(thread, CRYPTO_THREAD_TERMINATED);
ossl_crypto_mutex_unlock(thread->statelock);
return 0;
}
}
if (CloseHandle(*handle) == 0) {
CRYPTO_THREAD_SET_ERROR(thread, CRYPTO_THREAD_TERMINATED);
return 0;
}
thread->handle = NULL;
OPENSSL_free(handle);
ossl_crypto_mutex_lock(thread->statelock);
terminated:
CRYPTO_THREAD_UNSET_ERROR(thread, CRYPTO_THREAD_TERMINATED);
CRYPTO_THREAD_SET_STATE(thread, CRYPTO_THREAD_TERMINATED);
ossl_crypto_mutex_unlock(thread->statelock);
return 1;
}
int ossl_crypto_thread_native_exit(void)
{
_endthreadex(0);

View File

@ -55,7 +55,6 @@ typedef CRYPTO_THREAD_RETVAL (*CRYPTO_THREAD_ROUTINE_CB)(void *,
# define CRYPTO_THREAD_FINISHED (1UL << 0)
# define CRYPTO_THREAD_JOIN_AWAIT (1UL << 1)
# define CRYPTO_THREAD_JOINED (1UL << 2)
# define CRYPTO_THREAD_TERMINATED (1UL << 3)
# define CRYPTO_THREAD_GET_STATE(THREAD, FLAG) ((THREAD)->state & (FLAG))
# define CRYPTO_THREAD_GET_ERROR(THREAD, FLAG) (((THREAD)->state >> 16) & (FLAG))
@ -112,7 +111,6 @@ int ossl_crypto_thread_native_join(CRYPTO_THREAD *thread,
CRYPTO_THREAD_RETVAL *retval);
int ossl_crypto_thread_native_perform_join(CRYPTO_THREAD *thread,
CRYPTO_THREAD_RETVAL *retval);
int ossl_crypto_thread_native_terminate(CRYPTO_THREAD *thread);
int ossl_crypto_thread_native_exit(void);
int ossl_crypto_thread_native_is_self(CRYPTO_THREAD *thread);
int ossl_crypto_thread_native_clean(CRYPTO_THREAD *thread);

View File

@ -784,19 +784,6 @@ static uint32_t test_thread_native_fn(void *data)
*ldata = *ldata + 1;
return *ldata - 1;
}
static uint32_t test_thread_noreturn(void *data)
{
while (1) {
OSSL_sleep(1000);
}
/* unreachable */
OPENSSL_die("test_thread_noreturn", __FILE__, __LINE__);
return 0;
}
/* Tests of native threads */
static int test_thread_native(void)
@ -805,7 +792,7 @@ static int test_thread_native(void)
uint32_t local;
CRYPTO_THREAD *t;
/* thread spawn, join and termination */
/* thread spawn, join */
local = 1;
t = ossl_crypto_thread_native_start(test_thread_native_fn, &local, 1);
@ -826,14 +813,6 @@ static int test_thread_native(void)
if (!TEST_int_eq(retval, 1) || !TEST_int_eq(local, 2))
return 0;
if (!TEST_int_eq(ossl_crypto_thread_native_terminate(t), 1))
return 0;
if (!TEST_int_eq(ossl_crypto_thread_native_terminate(t), 1))
return 0;
if (!TEST_int_eq(ossl_crypto_thread_native_join(t, &retval), 0))
return 0;
if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 1))
return 0;
t = NULL;
@ -841,16 +820,6 @@ static int test_thread_native(void)
if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 0))
return 0;
/* termination of a long running thread */
t = ossl_crypto_thread_native_start(test_thread_noreturn, NULL, 1);
if (!TEST_ptr(t))
return 0;
if (!TEST_int_eq(ossl_crypto_thread_native_terminate(t), 1))
return 0;
if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 1))
return 0;
return 1;
}