]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/performance/23_containers/producer_consumer/sequence.cc
All files: Update FSF address.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / performance / 23_containers / producer_consumer / sequence.cc
1 // Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
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
16 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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
28 #include <testsuite_common_types.h>
29
30 typedef int test_type;
31
32 // The number of iterations to be performed.
33 int iterations = 1000;
34
35 // TODO - restore Stefan's comment? i don't understand it. -- fwy
36 int insert_values = 128;
37
38 class Lock
39 {
40 public:
41 Lock() {pthread_mutex_init(&mutex, 0);}
42 ~Lock() {pthread_mutex_destroy(&mutex);}
43
44 public:
45 inline pthread_mutex_t* operator&() {return &mutex;}
46
47 public:
48 inline void lock() {pthread_mutex_lock(&mutex);}
49 inline void unlock() {pthread_mutex_unlock(&mutex);}
50
51 private:
52 Lock(const Lock&);
53 Lock& operator=(Lock&);
54
55 private:
56 pthread_mutex_t mutex;
57 };
58
59 class AutoLock
60 {
61 public:
62 AutoLock(Lock& _lock)
63 : lock(_lock)
64 {lock.lock();}
65
66 ~AutoLock() {lock.unlock();}
67
68 private:
69 AutoLock(AutoLock&);
70 AutoLock& operator=(AutoLock&);
71
72 private:
73 Lock& lock;
74 };
75
76 template<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
93 template<typename Container>
94 void
95 Queue<Container>::push_back(const typename Container::value_type& value)
96 {
97 AutoLock auto_lock(lock);
98 const bool signal = queue.empty();
99 queue.insert(queue.end(), value);
100 if (signal) pthread_cond_signal(&condition);
101 }
102
103 template<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
112 class Thread
113 {
114 // NB: Make this the last data member of an object defining operator()().
115 public:
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
129 public:
130 Thread() {thread = pthread_self();}
131 ~Thread();
132
133 public:
134 template <typename ThreadOwner>
135 void create(ThreadOwner* owner);
136
137 private:
138 pthread_t thread;
139 };
140
141 Thread::Attributes::Attributes(int state)
142 {
143 pthread_attr_init(&attributes);
144 pthread_attr_setdetachstate(&attributes, state);
145 }
146
147 Thread::~Thread()
148 {
149 if (!pthread_equal(thread, pthread_self()))
150 pthread_join(thread, 0);
151 }
152
153 template<typename ThreadOwner>
154 void*
155 create_thread(void* _this)
156 {
157 ThreadOwner* owner = static_cast<ThreadOwner*>(_this);
158 (*owner)();
159 return 0;
160 }
161
162 template<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
170 template<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
186 template<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
198 template<typename TestType>
199 struct Value : public std::pair<TestType, TestType>
200 {
201 Value()
202 : std::pair<TestType, TestType>(0, 0)
203 { }
204
205 inline Value operator++() {return ++this->first, *this;}
206 inline operator TestType() const {return this->first;}
207 };
208
209 template<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
222 template<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
232 template<typename Container, int Iter>
233 void
234 do_loop()
235 {
236 ProducerConsumer<Container> pc1;
237 ProducerConsumer<Container> pc2;
238 }
239
240 int
241 main()
242 {
243 #ifdef TEST_T1
244 #define thread_type true
245 #endif
246
247 using __gnu_test::sequence_containers;
248 typedef sequence_containers<test_type, thread_type>::type container_types;
249
250 typedef test_sequence<thread_type> test_type;
251 test_type test("producer_consumer_sequence");
252 __gnu_cxx::apply<test_type, container_types> applier;
253 applier(test);
254
255 return 0;
256 }
257