1 //===-- sanitizer_pthread_wrappers.h ----------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #ifndef SANITIZER_PTHREAD_WRAPPERS_H
18 #define SANITIZER_PTHREAD_WRAPPERS_H
20 #include "sanitizer_test_utils.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))
28 typedef HANDLE pthread_t
;
30 struct PthreadHelperCreateThreadInfo
{
31 void *(*start_routine
)(void *);
35 inline DWORD WINAPI
PthreadHelperThreadProc(void *arg
) {
36 PthreadHelperCreateThreadInfo
*start_data
=
37 reinterpret_cast<PthreadHelperCreateThreadInfo
*>(arg
);
38 (start_data
->start_routine
)(start_data
->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
;
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"
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
));
61 inline void pthread_exit(void *retval
) {
62 ASSERT_EQ(0, retval
) << "Nonzero retval is not supported yet.";
67 #endif // SANITIZER_PTHREAD_WRAPPERS_H