threads_win.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. return TlsGetValue(*key);
  84. }
  85. int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
  86. {
  87. if (TlsSetValue(*key, val) == 0)
  88. return 0;
  89. return 1;
  90. }
  91. int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
  92. {
  93. if (TlsFree(*key) == 0)
  94. return 0;
  95. return 1;
  96. }
  97. CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
  98. {
  99. return GetCurrentThreadId();
  100. }
  101. int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
  102. {
  103. return (a == b);
  104. }
  105. int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
  106. {
  107. *ret = InterlockedExchangeAdd(val, amount) + amount;
  108. return 1;
  109. }
  110. int CRYPTO_atomic_read(int *val, int *ret, CRYPTO_RWLOCK *lock)
  111. {
  112. *ret = InterlockedCompareExchange(val, 0, 0);
  113. return 1;
  114. }
  115. int CRYPTO_atomic_write(int *val, int n, CRYPTO_RWLOCK *lock)
  116. {
  117. InterlockedExchange(val, n);
  118. return 1;
  119. }
  120. int openssl_init_fork_handlers(void)
  121. {
  122. return 0;
  123. }
  124. #endif