1 /* 2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the OpenSSL license (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/crypto.h> 11 #include "internal/cryptlib.h" 12 13 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG) 14 15 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void) 16 { 17 CRYPTO_RWLOCK *lock; 18 19 if ((lock = OPENSSL_zalloc(sizeof(unsigned int))) == NULL) { 20 /* Don't set error, to avoid recursion blowup. */ 21 return NULL; 22 } 23 24 *(unsigned int *)lock = 1; 25 26 return lock; 27 } 28 29 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock) 30 { 31 if (!ossl_assert(*(unsigned int *)lock == 1)) 32 return 0; 33 return 1; 34 } 35 36 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock) 37 { 38 if (!ossl_assert(*(unsigned int *)lock == 1)) 39 return 0; 40 return 1; 41 } 42 43 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock) 44 { 45 if (!ossl_assert(*(unsigned int *)lock == 1)) 46 return 0; 47 return 1; 48 } 49 50 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) { 51 if (lock == NULL) 52 return; 53 54 *(unsigned int *)lock = 0; 55 OPENSSL_free(lock); 56 57 return; 58 } 59 60 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void)) 61 { 62 if (*once != 0) 63 return 1; 64 65 init(); 66 *once = 1; 67 68 return 1; 69 } 70 71 #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256 72 73 static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX]; 74 75 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) 76 { 77 static unsigned int thread_local_key = 0; 78 79 if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX) 80 return 0; 81 82 *key = thread_local_key++; 83 84 thread_local_storage[*key] = NULL; 85 86 return 1; 87 } 88 89 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key) 90 { 91 if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX) 92 return NULL; 93 94 return thread_local_storage[*key]; 95 } 96 97 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val) 98 { 99 if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX) 100 return 0; 101 102 thread_local_storage[*key] = val; 103 104 return 1; 105 } 106 107 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key) 108 { 109 *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1; 110 return 1; 111 } 112 113 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void) 114 { 115 return 0; 116 } 117 118 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b) 119 { 120 return (a == b); 121 } 122 123 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock) 124 { 125 *val += amount; 126 *ret = *val; 127 128 return 1; 129 } 130 131 int openssl_init_fork_handlers(void) 132 { 133 return 0; 134 } 135 136 #endif 137