File: Task.h

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (147 lines) | stat: -rw-r--r-- 2,430 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#pragma once

#if defined(WITH_COROUTINES)
#	include <coroutine>
#endif

#include <optional>

namespace nCine
{
	/// Represents an asynchronous operation
	template <typename T>
	struct Task
	{

#if defined(WITH_COROUTINES) || defined(DOXYGEN_GENERATING_OUTPUT)

		struct task_promise;

		using promise_type = task_promise;
		using handle_type = std::coroutine_handle<promise_type>;

#ifndef DOXYGEN_GENERATING_OUTPUT
		mutable handle_type m_handle;
#endif

		Task(handle_type handle)
			: m_handle(handle)
		{
		}

		Task(Task&& other) noexcept
			: m_handle(other.m_handle)
		{
			other.m_handle = nullptr;
		}

		~Task()
		{
			if (m_handle)
				m_handle.destroy();
		}

		bool await_ready()
		{
			return (!m_handle || m_handle.done());
		}

		bool await_suspend(std::coroutine_handle<> handle)
		{
			return true;
		}

		bool await_suspend(std::coroutine_handle<promise_type> handle)
		{
			handle.promise().m_inner_handler = m_handle;
			m_handle.promise().m_outer_handler = handle;
			return true;
		}

		auto await_resume()
		{
			return *m_handle.promise().m_value;
		}

		// Manualy wait for finish
		bool one_step()
		{
			auto curr = m_handle;
			while (curr) {
				if (!curr.promise().m_inner_handler) {
					while (!curr.done()) {
						curr.resume();
						if (!curr.done()) {
							return true;
						}
						if (curr.promise().m_outer_handler) {
							curr = curr.promise().m_outer_handler;
							curr.promise().m_inner_handler = nullptr;
						} else {
							return false;
						}
					}
					break;
				}
				curr = curr.promise().m_inner_handler;
			}
			return !curr.done();
		}

		struct task_promise
		{
#ifndef DOXYGEN_GENERATING_OUTPUT
			std::optional<T> m_value{};
			std::coroutine_handle<promise_type> m_inner_handler{};
			std::coroutine_handle<promise_type> m_outer_handler{};
#endif

			auto value()
			{
				return m_value;
			}

			auto initial_suspend()
			{
				return std::suspend_never{};
			}

			auto final_suspend() noexcept
			{
				return std::suspend_always{};
			}

			void return_value(T t)
			{
				m_value = t;
				//return std::suspend_always{};
			}

			Task<T> get_return_object()
			{
				return { handle_type::from_promise(*this) };
			}

			void unhandled_exception()
			{
				std::terminate();
			}

			void rethrow_if_unhandled_exception()
			{
			}
		};
#else

		Task(T value) : _value(value) { }

		operator T() const {
			return _value;
		}

		T _value;

#endif

	};
}