]> 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-2018 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-options "-pthread" }
22 // { dg-require-effective-target c++11 }
23 // { dg-require-effective-target pthread }
24
25 #include <memory>
26 #include <random>
27 #include <vector>
28 #include <iostream>
29 #include <cstdlib>
30 #include <thread>
31 #include <atomic>
32 #include <functional>
33 #include <testsuite_hooks.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
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;
52
53 struct A
54 {
55 static std::atomic<int> counter;
56 A() { counter.fetch_add(1, std::memory_order_relaxed); }
57 ~A() { counter.fetch_sub(1, std::memory_order_relaxed); }
58 };
59
60 std::atomic<int> A::counter{ 0 };
61
62 typedef std::shared_ptr<A> sp_A_t;
63 typedef std::weak_ptr<A> wp_A_t;
64
65 typedef std::vector<sp_A_t> sp_vector_t;
66 typedef std::vector<wp_A_t> wp_vector_t;
67
68 struct shared_and_weak_pools
69 {
70 sp_vector_t& shared_pool;
71 wp_vector_t& weak_pool;
72
73 shared_and_weak_pools(sp_vector_t& _shared_pool, wp_vector_t& _weak_pool)
74 : shared_pool(_shared_pool), weak_pool(_weak_pool)
75 { }
76 };
77
78 void thread_hammer_and_kill(shared_and_weak_pools& pools)
79 {
80 std::mt19937 urbg;
81 std::uniform_int_distribution<> dist(0, KILL_ONE_IN - 1);
82
83 sp_vector_t::iterator cur_shared = pools.shared_pool.begin();
84 wp_vector_t::iterator cur_weak = pools.weak_pool.begin();
85
86 for (unsigned int i = 0; i < HAMMER_REPEAT; ++i)
87 {
88 try
89 {
90 sp_A_t strong(*cur_weak);
91 }
92 catch (std::bad_weak_ptr& exception)
93 {
94 ++cur_weak;
95 if (cur_weak == pools.weak_pool.end())
96 break;
97 }
98
99 if (dist(urbg) == 0)
100 {
101 cur_shared->reset();
102 ++cur_shared;
103 }
104 }
105 }
106
107 void thread_hammer(wp_vector_t& weak_pool)
108 {
109 wp_vector_t::iterator cur_weak = weak_pool.begin();
110
111 for (unsigned int i = 0; i < HAMMER_REPEAT; ++i)
112 {
113 try
114 {
115 sp_A_t strong(*cur_weak);
116 }
117 catch (std::bad_weak_ptr& exception)
118 {
119 ++cur_weak;
120 if (cur_weak == weak_pool.end())
121 break;
122 }
123 }
124 }
125
126 void
127 test01()
128 {
129 sp_vector_t obj_pool(POOL_SIZE);
130
131 for(auto& obj : obj_pool)
132 obj.reset(new A);
133
134 // Obtain weak references.
135 std::vector<wp_vector_t> weak_pool(HAMMER_MAX_THREADS, wp_vector_t(obj_pool.begin(), obj_pool.end()));
136
137 // Launch threads with pointer to weak reference.
138 std::thread threads[HAMMER_MAX_THREADS];
139 #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
140 pthread_setconcurrency (HAMMER_MAX_THREADS);
141 #endif
142
143 shared_and_weak_pools pools(obj_pool, weak_pool[0]);
144 threads[0] = std::thread(thread_hammer_and_kill, std::ref(pools));
145 for (unsigned int worker = 1; worker < HAMMER_MAX_THREADS; worker++)
146 threads[worker] = std::thread(thread_hammer, std::ref(weak_pool[worker]));
147
148 // Wait for threads to complete, then check integrity of reference.
149 for (auto& thread : threads)
150 thread.join();
151
152 obj_pool.clear();
153
154 VERIFY( A::counter == 0 );
155 }
156
157 int
158 main()
159 {
160 test01();
161 return 0;
162 }