2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / thread / pthread7-rope.cc
blob36acd461e8733c274235e1d810b416c0840f82aa
1 // 2003-05-03 Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
2 //
3 // Copyright (C) 2003 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* } }
22 // { dg-options "-D_GLIBCXX_ASSERT -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* } }
23 // { dg-options "-D_GLIBCXX_ASSERT -pthreads" { target *-*-solaris* } }
25 #include <ext/rope>
26 #include <cstring>
27 #include <testsuite_hooks.h>
29 // Do not include <pthread.h> explicitly; if threads are properly
30 // configured for the port, then it is picked up free from STL headers.
32 #if __GTHREADS
34 const int max_thread_count = 4;
35 const int max_loop_count = 10000;
37 __gnu_cxx::crope foo4;
39 void* thread_main(void *)
41 // To see a problem with gcc 3.3 and before, set a break point here.
42 // Single step through c_str implementation, call sched_yield after
43 // capture of NULL __old_c_string in any thread. Single step
44 // through another thread past that same point. Now, one thread
45 // will receive a bad pointer return. Adding dummy sched_yield
46 // should never change program semantics under POSIX threads.
47 const char* data4 = foo4.c_str();
49 // Please note that the memory leak in the rope implementation with
50 // this test case, existed before and after fixing this bug...
51 bool test __attribute__((unused)) = true;
52 VERIFY( !std::strcmp (data4, "barbazbonglehellohellohello") );
53 return 0;
56 int
57 main()
59 bool test __attribute__((unused)) = true;
61 pthread_t tid[max_thread_count];
63 #if defined(__sun) && defined(__svr4__)
64 pthread_setconcurrency (max_thread_count);
65 #endif
67 __gnu_cxx::crope foo;
68 foo += "bar";
69 foo += "baz";
70 foo += "bongle";
71 const char* data = foo.c_str();
72 VERIFY( !std::strcmp (data, "barbazbongle") );
74 const char* data2;
76 __gnu_cxx::crope foo2;
77 foo2 += "bar2";
78 foo2 += "baz2";
79 foo2 += "bongle2";
80 data2 = foo2.c_str();
81 VERIFY( !std::strcmp (data2, "bar2baz2bongle2") );
84 __gnu_cxx::crope foo3 ("hello");
85 const char* data3 = foo3.c_str();
86 VERIFY( !std::strcmp (data3, "hello") );
88 for (int j = 0; j < max_loop_count; j++)
90 foo4 = foo;
91 foo4 += foo3;
92 foo4 += foo3;
93 foo4 += foo3;
95 for (int i = 0; i < max_thread_count; i++)
96 pthread_create (&tid[i], NULL, thread_main, 0);
98 for (int i = 0; i < max_thread_count; i++)
99 pthread_join (tid[i], NULL);
102 // Nothing says the data will be trashed at this point...
103 VERIFY( std::strcmp (data2, "bar2baz2bongle2") );
105 return 0;
107 #else
108 int main (void) {}
109 #endif