123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #ifndef BOOST_STATECHART_FIFO_WORKER_HPP_INCLUDED
- #define BOOST_STATECHART_FIFO_WORKER_HPP_INCLUDED
- #include <boost/assert.hpp>
- #include <boost/noncopyable.hpp>
- #include <boost/function/function0.hpp>
- #include <boost/bind.hpp>
- #include <boost/config.hpp>
- #include <boost/detail/allocator_utilities.hpp>
- #ifdef BOOST_HAS_THREADS
- # ifdef BOOST_MSVC
- # pragma warning( push )
-
- # pragma warning( disable: 4127 )
-
- # pragma warning( disable: 4244 )
-
- # pragma warning( disable: 4251 )
-
- # pragma warning( disable: 4512 )
-
-
- # pragma warning( disable: 4996 )
- # endif
- # include <boost/thread/mutex.hpp>
- # include <boost/thread/condition.hpp>
- # ifdef BOOST_MSVC
- # pragma warning( pop )
- # endif
- #endif
- #include <list>
- #include <memory> // std::allocator
- namespace boost
- {
- namespace statechart
- {
- template< class Allocator = std::allocator< none > >
- class fifo_worker : noncopyable
- {
- public:
-
- #ifdef BOOST_HAS_THREADS
- fifo_worker( bool waitOnEmptyQueue = false ) :
- waitOnEmptyQueue_( waitOnEmptyQueue ),
- #else
- fifo_worker() :
- #endif
- terminated_( false )
- {
- }
- typedef function0< void > work_item;
-
-
-
- void queue_work_item( work_item & item )
- {
- if ( item.empty() )
- {
- return;
- }
- #ifdef BOOST_HAS_THREADS
- mutex::scoped_lock lock( mutex_ );
- #endif
- workQueue_.push_back( work_item() );
- workQueue_.back().swap( item );
- #ifdef BOOST_HAS_THREADS
- queueNotEmpty_.notify_one();
- #endif
- }
-
-
-
-
- void queue_work_item( const work_item & item )
- {
- work_item copy = item;
- queue_work_item( copy );
- }
- void terminate()
- {
- work_item item = boost::bind( &fifo_worker::terminate_impl, this );
- queue_work_item( item );
- }
-
-
- bool terminated() const
- {
- return terminated_;
- }
- unsigned long operator()( unsigned long maxItemCount = 0 )
- {
- unsigned long itemCount = 0;
- while ( !terminated() &&
- ( ( maxItemCount == 0 ) || ( itemCount < maxItemCount ) ) )
- {
- work_item item = dequeue_item();
- if ( item.empty() )
- {
-
-
-
- return itemCount;
- }
- item();
- ++itemCount;
- }
- return itemCount;
- }
- private:
-
- work_item dequeue_item()
- {
- #ifdef BOOST_HAS_THREADS
- mutex::scoped_lock lock( mutex_ );
- if ( !waitOnEmptyQueue_ && workQueue_.empty() )
- {
- return work_item();
- }
- while ( workQueue_.empty() )
- {
- queueNotEmpty_.wait( lock );
- }
- #else
-
-
-
-
-
-
- if ( workQueue_.empty() )
- {
- return work_item();
- }
- #endif
-
-
- work_item result;
- result.swap( workQueue_.front() );
- workQueue_.pop_front();
- return result;
- }
- void terminate_impl()
- {
- terminated_ = true;
- }
- typedef std::list<
- work_item,
- typename boost::detail::allocator::rebind_to<
- Allocator, work_item >::type
- > work_queue_type;
- work_queue_type workQueue_;
- #ifdef BOOST_HAS_THREADS
- mutex mutex_;
- condition queueNotEmpty_;
- const bool waitOnEmptyQueue_;
- #endif
- bool terminated_;
- };
- }
- }
- #endif
|