libcopp  2.2.0
task_impl.h
Go to the documentation of this file.
1 // Copyright 2023 owent
2 
3 #pragma once
4 
5 #include <libcopp/utils/config/libcopp_build_features.h>
6 
8 
10 
13 
14 #include <libcotask/task_actions.h>
15 
16 // clang-format off
17 #include <libcopp/utils/config/stl_include_prefix.h> // NOLINT(build/include_order)
18 // clang-format on
19 #include <stdint.h>
20 #include <list>
21 #include <memory>
22 // clang-format off
23 #include <libcopp/utils/config/stl_include_suffix.h> // NOLINT(build/include_order)
24 // clang-format on
25 
26 LIBCOPP_COTASK_NAMESPACE_BEGIN
36 };
37 
38 namespace impl {
39 
41  public:
43  using id_allocator_type = LIBCOPP_COPP_NAMESPACE_ID::util::uint64_id_allocator;
44 
45  // Compability with libcopp-1.x
46  using id_t = id_type;
48 
49  protected:
51 
52  // Compability with libcopp-1.x
54 
55  struct LIBCOPP_COTASK_API ext_coroutine_flag_t {
56  enum type {
57  EN_ECFT_UNKNOWN = 0,
58  EN_ECFT_COTASK = 0x0100,
59  EN_ECFT_MASK = 0xFF00,
60  };
61  };
62 
63  private:
64  task_impl(const task_impl &) = delete;
65  task_impl &operator=(const task_impl &) = delete;
66 
67  task_impl(const task_impl &&) = delete;
68  task_impl &operator=(const task_impl &&) = delete;
69 
70  public:
71  LIBCOPP_COTASK_API task_impl();
72  LIBCOPP_COTASK_API virtual ~task_impl() = 0;
73 
74  UTIL_FORCEINLINE id_type get_id() const LIBCOPP_MACRO_NOEXCEPT { return id_; }
75 
80  UTIL_FORCEINLINE EN_TASK_STATUS get_status() const LIBCOPP_MACRO_NOEXCEPT {
82  }
83 
84  LIBCOPP_COTASK_API virtual bool is_canceled() const LIBCOPP_MACRO_NOEXCEPT;
85  LIBCOPP_COTASK_API virtual bool is_completed() const LIBCOPP_MACRO_NOEXCEPT;
86  LIBCOPP_COTASK_API virtual bool is_faulted() const LIBCOPP_MACRO_NOEXCEPT;
87  LIBCOPP_COTASK_API virtual bool is_timeout() const LIBCOPP_MACRO_NOEXCEPT;
95  LIBCOPP_COTASK_API bool is_exiting() const LIBCOPP_MACRO_NOEXCEPT;
96 
97  public:
98  LIBCOPP_COTASK_API virtual int get_ret_code() const = 0;
99 
100  virtual int start(void *priv_data, EN_TASK_STATUS expected_status = EN_TS_CREATED) = 0;
101  virtual int resume(void *priv_data, EN_TASK_STATUS expected_status = EN_TS_WAITING) = 0;
102  virtual int yield(void **priv_data) = 0;
103  virtual int cancel(void *priv_data) = 0;
104  virtual int kill(enum EN_TASK_STATUS status, void *priv_data) = 0;
105  UTIL_FORCEINLINE int kill(void *priv_data) { return kill(EN_TS_KILLED, priv_data); }
106 
107  UTIL_FORCEINLINE int start() { return start(nullptr); }
108  UTIL_FORCEINLINE int resume() { return resume(nullptr); }
109  UTIL_FORCEINLINE int yield() { return yield(nullptr); }
110  UTIL_FORCEINLINE int cancel() { return cancel(nullptr); }
111  UTIL_FORCEINLINE int kill(enum EN_TASK_STATUS status) { return kill(status, nullptr); }
112  UTIL_FORCEINLINE int kill() { return kill(nullptr); }
113 
114  LIBCOPP_COTASK_API virtual int on_finished();
115 
116 #if defined(LIBCOPP_MACRO_ENABLE_WIN_FIBER) && LIBCOPP_MACRO_ENABLE_WIN_FIBER
117  virtual bool is_fiber() const LIBCOPP_MACRO_NOEXCEPT = 0;
118 #endif
119 
124  static LIBCOPP_COTASK_API task_impl *this_task();
125 
132  UTIL_FORCEINLINE action_ptr_type get_raw_action() const LIBCOPP_MACRO_NOEXCEPT { return action_; }
133 
134  protected:
135  LIBCOPP_COTASK_API void _set_action(action_ptr_type action);
136  LIBCOPP_COTASK_API action_ptr_type _get_action();
137 
138  LIBCOPP_COTASK_API bool _cas_status(EN_TASK_STATUS &expected, EN_TASK_STATUS desired);
139 
140 #if defined(LIBCOPP_MACRO_ENABLE_STD_EXCEPTION_PTR) && LIBCOPP_MACRO_ENABLE_STD_EXCEPTION_PTR
141  LIBCOPP_COTASK_API int _notify_finished(std::list<std::exception_ptr> &unhandled, void *priv_data);
142 #else
143  LIBCOPP_COTASK_API int _notify_finished(void *priv_data);
144 #endif
145 
146  private:
149 
150  protected:
152 
153  private:
154 #if !defined(LIBCOPP_DISABLE_ATOMIC_LOCK) || !(LIBCOPP_DISABLE_ATOMIC_LOCK)
155  LIBCOPP_COPP_NAMESPACE_ID::util::lock::atomic_int_type<uint32_t> status_;
156 #else
157  LIBCOPP_COPP_NAMESPACE_ID::util::lock::atomic_int_type<
158  LIBCOPP_COPP_NAMESPACE_ID::util::lock::unsafe_int_type<uint32_t> >
159  status_;
160 #endif
161 };
162 } // namespace impl
163 LIBCOPP_COTASK_NAMESPACE_END
atomic wrapper fo integers Licensed under the MIT licenses.
UTIL_FORCEINLINE int start()
Definition: task_impl.h:107
id_allocator_type id_allocator_t
Definition: task_impl.h:47
LIBCOPP_COPP_NAMESPACE_ID::util::lock::atomic_int_type< uint32_t > status_
Definition: task_impl.h:155
LIBCOPP_COPP_NAMESPACE_ID::util::uint64_id_allocator id_allocator_type
Definition: task_impl.h:43
UTIL_FORCEINLINE int resume()
Definition: task_impl.h:108
id_type id_t
Definition: task_impl.h:46
task_impl & operator=(const task_impl &&)=delete
UTIL_FORCEINLINE int yield()
Definition: task_impl.h:109
task_impl(const task_impl &)=delete
UTIL_FORCEINLINE action_ptr_type get_raw_action() const LIBCOPP_MACRO_NOEXCEPT
get raw action pointer
Definition: task_impl.h:132
task_impl(const task_impl &&)=delete
LIBCOPP_COPP_NAMESPACE_ID::util::uint64_id_allocator::value_type id_type
Definition: task_impl.h:42
UTIL_FORCEINLINE id_type get_id() const LIBCOPP_MACRO_NOEXCEPT
Definition: task_impl.h:74
UTIL_FORCEINLINE EN_TASK_STATUS get_status() const LIBCOPP_MACRO_NOEXCEPT
Definition: task_impl.h:80
void * finish_priv_data_
Definition: task_impl.h:151
UTIL_FORCEINLINE int cancel()
Definition: task_impl.h:110
UTIL_FORCEINLINE int kill()
Definition: task_impl.h:112
action_ptr_type action_
Definition: task_impl.h:147
UTIL_FORCEINLINE int kill(void *priv_data)
Definition: task_impl.h:105
UTIL_FORCEINLINE int kill(enum EN_TASK_STATUS status)
Definition: task_impl.h:111
task_impl & operator=(const task_impl &)=delete
#define UTIL_FORCEINLINE
#define UTIL_SYMBOL_VISIBLE
锁管理器 Licensed under the MIT licenses.
LIBCOPP_COPP_API int yield(void **priv_data=nullptr) LIBCOPP_MACRO_NOEXCEPT
yield current coroutine
std::shared_ptr< cli::cmd_option_value > value_type
Definition: cmd_option.h:50
自旋锁 Licensed under the MIT licenses.
EN_TASK_STATUS
Definition: task_impl.h:27
@ EN_TS_DONE
Definition: task_impl.h:32
@ EN_TS_CREATED
Definition: task_impl.h:29
@ EN_TS_TIMEOUT
Definition: task_impl.h:35
@ EN_TS_KILLED
Definition: task_impl.h:34
@ EN_TS_INVALID
Definition: task_impl.h:28
@ EN_TS_WAITING
Definition: task_impl.h:31
@ EN_TS_RUNNING
Definition: task_impl.h:30
@ EN_TS_CANCELED
Definition: task_impl.h:33