Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libstdc++-v3 / testsuite / performance / 23_containers / producer_consumer / sequence.cc
blob5e71a6a588de6f559e5fb1c7f1d4a316e5796d11
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.
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.
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.
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.
28 #include <testsuite_common_types.h>
30 typedef int test_type;
32 // The number of iterations to be performed.
33 int iterations = 1000;
35 // TODO - restore Stefan's comment? i don't understand it. -- fwy
36 int insert_values = 128;
38 class Lock
40 public:
41 Lock() {pthread_mutex_init(&mutex, 0);}
42 ~Lock() {pthread_mutex_destroy(&mutex);}
44 public:
45 inline pthread_mutex_t* operator&() {return &mutex;}
47 public:
48 inline void lock() {pthread_mutex_lock(&mutex);}
49 inline void unlock() {pthread_mutex_unlock(&mutex);}
51 private:
52 Lock(const Lock&);
53 Lock& operator=(Lock&);
55 private:
56 pthread_mutex_t mutex;
59 class AutoLock
61 public:
62 AutoLock(Lock& _lock)
63 : lock(_lock)
64 {lock.lock();}
66 ~AutoLock() {lock.unlock();}
68 private:
69 AutoLock(AutoLock&);
70 AutoLock& operator=(AutoLock&);
72 private:
73 Lock& lock;
76 template<typename Container>
77 class Queue
79 public:
80 Queue() {pthread_cond_init(&condition, 0);}
81 ~Queue() {pthread_cond_destroy(&condition);}
83 public:
84 void push_back(const typename Container::value_type& x);
85 void swap(Container& container);
87 private:
88 pthread_cond_t condition;
89 Lock lock;
90 Container queue;
93 template<typename Container>
94 void
95 Queue<Container>::push_back(const typename Container::value_type& value)
97 AutoLock auto_lock(lock);
98 const bool signal = queue.empty();
99 queue.insert(queue.end(), value);
100 if (signal) pthread_cond_signal(&condition);
103 template<typename Container>
104 void
105 Queue<Container>::swap(Container& container)
107 AutoLock auto_lock(lock);
108 while (queue.empty()) pthread_cond_wait(&condition, &lock);
109 queue.swap(container);
112 class Thread
114 // NB: Make this the last data member of an object defining operator()().
115 public:
116 class Attributes
118 public:
119 Attributes(int state = PTHREAD_CREATE_JOINABLE);
120 ~Attributes() {pthread_attr_destroy(&attributes);}
122 public:
123 inline pthread_attr_t* operator&() {return &attributes;}
125 private:
126 pthread_attr_t attributes;
129 public:
130 Thread() {thread = pthread_self();}
131 ~Thread();
133 public:
134 template <typename ThreadOwner>
135 void create(ThreadOwner* owner);
137 private:
138 pthread_t thread;
141 Thread::Attributes::Attributes(int state)
143 pthread_attr_init(&attributes);
144 pthread_attr_setdetachstate(&attributes, state);
147 Thread::~Thread()
149 if (!pthread_equal(thread, pthread_self()))
150 pthread_join(thread, 0);
153 template<typename ThreadOwner>
154 void*
155 create_thread(void* _this)
157 ThreadOwner* owner = static_cast<ThreadOwner*>(_this);
158 (*owner)();
159 return 0;
162 template<typename ThreadOwner>
163 void
164 Thread::create(ThreadOwner* owner)
166 Thread::Attributes attributes;
167 pthread_create(&thread, &attributes, create_thread<ThreadOwner>, owner);
170 template<typename Container>
171 class Consumer
173 public:
174 Consumer(Queue<Container>& _queue)
175 : queue(_queue)
176 {thread.create(this);}
178 public:
179 void operator()();
181 private:
182 Queue<Container>& queue;
183 Thread thread;
186 template<typename Container>
187 void
188 Consumer<Container>::operator()()
190 for (int j = insert_values * iterations; j > 0;)
192 Container container;
193 queue.swap(container);
194 j -= container.size();
198 template<typename TestType>
199 struct Value : public std::pair<TestType, TestType>
201 Value()
202 : std::pair<TestType, TestType>(0, 0)
205 inline Value operator++() {return ++this->first, *this;}
206 inline operator TestType() const {return this->first;}
209 template<typename Container>
210 class ProducerConsumer : private Queue<Container>
212 public:
213 ProducerConsumer() {thread.create(this);}
215 public:
216 void operator()();
218 private:
219 Thread thread;
222 template<typename Container>
223 void
224 ProducerConsumer<Container>::operator()()
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);
232 template<typename Container, int Iter>
233 void
234 do_loop()
236 ProducerConsumer<Container> pc1;
237 ProducerConsumer<Container> pc2;
241 main()
243 #ifdef TEST_T1
244 #define thread_type true
245 #endif
247 using __gnu_test::sequence_containers;
248 typedef sequence_containers<test_type, thread_type>::type container_types;
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);
255 return 0;