libcopp  1.1.0
task_impl.cpp
Go to the documentation of this file.
1 /*
2  * task_impl.cpp
3  *
4  * Created on: 2014年4月2日
5  * Author: owent
6  *
7  * Released under the MIT license
8  */
9 
10 #include <algorithm>
11 #include <cstdlib>
12 
13 #include <assert.h>
14 
18 
19 
20 namespace cotask {
21  namespace impl {
22  task_impl::task_impl() : action_(UTIL_CONFIG_NULLPTR), finish_priv_data_(UTIL_CONFIG_NULLPTR), status_(EN_TS_CREATED) {}
23 
24  task_impl::~task_impl() { assert(status_ <= EN_TS_CREATED || status_ >= EN_TS_DONE); }
25 
26  bool task_impl::is_canceled() const UTIL_CONFIG_NOEXCEPT { return EN_TS_CANCELED == get_status(); }
27 
28  bool task_impl::is_completed() const UTIL_CONFIG_NOEXCEPT { return is_exiting(); }
29 
30  bool task_impl::is_faulted() const UTIL_CONFIG_NOEXCEPT { return EN_TS_KILLED <= get_status(); }
31 
32  bool task_impl::is_timeout() const UTIL_CONFIG_NOEXCEPT { return EN_TS_TIMEOUT == get_status(); }
33 
34  bool task_impl::is_exiting() const UTIL_CONFIG_NOEXCEPT { return EN_TS_DONE <= get_status(); }
35 
36  int task_impl::on_finished() { return 0; }
37 
40  if (UTIL_CONFIG_NULLPTR == this_co) {
41  return UTIL_CONFIG_NULLPTR;
42  }
43 
44  if (false == this_co->check_flags(ext_coroutine_flag_t::EN_ECFT_COTASK)) {
45  return UTIL_CONFIG_NULLPTR;
46  }
47 
48  return *((task_impl **)this_co->get_private_buffer());
49  }
50 
52 
54 
56  uint32_t expected_int = expected;
57  bool ret =
59  expected = static_cast<EN_TASK_STATUS>(expected_int);
60  return ret;
61  }
62 
63  int task_impl::_notify_finished(void *priv_data) {
64  finish_priv_data_ = priv_data;
65 
66  _get_action()->on_finished(*this);
67  int ret = on_finished();
68 
69  return ret;
70  }
71  } // namespace impl
72 } // namespace cotask
int _notify_finished(void *priv_data)
Definition: task_impl.cpp:63
virtual bool is_canceled() const UTIL_CONFIG_NOEXCEPT
Definition: task_impl.cpp:26
virtual int on_finished()
Definition: task_impl.cpp:36
bool is_exiting() const UTIL_CONFIG_NOEXCEPT
check if a cotask is exiting
Definition: task_impl.cpp:34
action_ptr_t action_
Definition: task_impl.h:122
bool _cas_status(EN_TASK_STATUS &expected, EN_TASK_STATUS desired)
Definition: task_impl.cpp:55
action_ptr_t _get_action()
Definition: task_impl.cpp:53
::util::lock::atomic_int_type< uint32_t > status_
Definition: task_impl.h:129
EN_TASK_STATUS get_status() const UTIL_CONFIG_NOEXCEPT
Definition: task_impl.h:64
virtual bool is_faulted() const UTIL_CONFIG_NOEXCEPT
Definition: task_impl.cpp:30
EN_TASK_STATUS
Definition: task_impl.h:28
coroutine_context * get_coroutine() UTIL_CONFIG_NOEXCEPT
get current coroutine
void _set_action(action_ptr_t action)
Definition: task_impl.cpp:51
bool compare_exchange_weak(value_type &expected, value_type desired,::util::lock::memory_order success,::util::lock::memory_order failure) UTIL_CONFIG_NOEXCEPT
virtual ~task_impl()=0
Definition: task_impl.cpp:24
void * get_private_buffer() const UTIL_CONFIG_NOEXCEPT
get private buffer(raw pointer)
bool check_flags(int flags) const
check flags
virtual bool is_completed() const UTIL_CONFIG_NOEXCEPT
Definition: task_impl.cpp:28
virtual int on_finished(task_impl &)
virtual bool is_timeout() const UTIL_CONFIG_NOEXCEPT
Definition: task_impl.cpp:32
base type of all coroutine context
static task_impl * this_task()
Definition: task_impl.cpp:38