threadstest.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright 2016-2017 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. #include "testutil.h"
  14. #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
  15. typedef unsigned int thread_t;
  16. static int run_thread(thread_t *t, void (*f)(void))
  17. {
  18. f();
  19. return 1;
  20. }
  21. static int wait_for_thread(thread_t thread)
  22. {
  23. return 1;
  24. }
  25. #elif defined(OPENSSL_SYS_WINDOWS)
  26. typedef HANDLE thread_t;
  27. static DWORD WINAPI thread_run(LPVOID arg)
  28. {
  29. void (*f)(void);
  30. *(void **) (&f) = arg;
  31. f();
  32. return 0;
  33. }
  34. static int run_thread(thread_t *t, void (*f)(void))
  35. {
  36. *t = CreateThread(NULL, 0, thread_run, *(void **) &f, 0, NULL);
  37. return *t != NULL;
  38. }
  39. static int wait_for_thread(thread_t thread)
  40. {
  41. return WaitForSingleObject(thread, INFINITE) == 0;
  42. }
  43. #else
  44. typedef pthread_t thread_t;
  45. static void *thread_run(void *arg)
  46. {
  47. void (*f)(void);
  48. *(void **) (&f) = arg;
  49. f();
  50. return NULL;
  51. }
  52. static int run_thread(thread_t *t, void (*f)(void))
  53. {
  54. return pthread_create(t, NULL, thread_run, *(void **) &f) == 0;
  55. }
  56. static int wait_for_thread(thread_t thread)
  57. {
  58. return pthread_join(thread, NULL) == 0;
  59. }
  60. #endif
  61. static int test_lock(void)
  62. {
  63. CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
  64. if (!TEST_true(CRYPTO_THREAD_read_lock(lock))
  65. || !TEST_true(CRYPTO_THREAD_unlock(lock)))
  66. return 0;
  67. CRYPTO_THREAD_lock_free(lock);
  68. return 1;
  69. }
  70. static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
  71. static unsigned once_run_count = 0;
  72. static void once_do_run(void)
  73. {
  74. once_run_count++;
  75. }
  76. static void once_run_thread_cb(void)
  77. {
  78. CRYPTO_THREAD_run_once(&once_run, once_do_run);
  79. }
  80. static int test_once(void)
  81. {
  82. thread_t thread;
  83. if (!TEST_true(run_thread(&thread, once_run_thread_cb))
  84. || !TEST_true(wait_for_thread(thread))
  85. || !CRYPTO_THREAD_run_once(&once_run, once_do_run)
  86. || !TEST_int_eq(once_run_count, 1))
  87. return 0;
  88. return 1;
  89. }
  90. static CRYPTO_THREAD_LOCAL thread_local_key;
  91. static unsigned destructor_run_count = 0;
  92. static int thread_local_thread_cb_ok = 0;
  93. static void thread_local_destructor(void *arg)
  94. {
  95. unsigned *count;
  96. if (arg == NULL)
  97. return;
  98. count = arg;
  99. (*count)++;
  100. }
  101. static void thread_local_thread_cb(void)
  102. {
  103. void *ptr;
  104. ptr = CRYPTO_THREAD_get_local(&thread_local_key);
  105. if (!TEST_ptr_null(ptr)
  106. || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key,
  107. &destructor_run_count)))
  108. return;
  109. ptr = CRYPTO_THREAD_get_local(&thread_local_key);
  110. if (!TEST_ptr_eq(ptr, &destructor_run_count))
  111. return;
  112. thread_local_thread_cb_ok = 1;
  113. }
  114. static int test_thread_local(void)
  115. {
  116. thread_t thread;
  117. void *ptr = NULL;
  118. if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
  119. thread_local_destructor)))
  120. return 0;
  121. ptr = CRYPTO_THREAD_get_local(&thread_local_key);
  122. if (!TEST_ptr_null(ptr)
  123. || !TEST_true(run_thread(&thread, thread_local_thread_cb))
  124. || !TEST_true(wait_for_thread(thread))
  125. || !TEST_int_eq(thread_local_thread_cb_ok, 1))
  126. return 0;
  127. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
  128. ptr = CRYPTO_THREAD_get_local(&thread_local_key);
  129. if (!TEST_ptr_null(ptr))
  130. return 0;
  131. # if !defined(OPENSSL_SYS_WINDOWS)
  132. if (!TEST_int_eq(destructor_run_count, 1))
  133. return 0;
  134. # endif
  135. #endif
  136. if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
  137. return 0;
  138. return 1;
  139. }
  140. int setup_tests(void)
  141. {
  142. ADD_TEST(test_lock);
  143. ADD_TEST(test_once);
  144. ADD_TEST(test_thread_local);
  145. return 1;
  146. }