]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/ext/rope/pthread7-rope.cc
Simplify dg-options for tests using pthreads
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / ext / rope / pthread7-rope.cc
CommitLineData
1976f0d9
LR
1// 2003-05-03 Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
2//
818ab71a 3// Copyright (C) 2003-2016 Free Software Foundation, Inc.
1976f0d9
LR
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
748086b7 8// Free Software Foundation; either version 3, or (at your option)
1976f0d9
LR
9// any later version.
10//
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
748086b7
JJ
17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
1976f0d9 19
37d13ae6 20// { dg-do run { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* *-*-cygwin *-*-rtems* *-*-darwin* } }
71c54f8e 21// { dg-options "-pthread" { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* } }
1976f0d9
LR
22
23#include <ext/rope>
24#include <cstring>
1769232d 25#include <pthread.h>
1976f0d9
LR
26#include <testsuite_hooks.h>
27
1976f0d9
LR
28const int max_thread_count = 4;
29const int max_loop_count = 10000;
30
e3f78d9b
BK
31typedef __gnu_cxx::rope<char, std::allocator<char> > rope_type;
32rope_type foo2;
33rope_type foo4;
1976f0d9 34
11f10e6b 35void* thread_main(void *)
1976f0d9
LR
36{
37 // To see a problem with gcc 3.3 and before, set a break point here.
38 // Single step through c_str implementation, call sched_yield after
39 // capture of NULL __old_c_string in any thread. Single step
40 // through another thread past that same point. Now, one thread
41 // will receive a bad pointer return. Adding dummy sched_yield
42 // should never change program semantics under POSIX threads.
43 const char* data4 = foo4.c_str();
44
45 // Please note that the memory leak in the rope implementation with
46 // this test case, existed before and after fixing this bug...
11f10e6b 47 bool test __attribute__((unused)) = true;
1976f0d9 48 VERIFY( !std::strcmp (data4, "barbazbonglehellohellohello") );
11f10e6b 49 return 0;
1976f0d9
LR
50}
51
52int
53main()
54{
11f10e6b
BK
55 bool test __attribute__((unused)) = true;
56
1976f0d9
LR
57 pthread_t tid[max_thread_count];
58
87bd0274 59#if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
1976f0d9
LR
60 pthread_setconcurrency (max_thread_count);
61#endif
62
e3f78d9b 63 rope_type foo;
1976f0d9
LR
64 foo += "bar";
65 foo += "baz";
66 foo += "bongle";
67 const char* data = foo.c_str();
68 VERIFY( !std::strcmp (data, "barbazbongle") );
69
70 const char* data2;
71 {
1976f0d9
LR
72 foo2 += "bar2";
73 foo2 += "baz2";
74 foo2 += "bongle2";
75 data2 = foo2.c_str();
76 VERIFY( !std::strcmp (data2, "bar2baz2bongle2") );
77 }
78
e3f78d9b 79 rope_type foo3 ("hello");
1976f0d9
LR
80 const char* data3 = foo3.c_str();
81 VERIFY( !std::strcmp (data3, "hello") );
82
83 for (int j = 0; j < max_loop_count; j++)
84 {
85 foo4 = foo;
86 foo4 += foo3;
87 foo4 += foo3;
88 foo4 += foo3;
89
90 for (int i = 0; i < max_thread_count; i++)
8fc81078 91 pthread_create (&tid[i], 0, thread_main, 0);
1976f0d9
LR
92
93 for (int i = 0; i < max_thread_count; i++)
8fc81078 94 pthread_join (tid[i], 0);
1976f0d9
LR
95 }
96
63041e68
PC
97 VERIFY( !std::strcmp (data, "barbazbongle") );
98 VERIFY( !std::strcmp (data2, "bar2baz2bongle2") );
1976f0d9
LR
99
100 return 0;
101}