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