]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string/pthread4.cc
Simplify dg-options for tests using pthreads
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / pthread4.cc
1 // 2002-01-23 Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
2 // Adapted from http://gcc.gnu.org/ml/gcc-bugs/2002-01/msg00679.html
3 // which was adapted from pthread1.cc by Mike Lu <MLu@dynamicsoft.com>
4 //
5 // Copyright (C) 2002-2016 Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
21
22 // { dg-do run { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* *-*-cygwin *-*-rtems* *-*-darwin* } }
23 // { dg-options "-pthread" { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* } }
24
25 #include <string>
26 #include <list>
27 #include <pthread.h>
28
29 using namespace std;
30
31 static list<string> foo;
32 static pthread_mutex_t fooLock = PTHREAD_MUTEX_INITIALIZER;
33 static pthread_cond_t fooCondOverflow = PTHREAD_COND_INITIALIZER;
34 static pthread_cond_t fooCondUnderflow = PTHREAD_COND_INITIALIZER;
35 static unsigned max_size = 10;
36 #if defined(__CYGWIN__)
37 static int iters = 10000;
38 #else
39 static int iters = 300000;
40 #endif
41
42 void*
43 produce (void*)
44 {
45 for (int num = 0; num < iters; )
46 {
47 string str ("test string");
48
49 pthread_mutex_lock (&fooLock);
50 while (foo.size () >= max_size)
51 pthread_cond_wait (&fooCondOverflow, &fooLock);
52 foo.push_back (str);
53 num++;
54 if (foo.size () >= (max_size / 2))
55 pthread_cond_signal (&fooCondUnderflow);
56 pthread_mutex_unlock (&fooLock);
57 }
58
59 // No more data will ever be written, ensure no fini race
60 pthread_mutex_lock (&fooLock);
61 pthread_cond_signal (&fooCondUnderflow);
62 pthread_mutex_unlock (&fooLock);
63
64 return 0;
65 }
66
67 void*
68 consume (void*)
69 {
70 for (int num = 0; num < iters; )
71 {
72 pthread_mutex_lock (&fooLock);
73 while (foo.size () == 0)
74 pthread_cond_wait (&fooCondUnderflow, &fooLock);
75 while (foo.size () > 0)
76 {
77 string str = foo.back ();
78 foo.pop_back ();
79 num++;
80 }
81 pthread_cond_signal (&fooCondOverflow);
82 pthread_mutex_unlock (&fooLock);
83 }
84
85 return 0;
86 }
87
88 int
89 main (void)
90 {
91 #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
92 pthread_setconcurrency (2);
93 #endif
94
95 pthread_t prod;
96 pthread_create (&prod, 0, produce, 0);
97 pthread_t cons;
98 pthread_create (&cons, 0, consume, 0);
99
100 pthread_join (prod, 0);
101 pthread_join (cons, 0);
102
103 return 0;
104 }