1 // Copyright (C) 2014 Ian Forbed
2 // Copyright (C) 2014 Vicente J. Botet Escriba
3 //
4 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
5 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 #ifndef BOOST_THREAD_EXECUTORS_DETAIL_PRIORITY_EXECUTOR_BASE_HPP
9 #define BOOST_THREAD_EXECUTORS_DETAIL_PRIORITY_EXECUTOR_BASE_HPP
10 
11 #include <boost/atomic.hpp>
12 #include <boost/function.hpp>
13 #include <boost/thread/thread.hpp>
14 #include <boost/thread/concurrent_queues/sync_timed_queue.hpp>
15 #include <boost/thread/executors/work.hpp>
16 
17 namespace boost
18 {
19 namespace executors
20 {
21 namespace detail
22 {
23   template <class Queue>
24   class priority_executor_base
25   {
26   public:
27     //typedef boost::function<void()> work;
28     typedef executors::work_pq work;
29   protected:
30     typedef Queue queue_type;
31     queue_type _workq;
32 
priority_executor_base()33     priority_executor_base() {}
34   public:
35 
~priority_executor_base()36     ~priority_executor_base()
37     {
38       if(!closed())
39       {
40         this->close();
41       }
42     }
43 
close()44     void close()
45     {
46       _workq.close();
47     }
48 
closed()49     bool closed()
50     {
51       return _workq.closed();
52     }
53 
loop()54     void loop()
55     {
56       try
57       {
58         for(;;)
59         {
60           try {
61             work task;
62             queue_op_status st = _workq.wait_pull(task);
63             if (st == queue_op_status::closed) return;
64             task();
65           }
66           catch (boost::thread_interrupted&)
67           {
68             return;
69           }
70         }
71       }
72       catch (...)
73       {
74         std::terminate();
75         return;
76       }
77     }
78   }; //end class
79 
80 } //end detail namespace
81 } //end executors namespace
82 } //end boost namespace
83 #endif
84