My Project
fifo_scheduler.hpp
Go to the documentation of this file.
1 #ifndef BOOST_STATECHART_FIFO_SCHEDULER_HPP_INCLUDED
2 #define BOOST_STATECHART_FIFO_SCHEDULER_HPP_INCLUDED
3 // Copyright 2002-2006 Andreas Huber Doenni
5 // Distributed under the Boost Software License, Version 1.0. (See accompany-
6 // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 
9 
10 
14 
15 #include <boost/intrusive_ptr.hpp>
16 #include <boost/noncopyable.hpp>
17 #include <boost/config.hpp> // BOOST_HAS_THREADS
18 
19 
20 
21 namespace boost
22 {
23 namespace statechart
24 {
25 
26 
27 
29 template<
30  class FifoWorker = fifo_worker<>,
31  class Allocator = std::allocator< none > >
32 class fifo_scheduler : noncopyable
33 {
34  typedef processor_container<
35  fifo_scheduler, typename FifoWorker::work_item, Allocator > container;
36  public:
38  #ifdef BOOST_HAS_THREADS
39  fifo_scheduler( bool waitOnEmptyQueue = false ) :
40  worker_( waitOnEmptyQueue )
41  {
42  }
43  #endif
44 
46  typedef typename container::processor_context processor_context;
47 
48  template< class Processor >
50  {
52  work_item item =
53  container_.template create_processor< Processor >( result, *this );
54  worker_.queue_work_item( item );
55  return result;
56  }
57 
58  template< class Processor, typename Arg1 >
60  {
62  work_item item = container_.template create_processor< Processor >(
63  result, *this, arg1 );
64  worker_.queue_work_item( item );
65  return result;
66  }
67 
68  template< class Processor, typename Arg1, typename Arg2 >
69  processor_handle create_processor( Arg1 arg1, Arg2 arg2 )
70  {
72  work_item item = container_.template create_processor< Processor >(
73  result, *this, arg1, arg2 );
74  worker_.queue_work_item( item );
75  return result;
76  }
77 
78  template< class Processor, typename Arg1, typename Arg2, typename Arg3 >
79  processor_handle create_processor( Arg1 arg1, Arg2 arg2, Arg3 arg3 )
80  {
82  work_item item = container_.template create_processor< Processor >(
83  result, *this, arg1, arg2, arg3 );
84  worker_.queue_work_item( item );
85  return result;
86  }
87 
88  template<
89  class Processor, typename Arg1, typename Arg2,
90  typename Arg3, typename Arg4 >
92  Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4 )
93  {
95  work_item item = container_.template create_processor< Processor >(
96  result, *this, arg1, arg2, arg3, arg4 );
97  worker_.queue_work_item( item );
98  return result;
99  }
100 
101  template<
102  class Processor, typename Arg1, typename Arg2,
103  typename Arg3, typename Arg4, typename Arg5 >
105  Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5 )
106  {
108  work_item item = container_.template create_processor< Processor >(
109  result, *this, arg1, arg2, arg3, arg4, arg5 );
110  worker_.queue_work_item( item );
111  return result;
112  }
113 
114  template<
115  class Processor, typename Arg1, typename Arg2,
116  typename Arg3, typename Arg4, typename Arg5, typename Arg6 >
118  Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6 )
119  {
121  work_item item = container_.template create_processor< Processor >(
122  result, *this, arg1, arg2, arg3, arg4, arg5, arg6 );
123  worker_.queue_work_item( item );
124  return result;
125  }
126 
127  void destroy_processor( const processor_handle & processor )
128  {
129  work_item item = container_.destroy_processor( processor );
130  worker_.queue_work_item( item );
131  }
132 
133  void initiate_processor( const processor_handle & processor )
134  {
135  work_item item = container_.initiate_processor( processor );
136  worker_.queue_work_item( item );
137  }
138 
139  void terminate_processor( const processor_handle & processor )
140  {
141  work_item item = container_.terminate_processor( processor );
142  worker_.queue_work_item( item );
143  }
144 
145  typedef intrusive_ptr< const event_base > event_ptr_type;
146 
148  const processor_handle & processor, const event_ptr_type & pEvent )
149  {
150  work_item item = container_.queue_event( processor, pEvent );
151  worker_.queue_work_item( item );
152  }
153 
154  typedef typename FifoWorker::work_item work_item;
155 
156  // We take a non-const reference so that we can move (i.e. swap) the item
157  // into the queue, what avoids copying the (possibly heap-allocated)
158  // implementation object inside work_item.
159  void queue_work_item( work_item & item )
160  {
161  worker_.queue_work_item( item );
162  }
163 
164  // Convenience overload so that temporary objects can be passed directly
165  // instead of having to create a work_item object first. Under most
166  // circumstances, this will lead to one unnecessary copy of the
167  // function implementation object.
168  void queue_work_item( const work_item & item )
169  {
170  worker_.queue_work_item( item );
171  }
172 
173  void terminate()
174  {
175  worker_.terminate();
176  }
177 
178  // Is not mutex-protected! Must only be called from the thread that also
179  // calls operator().
180  bool terminated() const
181  {
182  return worker_.terminated();
183  }
184 
185  unsigned long operator()( unsigned long maxEventCount = 0 )
186  {
187  return worker_( maxEventCount );
188  }
189 
190  private:
192  container container_;
193  FifoWorker worker_;
194 };
195 
196 
197 
198 } // namespace statechart
199 } // namespace boost
200 
201 
202 
203 #endif
boost::statechart::fifo_scheduler::queue_work_item
void queue_work_item(const work_item &item)
Definition: fifo_scheduler.hpp:168
event_base.hpp
boost::statechart::fifo_scheduler::create_processor
processor_handle create_processor(Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
Definition: fifo_scheduler.hpp:104
boost::statechart::processor_container::queue_event
WorkItem queue_event(const processor_handle &processor, const event_ptr_type &pEvent)
Definition: processor_container.hpp:291
boost::statechart::fifo_scheduler<>::work_item
FifoWorker::work_item work_item
Definition: fifo_scheduler.hpp:154
boost::statechart::fifo_worker<>
boost::statechart::processor_container::initiate_processor
WorkItem initiate_processor(const processor_handle &processor)
Definition: processor_container.hpp:273
boost::statechart::fifo_scheduler::operator()
unsigned long operator()(unsigned long maxEventCount=0)
Definition: fifo_scheduler.hpp:185
boost::statechart::fifo_scheduler::terminated
bool terminated() const
Definition: fifo_scheduler.hpp:180
boost::statechart::result
detail::safe_reaction_result result
Definition: result.hpp:91
boost::statechart::processor_container::terminate_processor
WorkItem terminate_processor(const processor_handle &processor)
Definition: processor_container.hpp:281
boost::statechart::detail::safe_reaction_result
Definition: result.hpp:38
boost::statechart::fifo_scheduler::terminate_processor
void terminate_processor(const processor_handle &processor)
Definition: fifo_scheduler.hpp:139
boost::statechart::fifo_scheduler::create_processor
processor_handle create_processor(Arg1 arg1, Arg2 arg2, Arg3 arg3)
Definition: fifo_scheduler.hpp:79
boost
Definition: asynchronous_state_machine.hpp:20
boost::statechart::fifo_scheduler::create_processor
processor_handle create_processor()
Definition: fifo_scheduler.hpp:49
boost::statechart::fifo_scheduler::initiate_processor
void initiate_processor(const processor_handle &processor)
Definition: fifo_scheduler.hpp:133
boost::statechart::fifo_scheduler
Definition: fifo_scheduler.hpp:33
boost::statechart::fifo_scheduler::processor_handle
container::processor_handle processor_handle
Definition: fifo_scheduler.hpp:45
boost::statechart::processor_container
Definition: processor_container.hpp:65
boost::statechart::processor_container< fifo_scheduler, typename FifoWorker::work_item, std::allocator< none > >::processor_handle
weak_ptr< processor_holder_type > processor_handle
Definition: processor_container.hpp:76
boost::statechart::fifo_scheduler::processor_context
container::processor_context processor_context
Definition: fifo_scheduler.hpp:46
boost::statechart::fifo_scheduler::create_processor
processor_handle create_processor(Arg1 arg1, Arg2 arg2)
Definition: fifo_scheduler.hpp:69
boost::statechart::fifo_scheduler::create_processor
processor_handle create_processor(Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
Definition: fifo_scheduler.hpp:91
boost::statechart::processor_container::destroy_processor
WorkItem destroy_processor(const processor_handle &processor)
Definition: processor_container.hpp:266
processor_container.hpp
boost::statechart::fifo_scheduler::event_ptr_type
intrusive_ptr< const event_base > event_ptr_type
Definition: fifo_scheduler.hpp:145
boost::statechart::fifo_scheduler::queue_work_item
void queue_work_item(work_item &item)
Definition: fifo_scheduler.hpp:159
boost::statechart::fifo_scheduler::create_processor
processor_handle create_processor(Arg1 arg1)
Definition: fifo_scheduler.hpp:59
boost::statechart::fifo_scheduler::create_processor
processor_handle create_processor(Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6)
Definition: fifo_scheduler.hpp:117
boost::statechart::fifo_scheduler::terminate
void terminate()
Definition: fifo_scheduler.hpp:173
boost::statechart::fifo_scheduler::queue_event
void queue_event(const processor_handle &processor, const event_ptr_type &pEvent)
Definition: fifo_scheduler.hpp:147
fifo_worker.hpp
boost::statechart::fifo_scheduler::destroy_processor
void destroy_processor(const processor_handle &processor)
Definition: fifo_scheduler.hpp:127