You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

177 line
4.7 KiB

  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. #include "internal.h"
  15. #if defined(OPENSSL_PTHREADS)
  16. #include <pthread.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <openssl/mem.h>
  20. #include <openssl/type_check.h>
  21. OPENSSL_COMPILE_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(pthread_rwlock_t),
  22. CRYPTO_MUTEX_too_small);
  23. void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
  24. if (pthread_rwlock_init((pthread_rwlock_t *) lock, NULL) != 0) {
  25. abort();
  26. }
  27. }
  28. void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) {
  29. if (pthread_rwlock_rdlock((pthread_rwlock_t *) lock) != 0) {
  30. abort();
  31. }
  32. }
  33. void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) {
  34. if (pthread_rwlock_wrlock((pthread_rwlock_t *) lock) != 0) {
  35. abort();
  36. }
  37. }
  38. void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock) {
  39. if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
  40. abort();
  41. }
  42. }
  43. void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock) {
  44. if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
  45. abort();
  46. }
  47. }
  48. void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
  49. pthread_rwlock_destroy((pthread_rwlock_t *) lock);
  50. }
  51. void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
  52. if (pthread_rwlock_rdlock(&lock->lock) != 0) {
  53. abort();
  54. }
  55. }
  56. void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
  57. if (pthread_rwlock_wrlock(&lock->lock) != 0) {
  58. abort();
  59. }
  60. }
  61. void CRYPTO_STATIC_MUTEX_unlock_read(struct CRYPTO_STATIC_MUTEX *lock) {
  62. if (pthread_rwlock_unlock(&lock->lock) != 0) {
  63. abort();
  64. }
  65. }
  66. void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX *lock) {
  67. if (pthread_rwlock_unlock(&lock->lock) != 0) {
  68. abort();
  69. }
  70. }
  71. void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) {
  72. if (pthread_once(once, init) != 0) {
  73. abort();
  74. }
  75. }
  76. static pthread_mutex_t g_destructors_lock = PTHREAD_MUTEX_INITIALIZER;
  77. static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
  78. static void thread_local_destructor(void *arg) {
  79. if (arg == NULL) {
  80. return;
  81. }
  82. thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
  83. if (pthread_mutex_lock(&g_destructors_lock) != 0) {
  84. return;
  85. }
  86. OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors));
  87. pthread_mutex_unlock(&g_destructors_lock);
  88. unsigned i;
  89. void **pointers = arg;
  90. for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
  91. if (destructors[i] != NULL) {
  92. destructors[i](pointers[i]);
  93. }
  94. }
  95. OPENSSL_free(pointers);
  96. }
  97. static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT;
  98. static pthread_key_t g_thread_local_key;
  99. static int g_thread_local_failed = 0;
  100. static void thread_local_init(void) {
  101. g_thread_local_failed =
  102. pthread_key_create(&g_thread_local_key, thread_local_destructor) != 0;
  103. }
  104. void *CRYPTO_get_thread_local(thread_local_data_t index) {
  105. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  106. if (g_thread_local_failed) {
  107. return NULL;
  108. }
  109. void **pointers = pthread_getspecific(g_thread_local_key);
  110. if (pointers == NULL) {
  111. return NULL;
  112. }
  113. return pointers[index];
  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_failed) {
  119. destructor(value);
  120. return 0;
  121. }
  122. void **pointers = pthread_getspecific(g_thread_local_key);
  123. if (pointers == NULL) {
  124. pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
  125. if (pointers == NULL) {
  126. destructor(value);
  127. return 0;
  128. }
  129. OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
  130. if (pthread_setspecific(g_thread_local_key, pointers) != 0) {
  131. OPENSSL_free(pointers);
  132. destructor(value);
  133. return 0;
  134. }
  135. }
  136. if (pthread_mutex_lock(&g_destructors_lock) != 0) {
  137. destructor(value);
  138. return 0;
  139. }
  140. g_destructors[index] = destructor;
  141. pthread_mutex_unlock(&g_destructors_lock);
  142. pointers[index] = value;
  143. return 1;
  144. }
  145. #endif // OPENSSL_PTHREADS