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