Home | History | Annotate | Line # | Download | only in internal
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos #ifndef OSSL_INTERNAL_THREAD_ARCH_H
     11  1.1.1.2  christos #define OSSL_INTERNAL_THREAD_ARCH_H
     12  1.1.1.2  christos #include <openssl/configuration.h>
     13  1.1.1.2  christos #include <openssl/e_os2.h>
     14  1.1.1.2  christos #include "internal/time.h"
     15  1.1.1.2  christos 
     16  1.1.1.2  christos #if defined(_WIN32)
     17  1.1.1.2  christos #include <windows.h>
     18  1.1.1.2  christos #endif
     19  1.1.1.2  christos 
     20  1.1.1.2  christos #if defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_UNIX)
     21  1.1.1.2  christos #define OPENSSL_THREADS_POSIX
     22  1.1.1.2  christos #elif defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_VMS)
     23  1.1.1.2  christos #define OPENSSL_THREADS_POSIX
     24  1.1.1.2  christos #elif defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_WINDOWS) && defined(_WIN32_WINNT)
     25  1.1.1.2  christos #if _WIN32_WINNT >= 0x0600
     26  1.1.1.2  christos #define OPENSSL_THREADS_WINNT
     27  1.1.1.2  christos #elif _WIN32_WINNT >= 0x0501
     28  1.1.1.2  christos #define OPENSSL_THREADS_WINNT
     29  1.1.1.2  christos #define OPENSSL_THREADS_WINNT_LEGACY
     30  1.1.1.2  christos #else
     31  1.1.1.2  christos #define OPENSSL_THREADS_NONE
     32  1.1.1.2  christos #endif
     33  1.1.1.2  christos #else
     34  1.1.1.2  christos #define OPENSSL_THREADS_NONE
     35  1.1.1.2  christos #endif
     36      1.1  christos 
     37  1.1.1.2  christos #include <openssl/crypto.h>
     38      1.1  christos 
     39      1.1  christos typedef struct crypto_mutex_st CRYPTO_MUTEX;
     40      1.1  christos typedef struct crypto_condvar_st CRYPTO_CONDVAR;
     41      1.1  christos 
     42      1.1  christos CRYPTO_MUTEX *ossl_crypto_mutex_new(void);
     43      1.1  christos void ossl_crypto_mutex_lock(CRYPTO_MUTEX *mutex);
     44      1.1  christos int ossl_crypto_mutex_try_lock(CRYPTO_MUTEX *mutex);
     45      1.1  christos void ossl_crypto_mutex_unlock(CRYPTO_MUTEX *mutex);
     46      1.1  christos void ossl_crypto_mutex_free(CRYPTO_MUTEX **mutex);
     47      1.1  christos 
     48      1.1  christos CRYPTO_CONDVAR *ossl_crypto_condvar_new(void);
     49      1.1  christos void ossl_crypto_condvar_wait(CRYPTO_CONDVAR *cv, CRYPTO_MUTEX *mutex);
     50      1.1  christos void ossl_crypto_condvar_wait_timeout(CRYPTO_CONDVAR *cv, CRYPTO_MUTEX *mutex,
     51  1.1.1.2  christos     OSSL_TIME deadline);
     52      1.1  christos void ossl_crypto_condvar_broadcast(CRYPTO_CONDVAR *cv);
     53      1.1  christos void ossl_crypto_condvar_signal(CRYPTO_CONDVAR *cv);
     54      1.1  christos void ossl_crypto_condvar_free(CRYPTO_CONDVAR **cv);
     55      1.1  christos 
     56      1.1  christos typedef uint32_t CRYPTO_THREAD_RETVAL;
     57      1.1  christos typedef CRYPTO_THREAD_RETVAL (*CRYPTO_THREAD_ROUTINE)(void *);
     58      1.1  christos typedef CRYPTO_THREAD_RETVAL (*CRYPTO_THREAD_ROUTINE_CB)(void *,
     59  1.1.1.2  christos     void (**)(void *),
     60  1.1.1.2  christos     void **);
     61      1.1  christos 
     62  1.1.1.2  christos #define CRYPTO_THREAD_NO_STATE 0UL
     63  1.1.1.2  christos #define CRYPTO_THREAD_FINISHED (1UL << 0)
     64  1.1.1.2  christos #define CRYPTO_THREAD_JOIN_AWAIT (1UL << 1)
     65  1.1.1.2  christos #define CRYPTO_THREAD_JOINED (1UL << 2)
     66      1.1  christos 
     67  1.1.1.2  christos #define CRYPTO_THREAD_GET_STATE(THREAD, FLAG) ((THREAD)->state & (FLAG))
     68  1.1.1.2  christos #define CRYPTO_THREAD_GET_ERROR(THREAD, FLAG) (((THREAD)->state >> 16) & (FLAG))
     69      1.1  christos 
     70      1.1  christos typedef struct crypto_thread_st {
     71      1.1  christos     uint32_t state;
     72      1.1  christos     void *data;
     73      1.1  christos     CRYPTO_THREAD_ROUTINE routine;
     74      1.1  christos     CRYPTO_THREAD_RETVAL retval;
     75      1.1  christos     void *handle;
     76      1.1  christos     CRYPTO_MUTEX *lock;
     77      1.1  christos     CRYPTO_MUTEX *statelock;
     78      1.1  christos     CRYPTO_CONDVAR *condvar;
     79      1.1  christos     unsigned long thread_id;
     80      1.1  christos     int joinable;
     81      1.1  christos     OSSL_LIB_CTX *ctx;
     82      1.1  christos } CRYPTO_THREAD;
     83      1.1  christos 
     84  1.1.1.2  christos #if defined(OPENSSL_THREADS)
     85      1.1  christos 
     86  1.1.1.2  christos #define CRYPTO_THREAD_UNSET_STATE(THREAD, FLAG) \
     87  1.1.1.2  christos     do {                                        \
     88  1.1.1.2  christos         (THREAD)->state &= ~(FLAG);             \
     89      1.1  christos     } while ((void)0, 0)
     90      1.1  christos 
     91  1.1.1.2  christos #define CRYPTO_THREAD_SET_STATE(THREAD, FLAG) \
     92  1.1.1.2  christos     do {                                      \
     93  1.1.1.2  christos         (THREAD)->state |= (FLAG);            \
     94      1.1  christos     } while ((void)0, 0)
     95      1.1  christos 
     96  1.1.1.2  christos #define CRYPTO_THREAD_SET_ERROR(THREAD, FLAG) \
     97  1.1.1.2  christos     do {                                      \
     98  1.1.1.2  christos         (THREAD)->state |= ((FLAG) << 16);    \
     99      1.1  christos     } while ((void)0, 0)
    100      1.1  christos 
    101  1.1.1.2  christos #define CRYPTO_THREAD_UNSET_ERROR(THREAD, FLAG) \
    102  1.1.1.2  christos     do {                                        \
    103  1.1.1.2  christos         (THREAD)->state &= ~((FLAG) << 16);     \
    104      1.1  christos     } while ((void)0, 0)
    105      1.1  christos 
    106  1.1.1.2  christos #else
    107      1.1  christos 
    108  1.1.1.2  christos #define CRYPTO_THREAD_UNSET_STATE(THREAD, FLAG)
    109  1.1.1.2  christos #define CRYPTO_THREAD_SET_STATE(THREAD, FLAG)
    110  1.1.1.2  christos #define CRYPTO_THREAD_SET_ERROR(THREAD, FLAG)
    111  1.1.1.2  christos #define CRYPTO_THREAD_UNSET_ERROR(THREAD, FLAG)
    112      1.1  christos 
    113  1.1.1.2  christos #endif /* defined(OPENSSL_THREADS) */
    114      1.1  christos 
    115  1.1.1.2  christos CRYPTO_THREAD *ossl_crypto_thread_native_start(CRYPTO_THREAD_ROUTINE routine,
    116  1.1.1.2  christos     void *data, int joinable);
    117      1.1  christos int ossl_crypto_thread_native_spawn(CRYPTO_THREAD *thread);
    118      1.1  christos int ossl_crypto_thread_native_join(CRYPTO_THREAD *thread,
    119  1.1.1.2  christos     CRYPTO_THREAD_RETVAL *retval);
    120      1.1  christos int ossl_crypto_thread_native_perform_join(CRYPTO_THREAD *thread,
    121  1.1.1.2  christos     CRYPTO_THREAD_RETVAL *retval);
    122      1.1  christos int ossl_crypto_thread_native_exit(void);
    123      1.1  christos int ossl_crypto_thread_native_is_self(CRYPTO_THREAD *thread);
    124      1.1  christos int ossl_crypto_thread_native_clean(CRYPTO_THREAD *thread);
    125      1.1  christos 
    126      1.1  christos #endif /* OSSL_INTERNAL_THREAD_ARCH_H */
    127