Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

163 rader
4.4 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_WINDOWS)
  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(CRYPTO_MUTEX *lock) {
  39. if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
  40. abort();
  41. }
  42. }
  43. void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
  44. pthread_rwlock_destroy((pthread_rwlock_t *) lock);
  45. }
  46. void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
  47. if (pthread_rwlock_rdlock(&lock->lock) != 0) {
  48. abort();
  49. }
  50. }
  51. void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
  52. if (pthread_rwlock_wrlock(&lock->lock) != 0) {
  53. abort();
  54. }
  55. }
  56. void CRYPTO_STATIC_MUTEX_unlock(struct CRYPTO_STATIC_MUTEX *lock) {
  57. if (pthread_rwlock_unlock(&lock->lock) != 0) {
  58. abort();
  59. }
  60. }
  61. void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) {
  62. pthread_once(once, init);
  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. static void thread_local_destructor(void *arg) {
  67. if (arg == NULL) {
  68. return;
  69. }
  70. thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
  71. if (pthread_mutex_lock(&g_destructors_lock) != 0) {
  72. return;
  73. }
  74. memcpy(destructors, g_destructors, sizeof(destructors));
  75. pthread_mutex_unlock(&g_destructors_lock);
  76. unsigned i;
  77. void **pointers = arg;
  78. for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
  79. if (destructors[i] != NULL) {
  80. destructors[i](pointers[i]);
  81. }
  82. }
  83. OPENSSL_free(pointers);
  84. }
  85. static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT;
  86. static pthread_key_t g_thread_local_key;
  87. static int g_thread_local_failed = 0;
  88. static void thread_local_init(void) {
  89. g_thread_local_failed =
  90. pthread_key_create(&g_thread_local_key, thread_local_destructor) != 0;
  91. }
  92. void *CRYPTO_get_thread_local(thread_local_data_t index) {
  93. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  94. if (g_thread_local_failed) {
  95. return NULL;
  96. }
  97. void **pointers = pthread_getspecific(g_thread_local_key);
  98. if (pointers == NULL) {
  99. return NULL;
  100. }
  101. return pointers[index];
  102. }
  103. int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
  104. thread_local_destructor_t destructor) {
  105. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  106. if (g_thread_local_failed) {
  107. destructor(value);
  108. return 0;
  109. }
  110. void **pointers = pthread_getspecific(g_thread_local_key);
  111. if (pointers == NULL) {
  112. pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
  113. if (pointers == NULL) {
  114. destructor(value);
  115. return 0;
  116. }
  117. memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
  118. if (pthread_setspecific(g_thread_local_key, pointers) != 0) {
  119. OPENSSL_free(pointers);
  120. destructor(value);
  121. return 0;
  122. }
  123. }
  124. if (pthread_mutex_lock(&g_destructors_lock) != 0) {
  125. destructor(value);
  126. return 0;
  127. }
  128. g_destructors[index] = destructor;
  129. pthread_mutex_unlock(&g_destructors_lock);
  130. pointers[index] = value;
  131. return 1;
  132. }
  133. #endif /* !OPENSSL_WINDOWS */