25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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