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;
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);
return 0;
}
-int ossl_crypto_thread_native_terminate(CRYPTO_THREAD *thread)
-{
- return 0;
-}
-
int ossl_crypto_thread_native_exit(void)
{
return 0;
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);
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);
# 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))
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);
*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)
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);
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;
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;
}