Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / thread / mutex_weaktoshared.cc
blob20c2da146e0c88b58403ba3de951af8c3af6d66f
1 // Copyright (C) 2006, 2007, 2008 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.
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.
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.
19 // 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
21 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
22 // { dg-options " -std=gnu++0x -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
23 // { dg-options " -std=gnu++0x -pthreads" { target *-*-solaris* } }
24 // { dg-options " -std=gnu++0x " { target *-*-cygwin *-*-darwin* } }
26 #include <memory>
27 #include <random>
28 #include <vector>
29 #include <testsuite_hooks.h>
30 #include <iostream>
31 #include <cstdlib>
33 #include <pthread.h>
35 #ifdef _GLIBCXX_HAVE_UNISTD_H
36 #include <unistd.h> // To test for _POSIX_THREAD_PRIORITY_SCHEDULING
37 #endif
39 /* This (brute-force) tests the atomicity and thus thread safety of the
40 * shared_ptr <- weak_ptr
41 * assignment operation by allocating a test object, retrieving a weak
42 * reference to it, and letting a number of threads repeatedly create strong
43 * references from the weak reference.
44 * Specifically, this tests the function _Sp_counted_base<true>::add_ref_lock()
48 const unsigned int HAMMER_MAX_THREADS = 10;
49 const unsigned int POOL_SIZE = 1000;
50 const unsigned long HAMMER_REPEAT = 100000;
51 const unsigned long KILL_ONE_IN = 1000;
53 struct A
55 static _Atomic_word counter;
56 A()
58 __gnu_cxx::__atomic_add(&counter, 1);
60 ~A()
62 __gnu_cxx::__atomic_add(&counter, -1);
66 _Atomic_word A::counter = 0;
68 using std::_S_mutex;
70 typedef std::__shared_ptr<A, _S_mutex> sp_A_t;
71 typedef std::__weak_ptr<A, _S_mutex> wp_A_t;
73 typedef std::vector<sp_A_t> sp_vector_t;
74 typedef std::vector<wp_A_t> wp_vector_t;
76 struct shared_and_weak_pools
78 sp_vector_t& shared_pool;
79 wp_vector_t& weak_pool;
81 shared_and_weak_pools(sp_vector_t& _shared_pool, wp_vector_t& _weak_pool)
82 : shared_pool(_shared_pool), weak_pool(_weak_pool)
83 { }
86 void* thread_hammer_and_kill(void* opaque_pools)
88 shared_and_weak_pools& pools = *static_cast<shared_and_weak_pools*>(opaque_pools);
89 // Using the same parameters as in the RNG test cases.
90 std::mersenne_twister<
91 unsigned long, 32, 624, 397, 31,
92 0x9908b0dful, 11, 7,
93 0x9d2c5680ul, 15,
94 0xefc60000ul, 18> rng;
96 sp_vector_t::iterator cur_shared = pools.shared_pool.begin();
97 wp_vector_t::iterator cur_weak = pools.weak_pool.begin();
99 for (unsigned int i = 0; i < HAMMER_REPEAT; ++i)
103 sp_A_t strong(*cur_weak);
105 catch (std::bad_weak_ptr& exception)
107 ++cur_weak;
108 if (cur_weak == pools.weak_pool.end())
109 break;
112 if (rng() % KILL_ONE_IN == 0)
114 cur_shared->reset();
115 ++cur_shared;
118 return 0;
121 void* thread_hammer(void* opaque_weak)
123 wp_vector_t& weak_pool = *static_cast<wp_vector_t*>(opaque_weak);
124 // Using the same parameters as in the RNG test cases.
125 std::mersenne_twister<
126 unsigned long, 32, 624, 397, 31,
127 0x9908b0dful, 11, 7,
128 0x9d2c5680ul, 15,
129 0xefc60000ul, 18> rng;
130 wp_vector_t::iterator cur_weak = weak_pool.begin();
132 for (unsigned int i = 0; i < HAMMER_REPEAT; ++i)
136 sp_A_t strong(*cur_weak);
138 catch (std::bad_weak_ptr& exception)
140 ++cur_weak;
141 if (cur_weak == weak_pool.end())
142 break;
145 return 0;
149 test01()
151 bool test __attribute__((unused)) = true;
152 sp_vector_t obj_pool(POOL_SIZE);
154 for(sp_vector_t::iterator cur = obj_pool.begin(); cur != obj_pool.end(); ++cur)
156 cur->reset(new A);
158 // Obtain weak references.
159 std::vector<wp_vector_t> weak_pool(HAMMER_MAX_THREADS, wp_vector_t(obj_pool.begin(), obj_pool.end()));
161 // Launch threads with pointer to weak reference.
162 pthread_t threads[HAMMER_MAX_THREADS];
163 #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
164 pthread_setconcurrency (HAMMER_MAX_THREADS);
165 #endif
167 pthread_attr_t tattr;
168 pthread_attr_init(&tattr);
170 shared_and_weak_pools pools(obj_pool, weak_pool[0]);
171 pthread_create(threads, &tattr, thread_hammer_and_kill, static_cast<void*>(&pools));
172 for (unsigned int worker = 1; worker < HAMMER_MAX_THREADS; worker++)
174 if (pthread_create(&threads[worker], &tattr,
175 thread_hammer, static_cast<void*>(&weak_pool[worker])))
176 std::abort();
178 // Wait for threads to complete, then check integrity of reference.
179 void* status;
180 for (unsigned int worker = 0; worker < HAMMER_MAX_THREADS; worker++)
182 if (pthread_join(threads[worker], &status))
183 std::abort();
185 obj_pool.clear();
187 VERIFY( A::counter == 0 );
189 return 0;
192 int
193 main()
195 test01();
196 return 0;