Add an UNSPEC_PROLOGUE_USE to prevent the link register from being considered dead.
[official-gcc.git] / libstdc++-v3 / testsuite / thread / pthread4.cc
blobb2b1c2e747f625dc0bba0f42f64fe244ff56e5d1
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 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.
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.
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.
23 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin } }
24 // { dg-options "-pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* } }
25 // { dg-options "-pthreads" { target *-*-solaris* } }
27 #include <string>
28 #include <list>
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.
33 #if __GTHREADS
34 using namespace std;
36 static list<string> foo;
37 static pthread_mutex_t fooLock = PTHREAD_MUTEX_INITIALIZER;
38 static unsigned max_size = 10;
39 #if defined(__CYGWIN__)
40 static int iters = 10000;
41 #else
42 static int iters = 300000;
43 #endif
45 void*
46 produce (void*)
48 for (int num = 0; num < iters; )
50 string str ("test string");
52 pthread_mutex_lock (&fooLock);
53 if (foo.size () < max_size)
55 foo.push_back (str);
56 num++;
58 pthread_mutex_unlock (&fooLock);
61 return 0;
64 void*
65 consume (void*)
67 for (int num = 0; num < iters; )
69 pthread_mutex_lock (&fooLock);
70 while (foo.size () > 0)
72 string str = foo.back ();
73 foo.pop_back ();
74 num++;
76 pthread_mutex_unlock (&fooLock);
79 return 0;
82 int
83 main (void)
85 #if defined(__sun) && defined(__svr4__)
86 pthread_setconcurrency (2);
87 #endif
89 pthread_t prod;
90 pthread_create (&prod, NULL, produce, NULL);
91 pthread_t cons;
92 pthread_create (&cons, NULL, consume, NULL);
94 pthread_join (prod, NULL);
95 pthread_join (cons, NULL);
97 return 0;
99 #else
100 int main (void) {}
101 #endif