]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/performance/23_containers/producer_consumer/associative.cc
typelist_assoc_container.hpp: Remove, unused.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / performance / 23_containers / producer_consumer / associative.cc
CommitLineData
cad367a6 1// Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
de8a2f87
BK
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 2, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING. If not, write to the Free
83f51799 16// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
de8a2f87
BK
17// USA.
18
19// As a special exception, you may use this file as part of a free software
20// library without restriction. Specifically, if other files instantiate
21// templates or use macros or inline functions from this file, or you compile
22// this file and link it with other files to produce an executable, this
23// file does not by itself cause the resulting executable to be covered by
24// the GNU General Public License. This exception does not however
25// invalidate any other reasons why the executable file might be covered by
26// the GNU General Public License.
27
fd1e1726 28#include <testsuite_common_types.h>
de8a2f87
BK
29
30typedef int test_type;
1b90e7a3 31
de8a2f87 32// The number of iterations to be performed.
fd1e1726 33int iterations = 1000;
de8a2f87
BK
34
35// TODO - restore Stefan's comment? i don't understand it. -- fwy
36int insert_values = 128;
37
38class Lock
39{
40public:
41 Lock() {pthread_mutex_init(&mutex, 0);}
42 ~Lock() {pthread_mutex_destroy(&mutex);}
43
44public:
45 inline pthread_mutex_t* operator&() {return &mutex;}
46
47public:
48 inline void lock() {pthread_mutex_lock(&mutex);}
49 inline void unlock() {pthread_mutex_unlock(&mutex);}
50
51private:
52 Lock(const Lock&);
53 Lock& operator=(Lock&);
54
55private:
56 pthread_mutex_t mutex;
57};
58
59class AutoLock
60{
61public:
62 AutoLock(Lock& _lock)
63 : lock(_lock)
64 {lock.lock();}
65
66 ~AutoLock() {lock.unlock();}
67
68private:
69 AutoLock(AutoLock&);
70 AutoLock& operator=(AutoLock&);
71
72private:
73 Lock& lock;
74};
75
76template<typename Container>
77 class Queue
78 {
79 public:
80 Queue() {pthread_cond_init(&condition, 0);}
81 ~Queue() {pthread_cond_destroy(&condition);}
82
83 public:
84 void push_back(const typename Container::value_type& x);
85 void swap(Container& container);
86
87 private:
88 pthread_cond_t condition;
89 Lock lock;
90 Container queue;
91 };
92
93template<typename Container>
94 void
95 Queue<Container>::push_back(const typename Container::value_type& value)
96 {
97 AutoLock auto_lock(lock);
b4a76c01 98 const bool signal = queue.empty();
de8a2f87 99 queue.insert(queue.end(), value);
b4a76c01 100 if (signal) pthread_cond_signal(&condition);
de8a2f87
BK
101 }
102
103template<typename Container>
104 void
105 Queue<Container>::swap(Container& container)
106 {
107 AutoLock auto_lock(lock);
108 while (queue.empty()) pthread_cond_wait(&condition, &lock);
109 queue.swap(container);
110 }
111
112class Thread
113{
114 // NB: Make this the last data member of an object defining operator()().
115public:
116 class Attributes
117 {
118 public:
119 Attributes(int state = PTHREAD_CREATE_JOINABLE);
120 ~Attributes() {pthread_attr_destroy(&attributes);}
121
122 public:
123 inline pthread_attr_t* operator&() {return &attributes;}
124
125 private:
126 pthread_attr_t attributes;
127 };
128
129public:
130 Thread() {thread = pthread_self();}
131 ~Thread();
132
133public:
134 template <typename ThreadOwner>
135 void create(ThreadOwner* owner);
136
137private:
138 pthread_t thread;
139};
140
141Thread::Attributes::Attributes(int state)
142{
143 pthread_attr_init(&attributes);
144 pthread_attr_setdetachstate(&attributes, state);
145}
146
147Thread::~Thread()
148{
149 if (!pthread_equal(thread, pthread_self()))
150 pthread_join(thread, 0);
151}
152
153template<typename ThreadOwner>
154 void*
155 create_thread(void* _this)
156 {
157 ThreadOwner* owner = static_cast<ThreadOwner*>(_this);
158 (*owner)();
159 return 0;
160 }
161
162template<typename ThreadOwner>
163 void
164 Thread::create(ThreadOwner* owner)
165 {
166 Thread::Attributes attributes;
167 pthread_create(&thread, &attributes, create_thread<ThreadOwner>, owner);
168 }
169
170template<typename Container>
171 class Consumer
172 {
173 public:
174 Consumer(Queue<Container>& _queue)
175 : queue(_queue)
176 {thread.create(this);}
177
178 public:
179 void operator()();
180
181 private:
182 Queue<Container>& queue;
183 Thread thread;
184 };
185
186template<typename Container>
187 void
188 Consumer<Container>::operator()()
189 {
190 for (int j = insert_values * iterations; j > 0;)
191 {
192 Container container;
193 queue.swap(container);
194 j -= container.size();
195 }
196 }
197
198template<typename TestType>
fd1e1726 199 struct Value : public std::pair<TestType, TestType>
de8a2f87
BK
200 {
201 Value()
fd1e1726 202 : std::pair<TestType, TestType>(0, 0)
de8a2f87
BK
203 { }
204
205 inline Value operator++() {return ++this->first, *this;}
206 inline operator TestType() const {return this->first;}
207 };
208
209template<typename Container>
210 class ProducerConsumer : private Queue<Container>
211 {
212 public:
213 ProducerConsumer() {thread.create(this);}
214
215 public:
216 void operator()();
217
218 private:
219 Thread thread;
220 };
221
222template<typename Container>
223 void
224 ProducerConsumer<Container>::operator()()
225 {
226 Consumer<Container> consumer(*this);
227 Value<test_type> test_value;
228 for (int j = insert_values * iterations; j-- > 0;)
229 this->push_back(++test_value);
230 }
231
fd1e1726 232template<typename Container, int Iter>
de8a2f87 233 void
fd1e1726 234 do_loop()
de8a2f87 235 {
fd1e1726
BK
236 ProducerConsumer<Container> pc1;
237 ProducerConsumer<Container> pc2;
238 }
de8a2f87 239
fd1e1726
BK
240int
241main()
242{
243#ifdef TEST_T1
244#define thread_type true
245#endif
de8a2f87 246
fd1e1726
BK
247 typedef __gnu_test::maps<test_type, thread_type>::type map_typelist;
248 typedef __gnu_test::sets<test_type, thread_type>::type set_typelist;
cad367a6 249 typedef __gnu_cxx::typelist::append<map_typelist, set_typelist>::type container_types;
de8a2f87 250
fd1e1726
BK
251 typedef test_sequence<thread_type> test_type;
252 test_type test("producer_consumer_associative");
d7f245b1 253 __gnu_cxx::typelist::apply(test, container_types());
de8a2f87
BK
254
255 return 0;
256}
257