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