neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
async_task.hpp
Go to the documentation of this file.
1// async_task.hpp
2/*
3 * Copyright (c) 2007, 2020 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 <atomic>
42#include <neolib/task/task.hpp>
45#include <neolib/task/event.hpp>
46
47namespace neolib
48{
49 class async_task;
50
51 class NEOLIB_EXPORT timer_service : public i_timer_service
52 {
53 // types
54 public:
55 // construction
56 public:
57 timer_service(async_task& aTask, bool aMultiThreaded = false);
58 // operations
59 public:
60 bool poll(bool aProcessEvents = true, std::size_t aMaximumPollCount = kDefaultPollCount) override;
61 void* native_object() override;
63 void remove_timer_object(i_timer_object& aObject) override;
64 // attributes
65 private:
66 async_task& iTask;
67 destroying_flag iTaskDestroying;
68 mutable std::recursive_mutex iMutex;
69 std::vector<ref_ptr<i_timer_object>> iObjects;
70 };
71
73 {
74 Init,
75 Running,
76 Halted,
78 };
79
80 class NEOLIB_EXPORT async_task : public task<reference_counted<i_async_task>>, public lifetime<>
81 {
82 friend class async_thread;
84 // events
85 public:
88 // exceptions
89 public:
90 struct no_thread : std::logic_error { no_thread() : std::logic_error{ "neolib::async_task::no_thread" } {} };
91 // types
92 public:
94 private:
95 typedef std::unique_ptr<i_message_queue> message_queue_pointer;
96 // construction
97 public:
98 async_task(const std::string& aName = std::string{});
99 async_task(i_thread& aThread, const std::string& aName = std::string{});
101 // operations
102 public:
103 i_thread& thread() const override;
104 bool joined() const override;
105 void join(i_thread& aThread) override;
106 void detach() override;
109 bool have_message_queue() const override;
110 bool have_messages() const override;
111 i_message_queue& create_message_queue(std::function<bool()> aIdleFunction = std::function<bool()>()) override;
112 const i_message_queue& message_queue() const override;
116 bool pump_messages() override;
117 bool running() const noexcept override;
118 bool halted() const noexcept override;
119 void halt() override;
120 bool finished() const noexcept override;
121 void wait() const noexcept override;
122 // implementation
123 protected:
124 // i_lifetime
125 void set_destroying() override;
126 void set_destroyed() override;
127 // task
128 void run(yield_type aYieldType = yield_type::NoYield) override;
129 bool do_work(yield_type aYieldType = yield_type::NoYield) override;
130 void cancel() noexcept override;
131 void idle() override;
132 // attributes
133 private:
134 std::recursive_mutex iMutex;
135 std::atomic<i_thread*> iThread;
136 std::optional<neolib::timer_service> iTimerService;
137 std::unique_ptr<i_async_service> iIoService;
138 message_queue_pointer iMessageQueue;
139 std::vector<i_async_event_queue*> iEventQueues;
140 std::atomic<async_task_state> iState;
141 };
142}
void detach() override
neolib::i_async_service & io_service() override
bool joined() const override
bool have_message_queue() const override
void register_event_queue(i_async_event_queue &aQueue) override
void unregister_event_queue(i_async_event_queue &aQueue) override
const i_message_queue & message_queue() const override
bool have_messages() const override
void join(i_thread &aThread) override
i_async_task abstract_type
bool pump_messages() override
neolib::timer_service & timer_service() override
i_message_queue & message_queue() override
async_task(i_thread &aThread, const std::string &aName=std::string{})
async_task(const std::string &aName=std::string{})
define_declared_event(Destroying, destroying) define_declared_event(Destroyed
i_thread & thread() const override
i_message_queue & create_message_queue(std::function< bool()> aIdleFunction=std::function< bool()>()) override
bool running() const noexcept override
void * native_object() override
timer_service(async_task &aTask, bool aMultiThreaded=false)
void remove_timer_object(i_timer_object &aObject) override
i_timer_object & create_timer_object() override
bool poll(bool aProcessEvents=true, std::size_t aMaximumPollCount=kDefaultPollCount) override
auto destroyed(Object &aObject, const Handler aHandler)
Definition i_object.hpp:73
auto destroying(Object &aObject, const Handler aHandler)
Definition i_object.hpp:64
Definition plf_hive.h:79
#define define_declared_event(name, declName,...)
Definition event.hpp:195