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