neoGFX
Cross-platform C++ app/game engine
async_task.hpp
Go to the documentation of this file.
1 // async_task.hpp v1.0
2 /*
3  * Copyright (c) 2007 Leigh Johnston.
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * * Neither the name of Leigh Johnston nor the names of any
19  * other contributors to this software may be used to endorse or
20  * promote products derived from this software without specific prior
21  * written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #pragma once
37 
38 #include <neolib/neolib.hpp>
39 #include <boost/asio.hpp>
40 #include <neolib/lifetime.hpp>
41 #include <neolib/i_thread.hpp>
43 #include <neolib/task.hpp>
44 #include <neolib/i_async_task.hpp>
45 #include <neolib/plugin_event.hpp>
46 #include <neolib/message_queue.hpp>
47 
48 namespace neolib
49 {
50  class async_task;
51 
52  class io_service : public i_io_service
53  {
54  // types
55  public:
56  typedef boost::asio::io_service native_io_service_type;
57  // construction
58  public:
59  io_service(async_task& aTask) : iTask(aTask) {}
60  // operations
61  public:
62  bool do_io(bool aProcessEvents = true, std::size_t aMaximumPollCount = kDefaultPollCount) override;
63  native_io_service_type& native_object() { return iNativeIoService; }
64  // attributes
65  private:
66  async_task& iTask;
67  native_io_service_type iNativeIoService;
68  };
69 
70  class async_task : public task<i_async_task>, public lifetime
71  {
72  friend class async_thread;
73  // events
74  public:
77  // exceptions
78  public:
79  struct no_thread : std::logic_error { no_thread() : std::logic_error{ "neolib::async_task::no_thread" } {} };
80  // types
81  private:
82  typedef std::unique_ptr<neolib::message_queue> message_queue_pointer;
83  // construction
84  public:
85  async_task(const std::string& aName = std::string{});
86  async_task(i_thread& aThread, const std::string& aName = std::string{});
87  ~async_task();
88  // operations
89  public:
90  i_thread& thread() const;
91  bool joined() const;
92  void join(i_thread& aThread);
93  void detach();
94  bool do_io(yield_type aYieldIfNoWork = yield_type::NoYield);
95  io_service& timer_io_service() { return iTimerIoService; }
96  io_service& networking_io_service() { return iNetworkingIoService; }
97  bool have_message_queue() const;
98  bool have_messages() const;
99  neolib::message_queue& create_message_queue(std::function<bool()> aIdleFunction = std::function<bool()>());
100  const neolib::message_queue& message_queue() const;
101  neolib::message_queue& message_queue();
102  bool pump_messages();
103  bool halted() const;
104  void halt();
105  // implementation
106  protected:
107  // i_lifetime
108  void set_destroying() override;
109  void set_destroyed() override;
110  // task
111  void run(yield_type aYieldType = yield_type::NoYield) override;
112  void do_work(yield_type aYieldType = yield_type::NoYield) override;
113  void idle() override;
114  // attributes
115  private:
116  i_thread* iThread;
117  io_service iTimerIoService;
118  io_service iNetworkingIoService;
119  message_queue_pointer iMessageQueue;
120  bool iHalted;
121  };
122 }
yield_type
Definition: i_thread.hpp:42
#define define_declared_event(name, declName,...)
io_service & networking_io_service()
Definition: async_task.hpp:96
static constexpr std::size_t kDefaultPollCount
native_io_service_type & native_object()
Definition: async_task.hpp:63
io_service & timer_io_service()
Definition: async_task.hpp:95
bool do_io(bool aProcessEvents=true, std::size_t aMaximumPollCount=kDefaultPollCount) override
boost::asio::io_service native_io_service_type
Definition: async_task.hpp:56
io_service(async_task &aTask)
Definition: async_task.hpp:59
auto destroying(Object &aObject, const Handler aHandler)
Definition: i_object.hpp:55
auto destroyed(Object &aObject, const Handler aHandler)
Definition: i_object.hpp:64