xref: /openbsd-src/gnu/llvm/compiler-rt/lib/sanitizer_common/tests/sanitizer_pthread_wrappers.h (revision 1f9cb04fc6f537ca6cf5a53c28927340cba218a2)
1 //===-- sanitizer_pthread_wrappers.h ----------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of *Sanitizer runtime.
10 // It provides handy wrappers for thread manipulation, that:
11 //  a) assert on any failure rather than returning an error code
12 //  b) defines pthread-like interface on platforms where where <pthread.h>
13 //     is not supplied by default.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef SANITIZER_PTHREAD_WRAPPERS_H
18 #define SANITIZER_PTHREAD_WRAPPERS_H
19 
20 #include "sanitizer_test_utils.h"
21 
22 #if !defined(_WIN32)
23 # include <pthread.h>
24 // Simply forward the arguments and check that the pthread functions succeed.
25 # define PTHREAD_CREATE(a, b, c, d) ASSERT_EQ(0, pthread_create(a, b, c, d))
26 # define PTHREAD_JOIN(a, b) ASSERT_EQ(0, pthread_join(a, b))
27 #else
28 typedef HANDLE pthread_t;
29 
30 struct PthreadHelperCreateThreadInfo {
31   void *(*start_routine)(void *);
32   void *arg;
33 };
34 
PthreadHelperThreadProc(void * arg)35 inline DWORD WINAPI PthreadHelperThreadProc(void *arg) {
36   PthreadHelperCreateThreadInfo *start_data =
37       reinterpret_cast<PthreadHelperCreateThreadInfo*>(arg);
38   (start_data->start_routine)(start_data->arg);
39   delete start_data;
40   return 0;
41 }
42 
PTHREAD_CREATE(pthread_t * thread,void * attr,void * (* start_routine)(void *),void * arg)43 inline void PTHREAD_CREATE(pthread_t *thread, void *attr,
44                            void *(*start_routine)(void *), void *arg) {
45   ASSERT_EQ(0, attr) << "Thread attributes are not supported yet.";
46   PthreadHelperCreateThreadInfo *data = new PthreadHelperCreateThreadInfo;
47   data->start_routine = start_routine;
48   data->arg = arg;
49   *thread = CreateThread(0, 0, PthreadHelperThreadProc, data, 0, 0);
50   DWORD err = GetLastError();
51   ASSERT_NE(nullptr, *thread) << "Failed to create a thread, got error 0x"
52                               << std::hex << err;
53 }
54 
PTHREAD_JOIN(pthread_t thread,void ** value_ptr)55 inline void PTHREAD_JOIN(pthread_t thread, void **value_ptr) {
56   ASSERT_EQ(0, value_ptr) << "Nonzero value_ptr is not supported yet.";
57   ASSERT_EQ(WAIT_OBJECT_0, WaitForSingleObject(thread, INFINITE));
58   ASSERT_NE(0, CloseHandle(thread));
59 }
60 
pthread_exit(void * retval)61 inline void pthread_exit(void *retval) {
62   ASSERT_EQ(0, retval) << "Nonzero retval is not supported yet.";
63   ExitThread(0);
64 }
65 #endif  // _WIN32
66 
67 #endif  // SANITIZER_PTHREAD_WRAPPERS_H
68