threads_win.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #if defined(_WIN32)
  10. # include <windows.h>
  11. # if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600
  12. # define USE_RWLOCK
  13. # endif
  14. #endif
  15. #include <openssl/crypto.h>
  16. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS)
  17. # ifdef USE_RWLOCK
  18. typedef struct {
  19. SRWLOCK lock;
  20. int exclusive;
  21. } CRYPTO_win_rwlock;
  22. # endif
  23. CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
  24. {
  25. CRYPTO_RWLOCK *lock;
  26. # ifdef USE_RWLOCK
  27. CRYPTO_win_rwlock *rwlock;
  28. if ((lock = OPENSSL_zalloc(sizeof(CRYPTO_win_rwlock))) == NULL)
  29. return NULL;
  30. rwlock = lock;
  31. InitializeSRWLock(&rwlock->lock);
  32. # else
  33. if ((lock = OPENSSL_zalloc(sizeof(CRITICAL_SECTION))) == NULL) {
  34. /* Don't set error, to avoid recursion blowup. */
  35. return NULL;
  36. }
  37. # if !defined(_WIN32_WCE)
  38. /* 0x400 is the spin count value suggested in the documentation */
  39. if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) {
  40. OPENSSL_free(lock);
  41. return NULL;
  42. }
  43. # else
  44. InitializeCriticalSection(lock);
  45. # endif
  46. # endif
  47. return lock;
  48. }
  49. __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
  50. {
  51. # ifdef USE_RWLOCK
  52. CRYPTO_win_rwlock *rwlock = lock;
  53. AcquireSRWLockShared(&rwlock->lock);
  54. # else
  55. EnterCriticalSection(lock);
  56. # endif
  57. return 1;
  58. }
  59. __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
  60. {
  61. # ifdef USE_RWLOCK
  62. CRYPTO_win_rwlock *rwlock = lock;
  63. AcquireSRWLockExclusive(&rwlock->lock);
  64. rwlock->exclusive = 1;
  65. # else
  66. EnterCriticalSection(lock);
  67. # endif
  68. return 1;
  69. }
  70. int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
  71. {
  72. # ifdef USE_RWLOCK
  73. CRYPTO_win_rwlock *rwlock = lock;
  74. if (rwlock->exclusive) {
  75. rwlock->exclusive = 0;
  76. ReleaseSRWLockExclusive(&rwlock->lock);
  77. } else {
  78. ReleaseSRWLockShared(&rwlock->lock);
  79. }
  80. # else
  81. LeaveCriticalSection(lock);
  82. # endif
  83. return 1;
  84. }
  85. void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
  86. {
  87. if (lock == NULL)
  88. return;
  89. # ifndef USE_RWLOCK
  90. DeleteCriticalSection(lock);
  91. # endif
  92. OPENSSL_free(lock);
  93. return;
  94. }
  95. # define ONCE_UNINITED 0
  96. # define ONCE_ININIT 1
  97. # define ONCE_DONE 2
  98. /*
  99. * We don't use InitOnceExecuteOnce because that isn't available in WinXP which
  100. * we still have to support.
  101. */
  102. int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
  103. {
  104. LONG volatile *lock = (LONG *)once;
  105. LONG result;
  106. if (*lock == ONCE_DONE)
  107. return 1;
  108. do {
  109. result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
  110. if (result == ONCE_UNINITED) {
  111. init();
  112. *lock = ONCE_DONE;
  113. return 1;
  114. }
  115. } while (result == ONCE_ININIT);
  116. return (*lock == ONCE_DONE);
  117. }
  118. int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
  119. {
  120. *key = TlsAlloc();
  121. if (*key == TLS_OUT_OF_INDEXES)
  122. return 0;
  123. return 1;
  124. }
  125. void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
  126. {
  127. DWORD last_error;
  128. void *ret;
  129. /*
  130. * TlsGetValue clears the last error even on success, so that callers may
  131. * distinguish it successfully returning NULL or failing. It is documented
  132. * to never fail if the argument is a valid index from TlsAlloc, so we do
  133. * not need to handle this.
  134. *
  135. * However, this error-mangling behavior interferes with the caller's use of
  136. * GetLastError. In particular SSL_get_error queries the error queue to
  137. * determine whether the caller should look at the OS's errors. To avoid
  138. * destroying state, save and restore the Windows error.
  139. *
  140. * https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx
  141. */
  142. last_error = GetLastError();
  143. ret = TlsGetValue(*key);
  144. SetLastError(last_error);
  145. return ret;
  146. }
  147. int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
  148. {
  149. if (TlsSetValue(*key, val) == 0)
  150. return 0;
  151. return 1;
  152. }
  153. int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
  154. {
  155. if (TlsFree(*key) == 0)
  156. return 0;
  157. return 1;
  158. }
  159. CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
  160. {
  161. return GetCurrentThreadId();
  162. }
  163. int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
  164. {
  165. return (a == b);
  166. }
  167. int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
  168. {
  169. *ret = (int)InterlockedExchangeAdd((long volatile *)val, (long)amount) + amount;
  170. return 1;
  171. }
  172. int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
  173. CRYPTO_RWLOCK *lock)
  174. {
  175. *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, (LONG64)op) | op;
  176. return 1;
  177. }
  178. int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
  179. {
  180. *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, 0);
  181. return 1;
  182. }
  183. int openssl_init_fork_handlers(void)
  184. {
  185. return 0;
  186. }
  187. int openssl_get_fork_id(void)
  188. {
  189. return 0;
  190. }
  191. #endif