11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
51cb0ef41Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
61cb0ef41Sopenharmony_ci * in the file LICENSE in the source distribution or at
71cb0ef41Sopenharmony_ci * https://www.openssl.org/source/license.html
81cb0ef41Sopenharmony_ci */
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include <openssl/crypto.h>
111cb0ef41Sopenharmony_ci#include "internal/cryptlib.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci#if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci# if defined(OPENSSL_SYS_UNIX)
161cb0ef41Sopenharmony_ci#  include <sys/types.h>
171cb0ef41Sopenharmony_ci#  include <unistd.h>
181cb0ef41Sopenharmony_ci# endif
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciCRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
211cb0ef41Sopenharmony_ci{
221cb0ef41Sopenharmony_ci    CRYPTO_RWLOCK *lock;
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci    if ((lock = OPENSSL_zalloc(sizeof(unsigned int))) == NULL) {
251cb0ef41Sopenharmony_ci        /* Don't set error, to avoid recursion blowup. */
261cb0ef41Sopenharmony_ci        return NULL;
271cb0ef41Sopenharmony_ci    }
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    *(unsigned int *)lock = 1;
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    return lock;
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci__owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
351cb0ef41Sopenharmony_ci{
361cb0ef41Sopenharmony_ci    if (!ossl_assert(*(unsigned int *)lock == 1))
371cb0ef41Sopenharmony_ci        return 0;
381cb0ef41Sopenharmony_ci    return 1;
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci__owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
421cb0ef41Sopenharmony_ci{
431cb0ef41Sopenharmony_ci    if (!ossl_assert(*(unsigned int *)lock == 1))
441cb0ef41Sopenharmony_ci        return 0;
451cb0ef41Sopenharmony_ci    return 1;
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciint CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci    if (!ossl_assert(*(unsigned int *)lock == 1))
511cb0ef41Sopenharmony_ci        return 0;
521cb0ef41Sopenharmony_ci    return 1;
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_civoid CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) {
561cb0ef41Sopenharmony_ci    if (lock == NULL)
571cb0ef41Sopenharmony_ci        return;
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci    *(unsigned int *)lock = 0;
601cb0ef41Sopenharmony_ci    OPENSSL_free(lock);
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci    return;
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciint CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
661cb0ef41Sopenharmony_ci{
671cb0ef41Sopenharmony_ci    if (*once != 0)
681cb0ef41Sopenharmony_ci        return 1;
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci    init();
711cb0ef41Sopenharmony_ci    *once = 1;
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci    return 1;
741cb0ef41Sopenharmony_ci}
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci#define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_cistatic void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ciint CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
811cb0ef41Sopenharmony_ci{
821cb0ef41Sopenharmony_ci    static unsigned int thread_local_key = 0;
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci    if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
851cb0ef41Sopenharmony_ci        return 0;
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci    *key = thread_local_key++;
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci    thread_local_storage[*key] = NULL;
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci    return 1;
921cb0ef41Sopenharmony_ci}
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_civoid *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
951cb0ef41Sopenharmony_ci{
961cb0ef41Sopenharmony_ci    if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
971cb0ef41Sopenharmony_ci        return NULL;
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci    return thread_local_storage[*key];
1001cb0ef41Sopenharmony_ci}
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ciint CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
1031cb0ef41Sopenharmony_ci{
1041cb0ef41Sopenharmony_ci    if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
1051cb0ef41Sopenharmony_ci        return 0;
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci    thread_local_storage[*key] = val;
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci    return 1;
1101cb0ef41Sopenharmony_ci}
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ciint CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
1131cb0ef41Sopenharmony_ci{
1141cb0ef41Sopenharmony_ci    *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
1151cb0ef41Sopenharmony_ci    return 1;
1161cb0ef41Sopenharmony_ci}
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ciCRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
1191cb0ef41Sopenharmony_ci{
1201cb0ef41Sopenharmony_ci    return 0;
1211cb0ef41Sopenharmony_ci}
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ciint CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
1241cb0ef41Sopenharmony_ci{
1251cb0ef41Sopenharmony_ci    return (a == b);
1261cb0ef41Sopenharmony_ci}
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ciint CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
1291cb0ef41Sopenharmony_ci{
1301cb0ef41Sopenharmony_ci    *val += amount;
1311cb0ef41Sopenharmony_ci    *ret  = *val;
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci    return 1;
1341cb0ef41Sopenharmony_ci}
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ciint CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
1371cb0ef41Sopenharmony_ci                     CRYPTO_RWLOCK *lock)
1381cb0ef41Sopenharmony_ci{
1391cb0ef41Sopenharmony_ci    *val |= op;
1401cb0ef41Sopenharmony_ci    *ret  = *val;
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci    return 1;
1431cb0ef41Sopenharmony_ci}
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ciint CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
1461cb0ef41Sopenharmony_ci{
1471cb0ef41Sopenharmony_ci    *ret  = *val;
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci    return 1;
1501cb0ef41Sopenharmony_ci}
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ciint openssl_init_fork_handlers(void)
1531cb0ef41Sopenharmony_ci{
1541cb0ef41Sopenharmony_ci    return 0;
1551cb0ef41Sopenharmony_ci}
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ciint openssl_get_fork_id(void)
1581cb0ef41Sopenharmony_ci{
1591cb0ef41Sopenharmony_ci# if defined(OPENSSL_SYS_UNIX)
1601cb0ef41Sopenharmony_ci    return getpid();
1611cb0ef41Sopenharmony_ci# else
1621cb0ef41Sopenharmony_ci    return 0;
1631cb0ef41Sopenharmony_ci# endif
1641cb0ef41Sopenharmony_ci}
1651cb0ef41Sopenharmony_ci#endif
166