]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/shared_ptr/thread/default_weaktoshared.cc
re PR testsuite/39696 (gcc.dg/tree-ssa/ssa-ccp-25.c scan-tree-dump doesn't work on...
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / thread / default_weaktoshared.cc
CommitLineData
8e79468d 1// Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation
aaf0ca6f
JW
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// 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
20
21// { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
22// { dg-options "-pthread -std=gnu++0x" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
23// { dg-options "-pthreads -std=gnu++0x" { target *-*-solaris* } }
dbf3a492 24// { dg-options " -std=gnu++0x " { target *-*-cygwin *-*-darwin* } }
aaf0ca6f
JW
25
26#include <memory>
27#include <random>
28#include <vector>
29#include <testsuite_hooks.h>
30#include <iostream>
31#include <cstdlib>
32
33#include <pthread.h>
34
35#ifdef _GLIBCXX_HAVE_UNISTD_H
36#include <unistd.h> // To test for _POSIX_THREAD_PRIORITY_SCHEDULING
37#endif
38
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()
45 */
46
47
48const unsigned int HAMMER_MAX_THREADS = 10;
49const unsigned int POOL_SIZE = 1000;
50const unsigned long HAMMER_REPEAT = 100000;
51const unsigned long KILL_ONE_IN = 1000;
52
53struct A
54 {
55 static _Atomic_word counter;
56 A()
57 {
58 __gnu_cxx::__atomic_add(&counter, 1);
59 }
60 ~A()
61 {
62 __gnu_cxx::__atomic_add(&counter, -1);
63 }
64 };
65
66_Atomic_word A::counter = 0;
67
68typedef std::shared_ptr<A> sp_A_t;
69typedef std::weak_ptr<A> wp_A_t;
70
71typedef std::vector<sp_A_t> sp_vector_t;
72typedef std::vector<wp_A_t> wp_vector_t;
73
74struct shared_and_weak_pools
75{
76 sp_vector_t& shared_pool;
77 wp_vector_t& weak_pool;
78
79 shared_and_weak_pools(sp_vector_t& _shared_pool, wp_vector_t& _weak_pool)
80 : shared_pool(_shared_pool), weak_pool(_weak_pool)
81 { }
82};
83
84void* thread_hammer_and_kill(void* opaque_pools)
85{
86 shared_and_weak_pools& pools = *static_cast<shared_and_weak_pools*>(opaque_pools);
87 // Using the same parameters as in the RNG test cases.
8e79468d 88 std::mersenne_twister_engine<
aaf0ca6f 89 unsigned long, 32, 624, 397, 31,
8e79468d
BK
90 0x9908b0dful, 11,
91 0xfffffffful, 7,
aaf0ca6f 92 0x9d2c5680ul, 15,
8e79468d 93 0xefc60000ul, 18, 1812433253ul> rng;
aaf0ca6f
JW
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::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
120void* thread_hammer(void* opaque_weak)
121{
122 wp_vector_t& weak_pool = *static_cast<wp_vector_t*>(opaque_weak);
123 // Using the same parameters as in the RNG test cases.
8e79468d 124 std::mersenne_twister_engine<
aaf0ca6f 125 unsigned long, 32, 624, 397, 31,
8e79468d
BK
126 0x9908b0dful, 11,
127 0xfffffffful, 7,
aaf0ca6f 128 0x9d2c5680ul, 15,
8e79468d
BK
129 0xefc60000ul, 18, 1812433253ul> rng;
130
aaf0ca6f
JW
131 wp_vector_t::iterator cur_weak = weak_pool.begin();
132
133 for (unsigned int i = 0; i < HAMMER_REPEAT; ++i)
134 {
135 try
136 {
137 sp_A_t strong(*cur_weak);
138 }
139 catch (std::bad_weak_ptr& exception)
140 {
141 ++cur_weak;
142 if (cur_weak == weak_pool.end())
143 break;
144 }
145 }
146 return 0;
147}
148
149int
150test01()
151{
152 bool test __attribute__((unused)) = true;
153 sp_vector_t obj_pool(POOL_SIZE);
154
155 for(sp_vector_t::iterator cur = obj_pool.begin(); cur != obj_pool.end(); ++cur)
156 {
157 cur->reset(new A);
158 }
159 // Obtain weak references.
160 std::vector<wp_vector_t> weak_pool(HAMMER_MAX_THREADS, wp_vector_t(obj_pool.begin(), obj_pool.end()));
161
162 // Launch threads with pointer to weak reference.
163 pthread_t threads[HAMMER_MAX_THREADS];
164#if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
165 pthread_setconcurrency (HAMMER_MAX_THREADS);
166#endif
167
168 pthread_attr_t tattr;
395c9e79 169 pthread_attr_init(&tattr);
aaf0ca6f
JW
170
171 shared_and_weak_pools pools(obj_pool, weak_pool[0]);
172 pthread_create(threads, &tattr, thread_hammer_and_kill, static_cast<void*>(&pools));
173 for (unsigned int worker = 1; worker < HAMMER_MAX_THREADS; worker++)
174 {
175 if (pthread_create(&threads[worker], &tattr,
176 thread_hammer, static_cast<void*>(&weak_pool[worker])))
177 std::abort();
178 }
179 // Wait for threads to complete, then check integrity of reference.
180 void* status;
181 for (unsigned int worker = 0; worker < HAMMER_MAX_THREADS; worker++)
182 {
183 if (pthread_join(threads[worker], &status))
184 std::abort();
185 }
186 obj_pool.clear();
187
188 VERIFY( A::counter == 0 );
189
190 return 0;
191}
192
193int
194main()
195{
196 test01();
197 return 0;
198}