threadstest.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. OPENSSL_thread_stop();
  51. return NULL;
  52. }
  53. static int run_thread(thread_t *t, void (*f)(void))
  54. {
  55. return pthread_create(t, NULL, thread_run, *(void **) &f) == 0;
  56. }
  57. static int wait_for_thread(thread_t thread)
  58. {
  59. return pthread_join(thread, NULL) == 0;
  60. }
  61. #endif