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