blob: 5a4aff929476659c4a47c57af60555700befd00b [file] [log] [blame]
barta6f70d52014-06-09 09:01:46 +00001// 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
17static int i;
18
19int 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 Hughesa0664b92017-04-18 17:46:52 -070029#if defined(__GNUC__) && __GNUC__ -0 < 6
barta6f70d52014-06-09 09:01:46 +000030//
31// From libstdc++-v3/src/c++11/thread.cc
32//
33
Elliott Hughesa0664b92017-04-18 17:46:52 -070034extern "C" void* _v_execute_native_thread_routine(void* __p)
barta6f70d52014-06-09 09:01:46 +000035{
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
53namespace 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 Hughesa0664b92017-04-18 17:46:52 -070066 int __e = __gthread_create(&_M_id._M_thread, _v_execute_native_thread_routine,
barta6f70d52014-06-09 09:01:46 +000067 __b.get());
68 if (__e) {
69 __b->_M_this_ptr.reset();
70 __throw_system_error(__e);
71 }
72 }
73}
Elliott Hughesa0664b92017-04-18 17:46:52 -070074#endif