103a78d15Sespie // 2002-01-23  Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
203a78d15Sespie //
303a78d15Sespie // Copyright (C) 2002, 2003 Free Software Foundation, Inc.
403a78d15Sespie //
503a78d15Sespie // This file is part of the GNU ISO C++ Library.  This library is free
603a78d15Sespie // software; you can redistribute it and/or modify it under the
703a78d15Sespie // terms of the GNU General Public License as published by the
803a78d15Sespie // Free Software Foundation; either version 2, or (at your option)
903a78d15Sespie // any later version.
1003a78d15Sespie //
1103a78d15Sespie // This library is distributed in the hope that it will be useful,
1203a78d15Sespie // but WITHOUT ANY WARRANTY; without even the implied warranty of
1303a78d15Sespie // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1403a78d15Sespie // GNU General Public License for more details.
1503a78d15Sespie //
1603a78d15Sespie // You should have received a copy of the GNU General Public License along
1703a78d15Sespie // with this library; see the file COPYING.  If not, write to the Free
1803a78d15Sespie // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1903a78d15Sespie // USA.
2003a78d15Sespie 
21*ae44bc93Skurt // { dg-do run { target *-*-openbsd* *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* } }
22*ae44bc93Skurt // { dg-options "-pthread" { target *-*-openbsd* *-*-freebsd* *-*-netbsd* *-*-linux* } }
2303a78d15Sespie // { dg-options "-pthreads" { target *-*-solaris* } }
2403a78d15Sespie 
2503a78d15Sespie // This multi-threading C++/STL/POSIX code adheres to rules outlined here:
2603a78d15Sespie // http://www.sgi.com/tech/stl/thread_safety.html
2703a78d15Sespie //
2803a78d15Sespie // It is believed to exercise the allocation code in a manner that
2903a78d15Sespie // should reveal memory leaks (and, under rare cases, race conditions,
3003a78d15Sespie // if the STL threading support is fubar'd).
3103a78d15Sespie 
3203a78d15Sespie #include <list>
3303a78d15Sespie 
3403a78d15Sespie // Do not include <pthread.h> explicitly; if threads are properly
3503a78d15Sespie // configured for the port, then it is picked up free from STL headers.
3603a78d15Sespie 
3703a78d15Sespie #if __GTHREADS
3803a78d15Sespie using namespace std;
3903a78d15Sespie 
4003a78d15Sespie const int thread_cycles = 10;
4103a78d15Sespie const int thread_pairs = 10;
4203a78d15Sespie const unsigned max_size = 100;
4303a78d15Sespie const int iters = 10000;
4403a78d15Sespie 
4503a78d15Sespie class task_queue
4603a78d15Sespie {
4703a78d15Sespie public:
task_queue()4803a78d15Sespie   task_queue ()
4903a78d15Sespie   {
5003a78d15Sespie     pthread_mutex_init (&fooLock, NULL);
5103a78d15Sespie     pthread_cond_init (&fooCond1, NULL);
5203a78d15Sespie     pthread_cond_init (&fooCond2, NULL);
5303a78d15Sespie   }
~task_queue()5403a78d15Sespie   ~task_queue ()
5503a78d15Sespie   {
5603a78d15Sespie     pthread_mutex_destroy (&fooLock);
5703a78d15Sespie     pthread_cond_destroy (&fooCond1);
5803a78d15Sespie     pthread_cond_destroy (&fooCond2);
5903a78d15Sespie   }
6003a78d15Sespie   list<int> foo;
6103a78d15Sespie   pthread_mutex_t fooLock;
6203a78d15Sespie   pthread_cond_t fooCond1;
6303a78d15Sespie   pthread_cond_t fooCond2;
6403a78d15Sespie };
6503a78d15Sespie 
6603a78d15Sespie void*
produce(void * t)6703a78d15Sespie produce (void* t)
6803a78d15Sespie {
6903a78d15Sespie   task_queue& tq = *(static_cast<task_queue*> (t));
7003a78d15Sespie   int num = 0;
7103a78d15Sespie   while (num < iters)
7203a78d15Sespie     {
7303a78d15Sespie       pthread_mutex_lock (&tq.fooLock);
7403a78d15Sespie       while (tq.foo.size () >= max_size)
7503a78d15Sespie 	pthread_cond_wait (&tq.fooCond1, &tq.fooLock);
7603a78d15Sespie       tq.foo.push_back (num++);
7703a78d15Sespie       pthread_cond_signal (&tq.fooCond2);
7803a78d15Sespie       pthread_mutex_unlock (&tq.fooLock);
7903a78d15Sespie     }
8003a78d15Sespie   return 0;
8103a78d15Sespie }
8203a78d15Sespie 
8303a78d15Sespie void*
consume(void * t)8403a78d15Sespie consume (void* t)
8503a78d15Sespie {
8603a78d15Sespie   task_queue& tq = *(static_cast<task_queue*> (t));
8703a78d15Sespie   int num = 0;
8803a78d15Sespie   while (num < iters)
8903a78d15Sespie     {
9003a78d15Sespie       pthread_mutex_lock (&tq.fooLock);
9103a78d15Sespie       while (tq.foo.size () == 0)
9203a78d15Sespie 	pthread_cond_wait (&tq.fooCond2, &tq.fooLock);
9303a78d15Sespie       if (tq.foo.front () != num++)
9403a78d15Sespie 	abort ();
9503a78d15Sespie       tq.foo.pop_front ();
9603a78d15Sespie       pthread_cond_signal (&tq.fooCond1);
9703a78d15Sespie       pthread_mutex_unlock (&tq.fooLock);
9803a78d15Sespie     }
9903a78d15Sespie   return 0;
10003a78d15Sespie }
10103a78d15Sespie 
10203a78d15Sespie int
main(int argc,char ** argv)10303a78d15Sespie main (int argc, char** argv)
10403a78d15Sespie {
10503a78d15Sespie   pthread_t prod[thread_pairs];
10603a78d15Sespie   pthread_t cons[thread_pairs];
10703a78d15Sespie 
10803a78d15Sespie   task_queue* tq[thread_pairs];
10903a78d15Sespie 
11003a78d15Sespie #if defined(__sun) && defined(__svr4__)
11103a78d15Sespie   pthread_setconcurrency (thread_pairs * 2);
11203a78d15Sespie #endif
11303a78d15Sespie 
11403a78d15Sespie   for (int j = 0; j < thread_cycles; j++)
11503a78d15Sespie     {
11603a78d15Sespie       for (int i = 0; i < thread_pairs; i++)
11703a78d15Sespie 	{
11803a78d15Sespie 	  tq[i] = new task_queue;
11903a78d15Sespie 	  pthread_create (&prod[i], NULL, produce, static_cast<void*> (tq[i]));
12003a78d15Sespie 	  pthread_create (&cons[i], NULL, consume, static_cast<void*> (tq[i]));
12103a78d15Sespie 	}
12203a78d15Sespie 
12303a78d15Sespie       for (int i = 0; i < thread_pairs; i++)
12403a78d15Sespie 	{
12503a78d15Sespie 	  pthread_join (prod[i], NULL);
12603a78d15Sespie 	  pthread_join (cons[i], NULL);
12703a78d15Sespie #if defined(__FreeBSD__) && __FreeBSD__ < 5
12803a78d15Sespie 	  // These lines are not required by POSIX since a successful
12903a78d15Sespie 	  // join is suppose to detach as well...
13003a78d15Sespie 	  pthread_detach (prod[i]);
13103a78d15Sespie 	  pthread_detach (cons[i]);
13203a78d15Sespie 	  // ...but they are according to the FreeBSD 4.X code base
13303a78d15Sespie 	  // or else you get a memory leak.
13403a78d15Sespie #endif
13503a78d15Sespie 	  delete tq[i];
13603a78d15Sespie 	}
13703a78d15Sespie     }
13803a78d15Sespie 
13903a78d15Sespie   return 0;
14003a78d15Sespie }
14103a78d15Sespie #else
main(void)14203a78d15Sespie int main (void) {}
14303a78d15Sespie #endif
144