1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert #if defined(_WIN32)
11*e0c4386eSCy Schubert # include <windows.h>
12*e0c4386eSCy Schubert #endif
13*e0c4386eSCy Schubert
14*e0c4386eSCy Schubert #include <string.h>
15*e0c4386eSCy Schubert #include "testutil.h"
16*e0c4386eSCy Schubert
17*e0c4386eSCy Schubert #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
18*e0c4386eSCy Schubert
19*e0c4386eSCy Schubert typedef unsigned int thread_t;
20*e0c4386eSCy Schubert
run_thread(thread_t * t,void (* f)(void))21*e0c4386eSCy Schubert static int run_thread(thread_t *t, void (*f)(void))
22*e0c4386eSCy Schubert {
23*e0c4386eSCy Schubert f();
24*e0c4386eSCy Schubert return 1;
25*e0c4386eSCy Schubert }
26*e0c4386eSCy Schubert
wait_for_thread(thread_t thread)27*e0c4386eSCy Schubert static int wait_for_thread(thread_t thread)
28*e0c4386eSCy Schubert {
29*e0c4386eSCy Schubert return 1;
30*e0c4386eSCy Schubert }
31*e0c4386eSCy Schubert
32*e0c4386eSCy Schubert #elif defined(OPENSSL_SYS_WINDOWS)
33*e0c4386eSCy Schubert
34*e0c4386eSCy Schubert typedef HANDLE thread_t;
35*e0c4386eSCy Schubert
thread_run(LPVOID arg)36*e0c4386eSCy Schubert static DWORD WINAPI thread_run(LPVOID arg)
37*e0c4386eSCy Schubert {
38*e0c4386eSCy Schubert void (*f)(void);
39*e0c4386eSCy Schubert
40*e0c4386eSCy Schubert *(void **) (&f) = arg;
41*e0c4386eSCy Schubert
42*e0c4386eSCy Schubert f();
43*e0c4386eSCy Schubert return 0;
44*e0c4386eSCy Schubert }
45*e0c4386eSCy Schubert
run_thread(thread_t * t,void (* f)(void))46*e0c4386eSCy Schubert static int run_thread(thread_t *t, void (*f)(void))
47*e0c4386eSCy Schubert {
48*e0c4386eSCy Schubert *t = CreateThread(NULL, 0, thread_run, *(void **) &f, 0, NULL);
49*e0c4386eSCy Schubert return *t != NULL;
50*e0c4386eSCy Schubert }
51*e0c4386eSCy Schubert
wait_for_thread(thread_t thread)52*e0c4386eSCy Schubert static int wait_for_thread(thread_t thread)
53*e0c4386eSCy Schubert {
54*e0c4386eSCy Schubert return WaitForSingleObject(thread, INFINITE) == 0;
55*e0c4386eSCy Schubert }
56*e0c4386eSCy Schubert
57*e0c4386eSCy Schubert #else
58*e0c4386eSCy Schubert
59*e0c4386eSCy Schubert typedef pthread_t thread_t;
60*e0c4386eSCy Schubert
thread_run(void * arg)61*e0c4386eSCy Schubert static void *thread_run(void *arg)
62*e0c4386eSCy Schubert {
63*e0c4386eSCy Schubert void (*f)(void);
64*e0c4386eSCy Schubert
65*e0c4386eSCy Schubert *(void **) (&f) = arg;
66*e0c4386eSCy Schubert
67*e0c4386eSCy Schubert f();
68*e0c4386eSCy Schubert return NULL;
69*e0c4386eSCy Schubert }
70*e0c4386eSCy Schubert
run_thread(thread_t * t,void (* f)(void))71*e0c4386eSCy Schubert static int run_thread(thread_t *t, void (*f)(void))
72*e0c4386eSCy Schubert {
73*e0c4386eSCy Schubert return pthread_create(t, NULL, thread_run, *(void **) &f) == 0;
74*e0c4386eSCy Schubert }
75*e0c4386eSCy Schubert
wait_for_thread(thread_t thread)76*e0c4386eSCy Schubert static int wait_for_thread(thread_t thread)
77*e0c4386eSCy Schubert {
78*e0c4386eSCy Schubert return pthread_join(thread, NULL) == 0;
79*e0c4386eSCy Schubert }
80*e0c4386eSCy Schubert
81*e0c4386eSCy Schubert #endif
82*e0c4386eSCy Schubert
83