From ddd69607433a8cd05cf50c524746bea197205b6d Mon Sep 17 00:00:00 2001
From: "Loren J. Rittle" <ljrittle@acm.org>
Date: Thu, 24 Jan 2002 07:35:11 +0000
Subject: [PATCH] pthread1.cc: New test.

	* testsuite/thread/pthread1.cc: New test.
	* testsuite/thread/pthread2.cc: New test adapted from libstdc++/5347.
	* testsuite/thread/pthread3.cc: Likewise.
	* testsuite/thread/pthread4.cc: New test adapted from
	http://gcc.gnu.org/ml/gcc-bugs/2002-01/msg00679.html
	* testsuite/thread/pthread5.cc: New test adapted from libstdc++/5464.
	* testsuite/thread/pthread6.cc: New test adapted from libstdc++/5444.

From-SVN: r49173
---
 libstdc++-v3/ChangeLog                    |  10 ++
 libstdc++-v3/testsuite/thread/pthread1.cc | 143 ++++++++++++++++++++++
 libstdc++-v3/testsuite/thread/pthread2.cc |  67 ++++++++++
 libstdc++-v3/testsuite/thread/pthread3.cc |  64 ++++++++++
 libstdc++-v3/testsuite/thread/pthread4.cc |  97 +++++++++++++++
 libstdc++-v3/testsuite/thread/pthread5.cc | 123 +++++++++++++++++++
 libstdc++-v3/testsuite/thread/pthread6.cc |  94 ++++++++++++++
 7 files changed, 598 insertions(+)
 create mode 100644 libstdc++-v3/testsuite/thread/pthread1.cc
 create mode 100644 libstdc++-v3/testsuite/thread/pthread2.cc
 create mode 100644 libstdc++-v3/testsuite/thread/pthread3.cc
 create mode 100644 libstdc++-v3/testsuite/thread/pthread4.cc
 create mode 100644 libstdc++-v3/testsuite/thread/pthread5.cc
 create mode 100644 libstdc++-v3/testsuite/thread/pthread6.cc

diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index bf0a1fd9cc47..b13f96516974 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,13 @@
+2002-01-23  Loren Rittle <ljrittle@acm.org>
+
+	* testsuite/thread/pthread1.cc: New test.
+	* testsuite/thread/pthread2.cc: New test adapted from libstdc++/5347.
+	* testsuite/thread/pthread3.cc: Likewise.
+	* testsuite/thread/pthread4.cc: New test adapted from
+	http://gcc.gnu.org/ml/gcc-bugs/2002-01/msg00679.html
+	* testsuite/thread/pthread5.cc: New test adapted from libstdc++/5464.
+	* testsuite/thread/pthread6.cc: New test adapted from libstdc++/5444.
+
 2002-01-23  Richard Henderson  <rth@redhat.com>
 
 	PR libstdc++/5198
