OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [libstdc++-v3/] [testsuite/] [23_containers/] [list/] [pthread1.cc] - Blame information for rev 424

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 424 jeremybenn
// 2002-01-23  Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
2
//
3
// Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2009
4
// Free Software Foundation, Inc.
5
//
6
// This file is part of the GNU ISO C++ Library.  This library is free
7
// software; you can redistribute it and/or modify it under the
8
// terms of the GNU General Public License as published by the
9
// Free Software Foundation; either version 3, or (at your option)
10
// any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License along
18
// with this library; see the file COPYING3.  If not see
19
// <http://www.gnu.org/licenses/>.
20
 
21
// { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
22
// { dg-options "-pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
23
// { dg-options "-pthreads" { target *-*-solaris* } }
24
 
25
// This multi-threading C++/STL/POSIX code adheres to rules outlined here:
26
// http://www.sgi.com/tech/stl/thread_safety.html
27
//
28
// It is believed to exercise the allocation code in a manner that
29
// should reveal memory leaks (and, under rare cases, race conditions,
30
// if the STL threading support is fubar'd).
31
 
32
#include <list>
33
#include <cstdlib>
34
#include <pthread.h>
35
 
36
const int thread_cycles = 10;
37
const int thread_pairs = 10;
38
const unsigned max_size = 100;
39
const int iters = 10000;
40
 
41
class task_queue
42
{
43
  typedef std::list<int> list_type;
44
 
45
public:
46
  task_queue ()
47
  {
48
    pthread_mutex_init (&fooLock, NULL);
49
    pthread_cond_init (&fooCond1, NULL);
50
    pthread_cond_init (&fooCond2, NULL);
51
  }
52
  ~task_queue ()
53
  {
54
    pthread_mutex_destroy (&fooLock);
55
    pthread_cond_destroy (&fooCond1);
56
    pthread_cond_destroy (&fooCond2);
57
  }
58
 
59
  list_type             foo;
60
  pthread_mutex_t       fooLock;
61
  pthread_cond_t        fooCond1;
62
  pthread_cond_t        fooCond2;
63
};
64
 
65
void*
66
produce(void* t)
67
{
68
  task_queue& tq = *(static_cast<task_queue*> (t));
69
  int num = 0;
70
  while (num < iters)
71
    {
72
      pthread_mutex_lock (&tq.fooLock);
73
      while (tq.foo.size () >= max_size)
74
        pthread_cond_wait (&tq.fooCond1, &tq.fooLock);
75
      tq.foo.push_back (num++);
76
      pthread_cond_signal (&tq.fooCond2);
77
      pthread_mutex_unlock (&tq.fooLock);
78
    }
79
  return 0;
80
}
81
 
82
void*
83
consume(void* t)
84
{
85
  task_queue& tq = *(static_cast<task_queue*> (t));
86
  int num = 0;
87
  while (num < iters)
88
    {
89
      pthread_mutex_lock (&tq.fooLock);
90
      while (tq.foo.size () == 0)
91
        pthread_cond_wait (&tq.fooCond2, &tq.fooLock);
92
      if (tq.foo.front () != num++)
93
        abort ();
94
      tq.foo.pop_front ();
95
      pthread_cond_signal (&tq.fooCond1);
96
      pthread_mutex_unlock (&tq.fooLock);
97
    }
98
  return 0;
99
}
100
 
101
int
102
main()
103
{
104
  pthread_t prod[thread_pairs];
105
  pthread_t cons[thread_pairs];
106
 
107
  task_queue* tq[thread_pairs];
108
 
109
#if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
110
  pthread_setconcurrency (thread_pairs * 2);
111
#endif
112
 
113
  for (int j = 0; j < thread_cycles; j++)
114
    {
115
      for (int i = 0; i < thread_pairs; i++)
116
        {
117
          tq[i] = new task_queue;
118
          pthread_create (&prod[i], NULL, produce, static_cast<void*> (tq[i]));
119
          pthread_create (&cons[i], NULL, consume, static_cast<void*> (tq[i]));
120
        }
121
 
122
      for (int i = 0; i < thread_pairs; i++)
123
        {
124
          pthread_join (prod[i], NULL);
125
          pthread_join (cons[i], NULL);
126
          delete tq[i];
127
        }
128
    }
129
 
130
  return 0;
131
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.