| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | // Ensure we can't call OPENSSL_malloc circularly. |
| 16 | #define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC |
| 17 | #include "internal.h" |
| 18 |
| 19 | #if defined(OPENSSL_PTHREADS) |
| 20 |
| 21 | #include <assert.h> |
| 22 | #include <pthread.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 |
| 26 | void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) { |
| 27 | if (pthread_rwlock_init(lock, NULL) != 0) { |
| 28 | abort(); |
| 29 | } |
| 30 | } |
| 31 |
| 32 | void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) { |
| 33 | if (pthread_rwlock_rdlock(lock) != 0) { |
| 34 | abort(); |
| 35 | } |
| 36 | } |
| 37 |
| 38 | void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) { |
| 39 | if (pthread_rwlock_wrlock(lock) != 0) { |
| 40 | abort(); |
| 41 | } |
| 42 | } |
| 43 |
| 44 | void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock) { |
| 45 | if (pthread_rwlock_unlock(lock) != 0) { |
| 46 | abort(); |
| 47 | } |
| 48 | } |
| 49 |
| 50 | void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock) { |
| 51 | if (pthread_rwlock_unlock(lock) != 0) { |
| 52 | abort(); |
| 53 | } |
| 54 | } |
| 55 |
| 56 | void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) { pthread_rwlock_destroy(lock); } |
| 57 |
| 58 | void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) { |
| 59 | if (pthread_once(once, init) != 0) { |
| 60 | abort(); |
| 61 | } |
| 62 | } |
| 63 |
| 64 | static pthread_mutex_t g_destructors_lock = PTHREAD_MUTEX_INITIALIZER; |
| 65 | static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS]; |
| 66 |
| 67 | // thread_local_destructor is called when a thread exits. It releases thread |
| 68 | // local data for that thread only. |
| 69 | static void thread_local_destructor(void *arg) { |
| 70 | if (arg == NULL) { |
| 71 | return; |
| 72 | } |
| 73 |
| 74 | thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS]; |
| 75 | if (pthread_mutex_lock(&g_destructors_lock) != 0) { |
| 76 | return; |
| 77 | } |
| 78 | OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors)); |
| 79 | pthread_mutex_unlock(&g_destructors_lock); |
| 80 |
| 81 | unsigned i; |
| 82 | void **pointers = reinterpret_cast<void **>(arg); |
| 83 | for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) { |
| 84 | if (destructors[i] != NULL) { |
| 85 | destructors[i](pointers[i]); |
| 86 | } |
| 87 | } |
| 88 |
| 89 | free(pointers); |
| 90 | } |
| 91 |
| 92 | static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT; |
| 93 | static pthread_key_t g_thread_local_key; |
| 94 | static int g_thread_local_key_created = 0; |
| 95 |
| 96 | static void thread_local_init(void) { |
| 97 | g_thread_local_key_created = |
| 98 | pthread_key_create(&g_thread_local_key, thread_local_destructor) == 0; |
| 99 | } |
| 100 |
| 101 | void *CRYPTO_get_thread_local(thread_local_data_t index) { |
| 102 | CRYPTO_once(&g_thread_local_init_once, thread_local_init); |
| 103 | if (!g_thread_local_key_created) { |
| 104 | return NULL; |
| 105 | } |
| 106 |
| 107 | void **pointers = |
| 108 | reinterpret_cast<void **>(pthread_getspecific(g_thread_local_key)); |
| 109 | if (pointers == NULL) { |
| 110 | return NULL; |
| 111 | } |
| 112 | return pointers[index]; |
| 113 | } |
| 114 |
| 115 | int CRYPTO_set_thread_local(thread_local_data_t index, void *value, |
| 116 | thread_local_destructor_t destructor) { |
| 117 | CRYPTO_once(&g_thread_local_init_once, thread_local_init); |
| 118 | if (!g_thread_local_key_created) { |
| 119 | destructor(value); |
| 120 | return 0; |
| 121 | } |
| 122 |
| 123 | void **pointers = |
| 124 | reinterpret_cast<void **>(pthread_getspecific(g_thread_local_key)); |
| 125 | if (pointers == NULL) { |
| 126 | pointers = reinterpret_cast<void **>( |
| 127 | malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS)); |
| 128 | if (pointers == NULL) { |
| 129 | destructor(value); |
| 130 | return 0; |
| 131 | } |
| 132 | OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS); |
| 133 | if (pthread_setspecific(g_thread_local_key, pointers) != 0) { |
| 134 | free(pointers); |
| 135 | destructor(value); |
| 136 | return 0; |
| 137 | } |
| 138 | } |
| 139 |
| 140 | if (pthread_mutex_lock(&g_destructors_lock) != 0) { |
| 141 | destructor(value); |
| 142 | return 0; |
| 143 | } |
| 144 | g_destructors[index] = destructor; |
| 145 | pthread_mutex_unlock(&g_destructors_lock); |
| 146 |
| 147 | pointers[index] = value; |
| 148 | return 1; |
| 149 | } |
| 150 |
| 151 | #endif // OPENSSL_PTHREADS |
| 152 |