re PR bootstrap/3456 (bootstrapping gcc-3.0 with threadmodel=posix fails on IRIX64...
[gcc.git] / libstdc++-v3 / testsuite / tr1 / 2_general_utilities / shared_ptr / thread / mutex_weaktoshared.cc
1 // Copyright (C) 2006, 2007 Free Software Foundation
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 2, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 // USA.
18
19 // TR1 2.2.2 Template class shared_ptr [tr.util.smartptr.shared]
20
21 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
22 // { dg-options "-pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
23 // { dg-options "-pthreads" { target *-*-solaris* } }
24
25 #include <tr1/memory>
26 #include <tr1/random>
27 #include <vector>
28 #include <testsuite_hooks.h>
29 #include <iostream>
30 #include <cstdlib>
31
32 #include <pthread.h>
33
34 #ifdef _GLIBCXX_HAVE_UNISTD_H
35 #include <unistd.h> // To test for _POSIX_THREAD_PRIORITY_SCHEDULING
36 #endif
37
38 /* This (brute-force) tests the atomicity and thus thread safety of the
39 * shared_ptr <- weak_ptr
40 * assignment operation by allocating a test object, retrieving a weak
41 * reference to it, and letting a number of threads repeatedly create strong
42 * references from the weak reference.
43 * Specifically, this tests the function _Sp_counted_base<true>::add_ref_lock()
44 */
45
46
47 const unsigned int HAMMER_MAX_THREADS = 10;
48 const unsigned int POOL_SIZE = 1000;
49 const unsigned long HAMMER_REPEAT = 100000;
50 const unsigned long KILL_ONE_IN = 1000;
51
52 struct A
53 {
54 static _Atomic_word counter;
55 A()
56 {
57 __gnu_cxx::__atomic_add(&counter, 1);
58 }
59 ~A()
60 {
61 __gnu_cxx::__atomic_add(&counter, -1);
62 }
63 };
64
65 _Atomic_word A::counter = 0;
66
67 using std::tr1::_S_mutex;
68
69 typedef std::tr1::__shared_ptr<A, _S_mutex> sp_A_t;
70 typedef std::tr1::__weak_ptr<A, _S_mutex> wp_A_t;
71
72 typedef std::vector<sp_A_t> sp_vector_t;
73 typedef std::vector<wp_A_t> wp_vector_t;
74
75 struct shared_and_weak_pools
76 {
77 sp_vector_t& shared_pool;
78 wp_vector_t& weak_pool;
79
80 shared_and_weak_pools(sp_vector_t& _shared_pool, wp_vector_t& _weak_pool)
81 : shared_pool(_shared_pool), weak_pool(_weak_pool)
82 { }
83 };
84
85 void* thread_hammer_and_kill(void* opaque_pools)
86 {
87 shared_and_weak_pools& pools = *reinterpret_cast<shared_and_weak_pools*>(opaque_pools);
88 // Using the same parameters as in the RNG test cases.
89 std::tr1::mersenne_twister<
90 unsigned long, 32, 624, 397, 31,
91 0x9908b0dful, 11, 7,
92 0x9d2c5680ul, 15,
93 0xefc60000ul, 18> rng;
94
95 sp_vector_t::iterator cur_shared = pools.shared_pool.begin();
96 wp_vector_t::iterator cur_weak = pools.weak_pool.begin();
97
98 for (unsigned int i = 0; i < HAMMER_REPEAT; ++i)
99 {
100 try
101 {
102 sp_A_t strong(*cur_weak);
103 }
104 catch (std::tr1::bad_weak_ptr& exception)
105 {
106 ++cur_weak;
107 if (cur_weak == pools.weak_pool.end())
108 break;
109 }
110
111 if (rng() % KILL_ONE_IN == 0)
112 {
113 cur_shared->reset();
114 ++cur_shared;
115 }
116 }
117 return 0;
118 }
119
120 void* thread_hammer(void* opaque_weak)
121 {
122 wp_vector_t& weak_pool = *reinterpret_cast<wp_vector_t*>(opaque_weak);
123 // Using the same parameters as in the RNG test cases.
124 std::tr1::mersenne_twister<
125 unsigned long, 32, 624, 397, 31,
126 0x9908b0dful, 11, 7,
127 0x9d2c5680ul, 15,
128 0xefc60000ul, 18> rng;
129 wp_vector_t::iterator cur_weak = weak_pool.begin();
130
131 for (unsigned int i = 0; i < HAMMER_REPEAT; ++i)
132 {
133 try
134 {
135 sp_A_t strong(*cur_weak);
136 }
137 catch (std::tr1::bad_weak_ptr& exception)
138 {
139 ++cur_weak;
140 if (cur_weak == weak_pool.end())
141 break;
142 }
143 }
144 return 0;
145 }
146
147 int
148 test01()
149 {
150 bool test __attribute__((unused)) = true;
151 sp_vector_t obj_pool(POOL_SIZE);
152
153 for(sp_vector_t::iterator cur = obj_pool.begin(); cur != obj_pool.end(); ++cur)
154 {
155 cur->reset(new A);
156 }
157 // Obtain weak references.
158 std::vector<wp_vector_t> weak_pool(HAMMER_MAX_THREADS, wp_vector_t(obj_pool.begin(), obj_pool.end()));
159
160 // Launch threads with pointer to weak reference.
161 pthread_t threads[HAMMER_MAX_THREADS];
162 #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
163 pthread_setconcurrency (HAMMER_MAX_THREADS);
164 #endif
165
166 pthread_attr_t tattr;
167 int ret = pthread_attr_init(&tattr);
168
169 shared_and_weak_pools pools(obj_pool, weak_pool[0]);
170 pthread_create(threads, &tattr, thread_hammer_and_kill, reinterpret_cast<void*>(&pools));
171 for (unsigned int worker = 1; worker < HAMMER_MAX_THREADS; worker++)
172 {
173 if (pthread_create(&threads[worker], &tattr,
174 thread_hammer, reinterpret_cast<void*>(&weak_pool[worker])))
175 std::abort();
176 }
177 // Wait for threads to complete, then check integrity of reference.
178 void* status;
179 for (unsigned int worker = 0; worker < HAMMER_MAX_THREADS; worker++)
180 {
181 if (pthread_join(threads[worker], &status))
182 std::abort();
183 }
184 obj_pool.clear();
185
186 VERIFY( A::counter == 0 );
187
188 return 0;
189 }
190
191 int
192 main()
193 {
194 test01();
195 return 0;
196 }