OSDN Git Service

* testsuite/thread/pthread4.cc: Further tweak to avoid fini race.
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / thread / 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, 2003 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 2, 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 COPYING.  If not, write to the Free
20 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // USA.
22
23 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* } }
24 // { dg-options "-pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* } }
25 // { dg-options "-pthreads" { target *-*-solaris* } }
26
27 #include <string>
28 #include <list>
29
30 // Do not include <pthread.h> explicitly; if threads are properly
31 // configured for the port, then it is picked up free from STL headers.
32
33 #if __GTHREADS
34 using namespace std;
35
36 static list<string> foo;
37 static pthread_mutex_t fooLock = PTHREAD_MUTEX_INITIALIZER;
38 static pthread_cond_t fooCondOverflow = PTHREAD_COND_INITIALIZER;
39 static pthread_cond_t fooCondUnderflow = PTHREAD_COND_INITIALIZER;
40 static unsigned max_size = 10;
41 #if defined(__CYGWIN__)
42 static int iters = 10000;
43 #else
44 static int iters = 300000;
45 #endif
46
47 void*
48 produce (void*)
49 {
50   for (int num = 0; num < iters; )
51     {
52       string str ("test string");
53
54       pthread_mutex_lock (&fooLock);
55       while (foo.size () >= max_size)
56         pthread_cond_wait (&fooCondOverflow, &fooLock);
57       foo.push_back (str);
58       num++;
59       if (foo.size () >= (max_size / 2))
60         pthread_cond_signal (&fooCondUnderflow);
61       pthread_mutex_unlock (&fooLock);
62     }
63
64   // No more data will ever be written, ensure no fini race
65   pthread_mutex_lock (&fooLock);
66   pthread_cond_signal (&fooCondUnderflow);
67   pthread_mutex_unlock (&fooLock);
68
69   return 0;
70 }
71
72 void*
73 consume (void*)
74 {
75   for (int num = 0; num < iters; )
76     {
77       pthread_mutex_lock (&fooLock);
78       while (foo.size () == 0)
79         pthread_cond_wait (&fooCondUnderflow, &fooLock);
80       while (foo.size () > 0)
81         {
82           string str = foo.back ();
83           foo.pop_back ();
84           num++;
85         }
86       pthread_cond_signal (&fooCondOverflow);
87       pthread_mutex_unlock (&fooLock);
88     }
89
90   return 0;
91 }
92
93 int
94 main (void)
95 {
96 #if defined(__sun) && defined(__svr4__)
97   pthread_setconcurrency (2);
98 #endif
99
100   pthread_t prod;
101   pthread_create (&prod, NULL, produce, NULL);
102   pthread_t cons;
103   pthread_create (&cons, NULL, consume, NULL);
104
105   pthread_join (prod, NULL);
106   pthread_join (cons, NULL);
107
108   return 0;
109 }
110 #else
111 int main (void) {}
112 #endif