diff --git a/libstdc++-v3/testsuite/thread/pthread1.cc b/libstdc++-v3/testsuite/thread/pthread1.cc
new file mode 100644
index 000000000000..f51881a90365
--- /dev/null
+++ b/libstdc++-v3/testsuite/thread/pthread1.cc
@@ -0,0 +1,143 @@
+// 2002-01-23  Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
+//
+// Copyright (C) 2002 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// { dg-do run { target *-*-freebsd* *-*-linux* *-*-solaris* } }
+// { dg-options "-pthread" { target *-*-freebsd* *-*-linux* } }
+// { dg-options "-pthreads" { target *-*-solaris* } }
+
+// This multi-threading C++/STL/POSIX code adheres to rules outlined here:
+// http://www.sgi.com/tech/stl/thread_safety.html
+//
+// It is believed to exercise the allocation code in a manner that
+// should reveal memory leaks (and, under rare cases, race conditions,
+// if the STL threading support is fubar'd).
+
+#include <list>
+
+// Do not include <pthread.h> explicitly; if threads are properly
+// configured for the port, then it is picked up free from STL headers.
+
+#if __GTHREADS
+using namespace std;
+
+const int thread_cycles = 10;
+const int thread_pairs = 10;
+const unsigned max_size = 100;
+const int iters = 10000;
+
+class task_queue
+{
+public:
+  task_queue ()
+  {
+    pthread_mutex_init (&fooLock, NULL);
+    pthread_cond_init (&fooCond, NULL);
+  }
+  ~task_queue ()
+  {
+    pthread_mutex_destroy (&fooLock);
+    pthread_cond_destroy (&fooCond);
+  }
+  list<int> foo;
+  pthread_mutex_t fooLock;
+  // This code uses a special case that allows us to use just one
+  // condition variable - in general, don't use this idiom unless you
+  // know what you are doing. ;-)
+  pthread_cond_t fooCond;
+};
+
+void*
+produce (void* t)
+{
+  task_queue& tq = *(static_cast<task_queue*> (t));
+  int num = 0;
+  while (num < iters)
+    {
+      pthread_mutex_lock (&tq.fooLock);
+      while (tq.foo.size () >= max_size)
+	pthread_cond_wait (&tq.fooCond, &tq.fooLock);
+      tq.foo.push_back (num++);
+      pthread_cond_signal (&tq.fooCond);
+      pthread_mutex_unlock (&tq.fooLock);
+    }
+  return 0;
+}
+
+void*
+consume (void* t)
+{
+  task_queue& tq = *(static_cast<task_queue*> (t));
+  int num = 0;
+  while (num < iters)
+    {
+      pthread_mutex_lock (&tq.fooLock);
+      while (tq.foo.size () == 0)
+	pthread_cond_wait (&tq.fooCond, &tq.fooLock);
+      if (tq.foo.front () != num++)
+	abort ();
+      tq.foo.pop_front ();
+      pthread_cond_signal (&tq.fooCond);
+      pthread_mutex_unlock (&tq.fooLock);
+    }
+  return 0;
+}
+
+int
+main (int argc, char** argv)
+{
+  pthread_t prod[thread_pairs];
+  pthread_t cons[thread_pairs];
+
+  task_queue* tq[thread_pairs];
+
+#if defined(__sun) && defined(__svr4__)
+  pthread_setconcurrency (thread_pairs * 2);
+#endif
+
+  for (int j = 0; j < thread_cycles; j++)
+    {
+      for (int i = 0; i < thread_pairs; i++)
+	{
+	  tq[i] = new task_queue;
+	  pthread_create (&prod[i], NULL, produce, static_cast<void*> (tq[i]));
+	  pthread_create (&cons[i], NULL, consume, static_cast<void*> (tq[i]));
+	}
+
+      for (int i = 0; i < thread_pairs; i++)
+	{
+	  pthread_join (prod[i], NULL);
+	  pthread_join (cons[i], NULL);
+#if defined(__FreeBSD__)
+	  // These lines are not required by POSIX since a successful
+	  // join is suppose to detach as well...
+	  pthread_detach (prod[i]);
+	  pthread_detach (cons[i]);
+	  // ...but they are according to the FreeBSD 4.X code base
+	  // or else you get a memory leak.
+#endif
+	  delete tq[i];
+	}
+    }
+
+  return 0;
+}
+#else
+int main (void) {}
+#endif
diff --git a/libstdc++-v3/testsuite/thread/pthread2.cc b/libstdc++-v3/testsuite/thread/pthread2.cc
new file mode 100644
index 000000000000..3ffba23f21d8
--- /dev/null
+++ b/libstdc++-v3/testsuite/thread/pthread2.cc
@@ -0,0 +1,67 @@
+// 2002-01-23  Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
+// Adpated from libstdc++/5347 submitted by markus.breuer@materna.de
+//
+// Copyright (C) 2002 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// { dg-do run { target *-*-freebsd* *-*-linux* *-*-solaris* } }
+// { dg-options "-pthread" { target *-*-freebsd* *-*-linux* } }
+// { dg-options "-pthreads" { target *-*-solaris* } }
+
+#include <fstream>
+
+// Do not include <pthread.h> explicitly; if threads are properly
+// configured for the port, then it is picked up free from STL headers.
+
+#if __GTHREADS
+
+const int max_thread_count = 2;
+const int max_loop_count = 1000000;
+
+void*
+thread_main (void *)
+{
+   for (int i = 0; i < max_loop_count; i++)
+   {
+      std::ofstream* pos1 = new std::ofstream;
+      delete pos1;
+   }
+
+   return 0;
+}
+
+int
+main()
+{
+  pthread_t tid[max_thread_count];
+
+#if defined(__sun) && defined(__svr4__)
+  pthread_setconcurrency (max_thread_count);
+#endif
+
+  for (int i = 0; i < max_thread_count; i++)
+    pthread_create (&tid[i], NULL, thread_main, 0);
+
+  for (int i = 0; i < max_thread_count; i++)
+    pthread_join (tid[i], NULL);
+
+  return 0;
+}
+#else
+int main (void) {}
+#endif
diff --git a/libstdc++-v3/testsuite/thread/pthread3.cc b/libstdc++-v3/testsuite/thread/pthread3.cc
new file mode 100644
index 000000000000..d029f3b5c976
--- /dev/null
+++ b/libstdc++-v3/testsuite/thread/pthread3.cc
@@ -0,0 +1,64 @@
+// 2002-01-23  Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
+// Adpated from libstdc++/5347 submitted by markus.breuer@materna.de
+//
+// Copyright (C) 2002 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// { dg-do run { target *-*-freebsd* *-*-linux* *-*-solaris* } }
+// { dg-options "-pthread" { target *-*-freebsd* *-*-linux* } }
+// { dg-options "-pthreads" { target *-*-solaris* } }
+
+#include <sstream>
+
+// Do not include <pthread.h> explicitly; if threads are properly
+// configured for the port, then it is picked up free from STL headers.
+
+#if __GTHREADS
+
+const int max_thread_count = 2;
+const int max_loop_count = 1000000;
+
+void*
+thread_main (void *)
+{
+   for (int i = 0; i < max_loop_count; i++)
+     std::ostringstream oss;
+
+   return 0;
+}
+
+int
+main()
+{
+  pthread_t tid[max_thread_count];
+
+#if defined(__sun) && defined(__svr4__)
+  pthread_setconcurrency (max_thread_count);
+#endif
+
+  for (int i = 0; i < max_thread_count; i++)
+    pthread_create (&tid[i], NULL, thread_main, 0);
+
+  for (int i = 0; i < max_thread_count; i++)
+    pthread_join (tid[i], NULL);
+
+  return 0;
+}
+#else
+int main (void) {}
+#endif
diff --git a/libstdc++-v3/testsuite/thread/pthread4.cc b/libstdc++-v3/testsuite/thread/pthread4.cc
new file mode 100644
index 000000000000..b6d70c9d775b
--- /dev/null
+++ b/libstdc++-v3/testsuite/thread/pthread4.cc
@@ -0,0 +1,97 @@
+// 2002-01-23  Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
+// Adapted from http://gcc.gnu.org/ml/gcc-bugs/2002-01/msg00679.html
+// which was adapted from pthread1.cc by Mike Lu <MLu@dynamicsoft.com>
+//
+// Copyright (C) 2002 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// { dg-do run { target *-*-freebsd* *-*-linux* *-*-solaris* } }
+// { dg-options "-pthread" { target *-*-freebsd* *-*-linux* } }
+// { dg-options "-pthreads" { target *-*-solaris* } }
+
+#include <string>
+#include <list>
+
+// Do not include <pthread.h> explicitly; if threads are properly
+// configured for the port, then it is picked up free from STL headers.
+
+#if __GTHREADS
+using namespace std;
+
+static list<string> foo;
+static pthread_mutex_t fooLock = PTHREAD_MUTEX_INITIALIZER;
+static unsigned max_size = 10;
+static int iters = 1000000;
+
+void*
+produce (void*)
+{
+  for (int num = 0; num < iters; )
+    {
+      string str ("test string");
+
+      pthread_mutex_lock (&fooLock);
+      if (foo.size () < max_size)
+	{
+	  foo.push_back (str);
+	  num++;
+	}
+      pthread_mutex_unlock (&fooLock);
+    }
+
+  return 0;
+}
+
+void*
+consume (void*)
+{
+  for (int num = 0; num < iters; )
+    {
+      pthread_mutex_lock (&fooLock);
+      while (foo.size () > 0)
+	{
+	  string str = foo.back ();
+	  foo.pop_back ();
+	  num++;
+	}
+      pthread_mutex_unlock (&fooLock);
+    }
+
+  return 0;
+}
+
+int
+main (void)
+{
+#if defined(__sun) && defined(__svr4__)
+  pthread_setconcurrency (2);
+#endif
+
+  pthread_t prod;
+  pthread_create (&prod, NULL, produce, NULL);
+  pthread_t cons;
+  pthread_create (&cons, NULL, consume, NULL);
+
+  pthread_join (prod, NULL);
+  pthread_join (cons, NULL);
+
+  return 0;
+}
+#else
+int main (void) {}
+#endif
diff --git a/libstdc++-v3/testsuite/thread/pthread5.cc b/libstdc++-v3/testsuite/thread/pthread5.cc
new file mode 100644
index 000000000000..fcd491d967ca
--- /dev/null
+++ b/libstdc++-v3/testsuite/thread/pthread5.cc
@@ -0,0 +1,123 @@
+// 2002-01-23  Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
+// Adpated from libstdc++/5464 submitted by jjessel@amadeus.net
+// Jean-Francois JESSEL (Amadeus SAS Development) 
+//
+// Copyright (C) 2002 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// { dg-do run { target *-*-freebsd* *-*-linux* *-*-solaris* } }
+// { dg-options "-pthread" { target *-*-freebsd* *-*-linux* } }
+// { dg-options "-pthreads" { target *-*-solaris* } }
+
+#include <vector>
+#include <list>
+#include <string>
+
+// Do not include <pthread.h> explicitly; if threads are properly
+// configured for the port, then it is picked up free from STL headers.
+
+#if __GTHREADS
+using namespace std;
+
+#define NTHREADS 8
+#define LOOPS 20
+
+struct tt_t
+{
+  char buf[100];
+  int  i;
+};
+
+void*
+thread_function (void* arg)
+{
+  int myid = *(int*) arg;
+  for (int i = 0; i < LOOPS; i++)
+    {
+      vector<tt_t> myvect1;
+
+      for (int j = 0; j < 2000; j++)
+	{
+	  vector<tt_t> myvect2;
+	  tt_t v;
+	  v.i = j;
+	  myvect1.push_back (v);
+	  myvect2.push_back (v);
+	  list<std::string *> mylist;
+	  std::string string_array[4];
+	  string_array[0] = "toto";
+	  string_array[1] = "titi";
+	  string_array[2] = "tata";
+	  string_array[3] = "tutu";
+	  for (int k = 0; k < 4; k++)
+	    {
+	      if (mylist.size ())
+		{
+		  list<std::string *>::iterator aIt;
+		  for (aIt = mylist.begin (); aIt != mylist.end (); ++aIt)
+		    {
+		      if ((*aIt) == &(string_array[k]))
+			abort ();
+		    }
+		}
+	      mylist.push_back (&(string_array[k]));
+	    }
+	}
+    }
+
+  return arg;
+}
+
+int
+main (int argc, char *argv[])
+{
+  int worker;
+  pthread_t threads[NTHREADS];
+  int ids[NTHREADS];
+  void* status;
+
+#if defined(__sun) && defined(__svr4__)
+  pthread_setconcurrency (NTHREADS);
+#endif
+
+  pthread_attr_t tattr;
+  int ret = pthread_attr_init (&tattr);
+  ret = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
+
+  for (worker = 0; worker < NTHREADS; worker++)
+    {
+      ids[worker] = worker;
+      if (pthread_create(&threads[worker], &tattr,
+			 thread_function, &ids[worker]))
+	abort ();
+    }
+
+  for (worker = 0; worker < NTHREADS; worker++)
+    {
+      if (pthread_join(threads[worker], static_cast<void **>(&status)))
+	abort ();
+
+      if (*((int *)status) != worker)
+	abort ();
+    }
+
+  return (0);
+}
+#else
+int main (void) {}
+#endif
diff --git a/libstdc++-v3/testsuite/thread/pthread6.cc b/libstdc++-v3/testsuite/thread/pthread6.cc
new file mode 100644
index 000000000000..d0bd82f1b395
--- /dev/null
+++ b/libstdc++-v3/testsuite/thread/pthread6.cc
@@ -0,0 +1,94 @@
+// 2002-01-23  Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
+// Adpated from libstdc++/5444 submitted by markus.breuer@materna.de
+//
+// Copyright (C) 2002 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// { dg-do run { target *-*-freebsd* *-*-linux* *-*-solaris* } }
+// { dg-options "-pthread" { target *-*-freebsd* *-*-linux* } }
+// { dg-options "-pthreads" { target *-*-solaris* } }
+
+#include <string>
+#include <map>
+#include <vector>
+
+// Do not include <pthread.h> explicitly; if threads are properly
+// configured for the port, then it is picked up free from STL headers.
+
+#if __GTHREADS
+
+const int max_thread_count = 8;
+const int loops = 100000;
+
+const char* my_default = "Hallo Welt!";
+
+const int upper_limit = 2500;
+const int lower_limit = 1000;
+
+typedef char charT;
+
+typedef std::string String;
+
+typedef String MyType;
+
+void*
+thread_main (void*)
+{
+  typedef std::map<unsigned int,MyType> Map;
+  typedef Map::value_type Value_Pair;
+  Map myMap;
+
+  for (int loop = 0; loop < loops; loop++)
+    {
+      String& str = myMap[loop];
+      str.append (my_default);
+      myMap.insert (Value_Pair (loop, str));
+      
+      if (myMap.size () > upper_limit)
+	{
+	  while (myMap.size () > lower_limit)
+	    {
+	      Map::iterator it = myMap.begin ();
+	      myMap.erase (it);
+	    }
+	}
+    }
+
+  return 0;
+}
+
+int
+main (void)
+{
+  pthread_t tid[max_thread_count];
+
+#if defined(__sun) && defined(__svr4__)
+  pthread_setconcurrency (max_thread_count);
+#endif
+
+  for (int i = 0; i < max_thread_count; i++)
+    pthread_create (&tid[i], NULL, thread_main, 0);
+
+  for (int i = 0; i < max_thread_count; i++)
+    pthread_join (tid[i], NULL);
+
+  return 0;
+}
+#else
+int main (void) {}
+#endif
-- 
GitLab