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