threadstest.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2021 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 <string.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