1 //===-- sanitizer_pthread_wrappers.h ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
16 //===----------------------------------------------------------------------===//
18 #ifndef SANITIZER_PTHREAD_WRAPPERS_H
19 #define SANITIZER_PTHREAD_WRAPPERS_H
21 #include "sanitizer_test_utils.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))
29 typedef HANDLE pthread_t;
31 struct PthreadHelperCreateThreadInfo {
32 void *(*start_routine)(void *);
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);
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;
50 *thread = CreateThread(0, 0, PthreadHelperThreadProc, data, 0, 0);
51 ASSERT_NE(nullptr, *thread) << "Failed to create a thread.";
54 inline void PTHREAD_JOIN(pthread_t thread, void **value_ptr) {
55 ASSERT_EQ(0, value_ptr) << "Nonzero value_ptr is not supported yet.";
56 ASSERT_EQ(WAIT_OBJECT_0, WaitForSingleObject(thread, INFINITE));
57 ASSERT_NE(0, CloseHandle(thread));
60 inline void pthread_exit(void *retval) {
61 ASSERT_EQ(0, retval) << "Nonzero retval is not supported yet.";
62 ExitThread((DWORD)retval);
66 #endif // SANITIZER_PTHREAD_WRAPPERS_H