threads_win.c 3.7 KB

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