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