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