Debian Codegraph @

libstdc++-v3/testsuite/27_io/basic_syncbuf/sync_ops/1.cc

raw
1 // Copyright (C) 2020-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 // { dg-do run { target c++20 } }
19 // { dg-require-effective-target cxx11_abi }
20 // { dg-require-gthreads "" }
21 // { dg-add-options libatomic }
22 // { dg-additional-options "-pthread" { target pthread } }
23
24 #include <algorithm>
25 #include <atomic>
26 #include <chrono>
27 #include <sstream>
28 #include <string>
29 #include <string_view>
30 #include <syncstream>
31 #include <thread>
32 #include <vector>
33 #include <unordered_map>
34 #include <utility>
35
36 #include <testsuite_hooks.h>
37
38 int
39 main()
40 {
41 using namespace std::chrono_literals;
42
43 std::stringbuf b;
44 std::atomic<unsigned> running(0);
45
46 auto const cstr = "This is a test";
47
48 constexpr int ct = 1000;
49 auto const body = [&]{
50 ++running;
51 auto tid = std::this_thread::get_id();
52 std::syncbuf s(&b);
53 for (auto i = 0; i < ct; ++i)
54 {
55 std::stringstream stm;
56 stm << tid << ' ' << cstr << ' ' << i << std::endl;
57 auto sv = stm.view();
58 s.sputn(sv.data(), sv.size());
59 VERIFY( s.emit() );
60 }
61 };
62
63 const auto tct = 8;
64 std::vector<std::thread> ts;
65 ts.reserve(tct);
66
67 for (auto i = 0; i < tct; ++i)
68 ts.emplace_back(std::thread(body));
69
70 do
71 {
72 std::this_thread::sleep_for(100ms);
73 }
74 while (running.load() < tct);
75
76 std::unordered_map<std::string, int> tids;
77 for (auto&& t : ts)
78 {
79 std::stringstream stm;
80 stm << t.get_id();
81 tids.emplace(std::make_pair(stm.str(), 0));
82 };
83
84 for (auto&& t : ts)
85 t.join();
86
87 std::vector<std::string_view> lines;
88 const auto lct = ct * ts.size();
89 lines.reserve(lct);
90
91 std::size_t last = 0;
92 auto sv = b.view();
93 auto p = sv.find('\n');
94 while (p != std::string_view::npos)
95 {
96 lines.emplace_back(sv.substr(last, p - last));
97 last = p+1;
98 p = sv.find('\n', last);
99 }
100 VERIFY( lines.size() == lct );
101
102 auto sep = "";
103 auto i = 0;
104 sv = std::string_view(cstr);
105
106 for (auto&& l : lines)
107 {
108 auto p = l.find(' ');
109 VERIFY( p != std::string_view::npos );
110 std::string tid(l.substr(0, p));
111 ++p;
112
113 VERIFY( l.substr(p, sv.size()) == sv );
114 std::string s(l.substr(++p + sv.size()));
115 std::stringstream stm(s);
116 int n;
117 stm >> n;
118 VERIFY( stm.eof() );
119 VERIFY( n >= 0 && n < ct );
120 auto it = tids.find(tid);
121 VERIFY( it != std::end(tids) );
122 ++(it->second);
123 }
124
125 for (auto const& t : tids)
126 {
127 VERIFY( t.second == ct );
128 }
129 return 0;
130 }