bart | f68af88 | 2011-12-10 19:42:05 +0000 | [diff] [blame] | 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. |
| 6 | |
| 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) |
bart | f68af88 | 2011-12-10 19:42:05 +0000 | [diff] [blame] | 12 | |
| 13 | #include <iostream> |
| 14 | #include <thread> |
| 15 | |
| 16 | int main(int argc, char** argv) |
| 17 | { |
| 18 | std::thread t( []() { } ); |
| 19 | t.join(); |
| 20 | std::cerr << "Done.\n"; |
| 21 | return 0; |
| 22 | } |
bart | ac1b70d | 2014-06-07 07:31:56 +0000 | [diff] [blame] | 23 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 24 | #if defined(__GNUC__) && __GNUC__ -0 < 6 |
bart | ac1b70d | 2014-06-07 07:31:56 +0000 | [diff] [blame] | 25 | // |
| 26 | // From libstdc++-v3/src/c++11/thread.cc |
| 27 | // |
| 28 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 29 | extern "C" void* _v_execute_native_thread_routine(void* __p) |
bart | ac1b70d | 2014-06-07 07:31:56 +0000 | [diff] [blame] | 30 | { |
| 31 | std::thread::_Impl_base* __t = static_cast<std::thread::_Impl_base*>(__p); |
| 32 | std::thread::__shared_base_type __local; |
| 33 | __local.swap(__t->_M_this_ptr); |
| 34 | |
| 35 | __try { |
| 36 | __t->_M_run(); |
| 37 | } __catch(const __cxxabiv1::__forced_unwind&) { |
| 38 | __throw_exception_again; |
| 39 | } __catch(...) { |
| 40 | std::terminate(); |
| 41 | } |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
bart | 05a6673 | 2014-06-07 07:48:04 +0000 | [diff] [blame] | 46 | #include <system_error> |
| 47 | |
bart | ac1b70d | 2014-06-07 07:31:56 +0000 | [diff] [blame] | 48 | namespace std |
| 49 | { |
| 50 | void thread::_M_start_thread(__shared_base_type __b) |
| 51 | { |
| 52 | if (!__gthread_active_p()) |
| 53 | #if __EXCEPTIONS |
| 54 | throw system_error(make_error_code(errc::operation_not_permitted), |
| 55 | "Enable multithreading to use std::thread"); |
| 56 | #else |
| 57 | __throw_system_error(int(errc::operation_not_permitted)); |
| 58 | #endif |
| 59 | |
| 60 | __b->_M_this_ptr = __b; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 61 | int __e = __gthread_create(&_M_id._M_thread, _v_execute_native_thread_routine, |
bart | 05a6673 | 2014-06-07 07:48:04 +0000 | [diff] [blame] | 62 | __b.get()); |
bart | ac1b70d | 2014-06-07 07:31:56 +0000 | [diff] [blame] | 63 | if (__e) { |
| 64 | __b->_M_this_ptr.reset(); |
| 65 | __throw_system_error(__e); |
| 66 | } |
| 67 | } |
| 68 | } |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 69 | #endif |