drd: Add a consistency check
[valgrind.git] / drd / tests / std_thread.cpp
blob9275c3abfc7ffd294f40b66e15624862ff755528
1 // Test whether no race conditions are reported on std::thread. Note: since
2 // the implementation of std::thread uses the shared pointer implementation,
3 // that implementation has to be annotated in order to avoid false positives.
4 // See also http://gcc.gnu.org/onlinedocs/libstdc++/manual/debug.html for more
5 // information.
7 #include "../../drd/drd.h"
8 #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(addr) \
9 ANNOTATE_HAPPENS_BEFORE(addr)
10 #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(addr) \
11 ANNOTATE_HAPPENS_AFTER(addr)
13 #include <iostream>
14 #include <thread>
16 int main(int argc, char** argv)
18 std::thread t( []() { } );
19 t.join();
20 std::cerr << "Done.\n";
21 return 0;
25 // From libstdc++-v3/src/c++11/thread.cc
28 extern "C" void* execute_native_thread_routine(void* __p)
30 std::thread::_Impl_base* __t = static_cast<std::thread::_Impl_base*>(__p);
31 std::thread::__shared_base_type __local;
32 __local.swap(__t->_M_this_ptr);
34 __try {
35 __t->_M_run();
36 } __catch(const __cxxabiv1::__forced_unwind&) {
37 __throw_exception_again;
38 } __catch(...) {
39 std::terminate();
42 return 0;
45 #include <system_error>
47 namespace std
49 void thread::_M_start_thread(__shared_base_type __b)
51 if (!__gthread_active_p())
52 #if __EXCEPTIONS
53 throw system_error(make_error_code(errc::operation_not_permitted),
54 "Enable multithreading to use std::thread");
55 #else
56 __throw_system_error(int(errc::operation_not_permitted));
57 #endif
59 __b->_M_this_ptr = __b;
60 int __e = __gthread_create(&_M_id._M_thread, execute_native_thread_routine,
61 __b.get());
62 if (__e) {
63 __b->_M_this_ptr.reset();
64 __throw_system_error(__e);