]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/shared_ptr/thread/default_weaktoshared.cc
testsuite_hooks.h: Rewrite VERIFY in terms of __builtin_printf and __builtin_abort.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / thread / default_weaktoshared.cc
1 // Copyright (C) 2006-2016 Free Software Foundation, Inc.
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 3, 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 COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
19
20 // { dg-do run { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* *-*-cygwin *-*-rtems* *-*-darwin* } }
21 // { dg-options "-pthread" { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* } }
22 // { dg-require-effective-target c++11 }
23
24 #include <memory>
25 #include <random>
26 #include <vector>
27 #include <iostream>
28 #include <cstdlib>
29 #include <thread>
30 #include <atomic>
31 #include <testsuite_hooks.h>
32
33 #ifdef _GLIBCXX_HAVE_UNISTD_H
34 #include <unistd.h> // To test for _POSIX_THREAD_PRIORITY_SCHEDULING
35 #endif
36
37 /* This (brute-force) tests the atomicity and thus thread safety of the
38 * shared_ptr <- weak_ptr
39 * assignment operation by allocating a test object, retrieving a weak
40 * reference to it, and letting a number of threads repeatedly create strong
41 * references from the weak reference.
42 * Specifically, this tests the function _Sp_counted_base<true>::add_ref_lock()
43 */
44
45
46 const unsigned int HAMMER_MAX_THREADS = 10;
47 const unsigned int POOL_SIZE = 1000;
48 const unsigned long HAMMER_REPEAT = 100000;
49 const unsigned long KILL_ONE_IN = 1000;
50
51 struct A
52 {
53 static std::atomic<int> counter;
54 A() { counter.fetch_add(1, std::memory_order_relaxed); }
55 ~A() { counter.fetch_sub(1, std::memory_order_relaxed); }
56 };
57
58 std::atomic<int> A::counter{ 0 };
59
60 typedef std::shared_ptr<A> sp_A_t;
61 typedef std::weak_ptr<A> wp_A_t;
62
63 typedef std::vector<sp_A_t> sp_vector_t;
64 typedef std::vector<wp_A_t> wp_vector_t;
65
66 struct shared_and_weak_pools
67 {
68 sp_vector_t& shared_pool;
69 wp_vector_t& weak_pool;
70
71 shared_and_weak_pools(sp_vector_t& _shared_pool, wp_vector_t& _weak_pool)
72 : shared_pool(_shared_pool), weak_pool(_weak_pool)
73 { }
74 };
75
76 void thread_hammer_and_kill(shared_and_weak_pools& pools)
77 {
78 std::mt19937 urbg;
79 std::uniform_int_distribution<> dist(0, KILL_ONE_IN - 1);
80
81 sp_vector_t::iterator cur_shared = pools.shared_pool.begin();
82 wp_vector_t::iterator cur_weak = pools.weak_pool.begin();
83
84 for (unsigned int i = 0; i < HAMMER_REPEAT; ++i)
85 {
86 try
87 {
88 sp_A_t strong(*cur_weak);
89 }
90 catch (std::bad_weak_ptr& exception)
91 {
92 ++cur_weak;
93 if (cur_weak == pools.weak_pool.end())
94 break;
95 }
96
97 if (dist(urbg) == 0)
98 {
99 cur_shared->reset();
100 ++cur_shared;
101 }
102 }
103 }
104
105 void thread_hammer(wp_vector_t& weak_pool)
106 {
107 wp_vector_t::iterator cur_weak = weak_pool.begin();
108
109 for (unsigned int i = 0; i < HAMMER_REPEAT; ++i)
110 {
111 try
112 {
113 sp_A_t strong(*cur_weak);
114 }
115 catch (std::bad_weak_ptr& exception)
116 {
117 ++cur_weak;
118 if (cur_weak == weak_pool.end())
119 break;
120 }
121 }
122 }
123
124 void
125 test01()
126 {
127 sp_vector_t obj_pool(POOL_SIZE);
128
129 for(auto& obj : obj_pool)
130 obj.reset(new A);
131
132 // Obtain weak references.
133 std::vector<wp_vector_t> weak_pool(HAMMER_MAX_THREADS, wp_vector_t(obj_pool.begin(), obj_pool.end()));
134
135 // Launch threads with pointer to weak reference.
136 std::thread threads[HAMMER_MAX_THREADS];
137 #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
138 pthread_setconcurrency (HAMMER_MAX_THREADS);
139 #endif
140
141 shared_and_weak_pools pools(obj_pool, weak_pool[0]);
142 threads[0] = std::thread(thread_hammer_and_kill, std::ref(pools));
143 for (unsigned int worker = 1; worker < HAMMER_MAX_THREADS; worker++)
144 threads[worker] = std::thread(thread_hammer, std::ref(weak_pool[worker]));
145
146 // Wait for threads to complete, then check integrity of reference.
147 for (auto& thread : threads)
148 thread.join();
149
150 obj_pool.clear();
151
152 VERIFY( A::counter == 0 );
153 }
154
155 int
156 main()
157 {
158 test01();
159 return 0;
160 }