threads_win.c 5.1 KB

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