threads_win.c 3.8 KB

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