X-Git-Url: https://oss.titaniummirror.com/gitweb?a=blobdiff_plain;f=libstdc%2B%2B-v3%2Ftestsuite%2Fperformance%2F23_containers%2Fproducer_consumer%2Fassociative.cc;fp=libstdc%2B%2B-v3%2Ftestsuite%2Fperformance%2F23_containers%2Fproducer_consumer%2Fassociative.cc;h=0db964e59f1da52e4f89b4d00ca94bc1dcf9caee;hb=6fed43773c9b0ce596dca5686f37ac3fc0fa11c0;hp=0000000000000000000000000000000000000000;hpb=27b11d56b743098deb193d510b337ba22dc52e5c;p=msp430-gcc.git diff --git a/libstdc++-v3/testsuite/performance/23_containers/producer_consumer/associative.cc b/libstdc++-v3/testsuite/performance/23_containers/producer_consumer/associative.cc new file mode 100644 index 00000000..0db964e5 --- /dev/null +++ b/libstdc++-v3/testsuite/performance/23_containers/producer_consumer/associative.cc @@ -0,0 +1,248 @@ +// Copyright (C) 2003, 2004, 2005, 2006, 2009 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + + +#include + +typedef int test_type; + +// The number of iterations to be performed. +int iterations = 1000; + +// TODO - restore Stefan's comment? i don't understand it. -- fwy +int insert_values = 128; + +class Lock +{ +public: + Lock() {pthread_mutex_init(&mutex, 0);} + ~Lock() {pthread_mutex_destroy(&mutex);} + +public: + inline pthread_mutex_t* operator&() {return &mutex;} + +public: + inline void lock() {pthread_mutex_lock(&mutex);} + inline void unlock() {pthread_mutex_unlock(&mutex);} + +private: + Lock(const Lock&); + Lock& operator=(Lock&); + +private: + pthread_mutex_t mutex; +}; + +class AutoLock +{ +public: + AutoLock(Lock& _lock) + : lock(_lock) + {lock.lock();} + + ~AutoLock() {lock.unlock();} + +private: + AutoLock(AutoLock&); + AutoLock& operator=(AutoLock&); + +private: + Lock& lock; +}; + +template + class Queue + { + public: + Queue() {pthread_cond_init(&condition, 0);} + ~Queue() {pthread_cond_destroy(&condition);} + + public: + void push_back(const typename Container::value_type& x); + void swap(Container& container); + + private: + pthread_cond_t condition; + Lock lock; + Container queue; + }; + +template + void + Queue::push_back(const typename Container::value_type& value) + { + AutoLock auto_lock(lock); + const bool signal = queue.empty(); + queue.insert(queue.end(), value); + if (signal) pthread_cond_signal(&condition); + } + +template + void + Queue::swap(Container& container) + { + AutoLock auto_lock(lock); + while (queue.empty()) pthread_cond_wait(&condition, &lock); + queue.swap(container); + } + +class Thread +{ + // NB: Make this the last data member of an object defining operator()(). +public: + class Attributes + { + public: + Attributes(int state = PTHREAD_CREATE_JOINABLE); + ~Attributes() {pthread_attr_destroy(&attributes);} + + public: + inline pthread_attr_t* operator&() {return &attributes;} + + private: + pthread_attr_t attributes; + }; + +public: + Thread() {thread = pthread_self();} + ~Thread(); + +public: + template + void create(ThreadOwner* owner); + +private: + pthread_t thread; +}; + +Thread::Attributes::Attributes(int state) +{ + pthread_attr_init(&attributes); + pthread_attr_setdetachstate(&attributes, state); +} + +Thread::~Thread() +{ + if (!pthread_equal(thread, pthread_self())) + pthread_join(thread, 0); +} + +template + void* + create_thread(void* _this) + { + ThreadOwner* owner = static_cast(_this); + (*owner)(); + return 0; + } + +template + void + Thread::create(ThreadOwner* owner) + { + Thread::Attributes attributes; + pthread_create(&thread, &attributes, create_thread, owner); + } + +template + class Consumer + { + public: + Consumer(Queue& _queue) + : queue(_queue) + {thread.create(this);} + + public: + void operator()(); + + private: + Queue& queue; + Thread thread; + }; + +template + void + Consumer::operator()() + { + for (int j = insert_values * iterations; j > 0;) + { + Container container; + queue.swap(container); + j -= container.size(); + } + } + +template + struct Value : public std::pair + { + Value() + : std::pair(0, 0) + { } + + inline Value operator++() {return ++this->first, *this;} + inline operator TestType() const {return this->first;} + }; + +template + class ProducerConsumer : private Queue + { + public: + ProducerConsumer() {thread.create(this);} + + public: + void operator()(); + + private: + Thread thread; + }; + +template + void + ProducerConsumer::operator()() + { + Consumer consumer(*this); + Value test_value; + for (int j = insert_values * iterations; j-- > 0;) + this->push_back(++test_value); + } + +template + void + do_loop() + { + ProducerConsumer pc1; + ProducerConsumer pc2; + } + +int +main() +{ +#ifdef TEST_T1 +#define thread_type true +#endif + + typedef __gnu_test::maps::type map_typelist; + typedef __gnu_test::sets::type set_typelist; + typedef __gnu_cxx::typelist::append::type container_types; + + typedef test_sequence test_type; + test_type test("producer_consumer_associative"); + __gnu_cxx::typelist::apply(test, container_types()); + + return 0; +} +