Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include "internal.h" |
| 16 | |
Adam Langley | 65a7e94 | 2015-05-07 18:28:27 -0700 | [diff] [blame] | 17 | #if !defined(OPENSSL_WINDOWS) && !defined(OPENSSL_NO_THREADS) |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 18 | |
| 19 | #include <pthread.h> |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 21 | #include <string.h> |
| 22 | |
| 23 | #include <openssl/mem.h> |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 24 | #include <openssl/type_check.h> |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 25 | |
| 26 | |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 27 | OPENSSL_COMPILE_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(pthread_rwlock_t), |
| 28 | CRYPTO_MUTEX_too_small); |
| 29 | |
| 30 | void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) { |
| 31 | if (pthread_rwlock_init((pthread_rwlock_t *) lock, NULL) != 0) { |
| 32 | abort(); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) { |
| 37 | if (pthread_rwlock_rdlock((pthread_rwlock_t *) lock) != 0) { |
| 38 | abort(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) { |
| 43 | if (pthread_rwlock_wrlock((pthread_rwlock_t *) lock) != 0) { |
| 44 | abort(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void CRYPTO_MUTEX_unlock(CRYPTO_MUTEX *lock) { |
| 49 | if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) { |
| 50 | abort(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) { |
| 55 | pthread_rwlock_destroy((pthread_rwlock_t *) lock); |
| 56 | } |
| 57 | |
| 58 | void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) { |
| 59 | if (pthread_rwlock_rdlock(&lock->lock) != 0) { |
| 60 | abort(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) { |
| 65 | if (pthread_rwlock_wrlock(&lock->lock) != 0) { |
| 66 | abort(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void CRYPTO_STATIC_MUTEX_unlock(struct CRYPTO_STATIC_MUTEX *lock) { |
| 71 | if (pthread_rwlock_unlock(&lock->lock) != 0) { |
| 72 | abort(); |
| 73 | } |
| 74 | } |
| 75 | |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 76 | void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) { |
| 77 | pthread_once(once, init); |
| 78 | } |
| 79 | |
| 80 | static pthread_mutex_t g_destructors_lock = PTHREAD_MUTEX_INITIALIZER; |
| 81 | static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS]; |
| 82 | |
| 83 | static void thread_local_destructor(void *arg) { |
| 84 | if (arg == NULL) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS]; |
| 89 | if (pthread_mutex_lock(&g_destructors_lock) != 0) { |
| 90 | return; |
| 91 | } |
| 92 | memcpy(destructors, g_destructors, sizeof(destructors)); |
| 93 | pthread_mutex_unlock(&g_destructors_lock); |
| 94 | |
| 95 | unsigned i; |
| 96 | void **pointers = arg; |
| 97 | for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) { |
| 98 | if (destructors[i] != NULL) { |
| 99 | destructors[i](pointers[i]); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | OPENSSL_free(pointers); |
| 104 | } |
| 105 | |
| 106 | static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT; |
| 107 | static pthread_key_t g_thread_local_key; |
| 108 | static int g_thread_local_failed = 0; |
| 109 | |
| 110 | static void thread_local_init(void) { |
| 111 | g_thread_local_failed = |
| 112 | pthread_key_create(&g_thread_local_key, thread_local_destructor) != 0; |
| 113 | } |
| 114 | |
| 115 | void *CRYPTO_get_thread_local(thread_local_data_t index) { |
| 116 | CRYPTO_once(&g_thread_local_init_once, thread_local_init); |
| 117 | if (g_thread_local_failed) { |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | void **pointers = pthread_getspecific(g_thread_local_key); |
| 122 | if (pointers == NULL) { |
| 123 | return NULL; |
| 124 | } |
| 125 | return pointers[index]; |
| 126 | } |
| 127 | |
| 128 | int CRYPTO_set_thread_local(thread_local_data_t index, void *value, |
| 129 | thread_local_destructor_t destructor) { |
| 130 | CRYPTO_once(&g_thread_local_init_once, thread_local_init); |
| 131 | if (g_thread_local_failed) { |
| 132 | destructor(value); |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | void **pointers = pthread_getspecific(g_thread_local_key); |
| 137 | if (pointers == NULL) { |
| 138 | pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS); |
| 139 | if (pointers == NULL) { |
| 140 | destructor(value); |
| 141 | return 0; |
| 142 | } |
| 143 | memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS); |
| 144 | if (pthread_setspecific(g_thread_local_key, pointers) != 0) { |
| 145 | OPENSSL_free(pointers); |
| 146 | destructor(value); |
| 147 | return 0; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (pthread_mutex_lock(&g_destructors_lock) != 0) { |
| 152 | destructor(value); |
| 153 | return 0; |
| 154 | } |
| 155 | g_destructors[index] = destructor; |
| 156 | pthread_mutex_unlock(&g_destructors_lock); |
| 157 | |
| 158 | pointers[index] = value; |
| 159 | return 1; |
| 160 | } |
| 161 | |
Adam Langley | 65a7e94 | 2015-05-07 18:28:27 -0700 | [diff] [blame] | 162 | #endif /* !OPENSSL_WINDOWS && !OPENSSL_NO_THREADS */ |