| 1 | // 2003-05-03 Loren J. Rittle <[email protected]> <[email protected]> |
| 2 | // |
| 3 | // Copyright (C) 2003-2025 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 3, or (at your option) |
| 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 |
| 17 | // with this library; see the file COPYING3. If not see |
| 18 | // <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | // { dg-do run } |
| 21 | // { dg-options "-pthread" } |
| 22 | // { dg-require-effective-target pthread } |
| 23 | // { dg-timeout-factor 2 } |
| 24 | |
| 25 | #include <ext/rope> |
| 26 | #include <cstring> |
| 27 | #include <pthread.h> |
| 28 | #include <testsuite_hooks.h> |
| 29 | |
| 30 | const int max_thread_count = 4; |
| 31 | const int max_loop_count = 10000; |
| 32 | |
| 33 | typedef __gnu_cxx::rope<char, std::allocator<char> > rope_type; |
| 34 | rope_type foo2; |
| 35 | rope_type foo4; |
| 36 | |
| 37 | void* thread_main(void *) |
| 38 | { |
| 39 | // To see a problem with gcc 3.3 and before, set a break point here. |
| 40 | // Single step through c_str implementation, call sched_yield after |
| 41 | // capture of NULL __old_c_string in any thread. Single step |
| 42 | // through another thread past that same point. Now, one thread |
| 43 | // will receive a bad pointer return. Adding dummy sched_yield |
| 44 | // should never change program semantics under POSIX threads. |
| 45 | const char* data4 = foo4.c_str(); |
| 46 | |
| 47 | // Please note that the memory leak in the rope implementation with |
| 48 | // this test case, existed before and after fixing this bug... |
| 49 | VERIFY( !std::strcmp (data4, "barbazbonglehellohellohello") ); |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | int |
| 54 | main() |
| 55 | { |
| 56 | pthread_t tid[max_thread_count]; |
| 57 | |
| 58 | #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500 |
| 59 | pthread_setconcurrency (max_thread_count); |
| 60 | #endif |
| 61 | |
| 62 | rope_type foo; |
| 63 | foo += "bar"; |
| 64 | foo += "baz"; |
| 65 | foo += "bongle"; |
| 66 | const char* data = foo.c_str(); |
| 67 | VERIFY( !std::strcmp (data, "barbazbongle") ); |
| 68 | |
| 69 | const char* data2; |
| 70 | { |
| 71 | foo2 += "bar2"; |
| 72 | foo2 += "baz2"; |
| 73 | foo2 += "bongle2"; |
| 74 | data2 = foo2.c_str(); |
| 75 | VERIFY( !std::strcmp (data2, "bar2baz2bongle2") ); |
| 76 | } |
| 77 | |
| 78 | rope_type foo3 ("hello"); |
| 79 | const char* data3 = foo3.c_str(); |
| 80 | VERIFY( !std::strcmp (data3, "hello") ); |
| 81 | |
| 82 | for (int j = 0; j < max_loop_count; j++) |
| 83 | { |
| 84 | foo4 = foo; |
| 85 | foo4 += foo3; |
| 86 | foo4 += foo3; |
| 87 | foo4 += foo3; |
| 88 | |
| 89 | for (int i = 0; i < max_thread_count; i++) |
| 90 | pthread_create (&tid[i], 0, thread_main, 0); |
| 91 | |
| 92 | for (int i = 0; i < max_thread_count; i++) |
| 93 | pthread_join (tid[i], 0); |
| 94 | } |
| 95 | |
| 96 | VERIFY( !std::strcmp (data, "barbazbongle") ); |
| 97 | VERIFY( !std::strcmp (data2, "bar2baz2bongle2") ); |
| 98 | |
| 99 | return 0; |
| 100 | } |