Skip to content

Commit

Permalink
cotask - manager 增加统计接口
Browse files Browse the repository at this point in the history
cotask - 增加await接口
  • Loading branch information
owent committed Mar 3, 2018
1 parent faec9aa commit 378d0d2
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 115 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ BasedOnStyle: LLVM
Language: Cpp
AccessModifierOffset: -4
AlignEscapedNewlinesLeft: true
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortIfStatementsOnASingleLine: true
AlwaysBreakTemplateDeclarations: true
Expand Down
29 changes: 17 additions & 12 deletions include/libcopp/utils/errno.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,31 @@ namespace copp {
enum copp_error_code {
COPP_EC_SUCCESS = 0, //!< COPP_EC_SUCCESS

COPP_EC_UNKNOWN = -101, //!< COPP_EC_UNKNOWN
COPP_EC_UNKNOWN = -101, //!< COPP_EC_UNKNOWN
COPP_EC_EXTERNAL_INSERT_FAILED = -102, //!< COPP_EC_EXTERNAL_INSERT_FAILED
COPP_EC_EXTERNAL_ERASE_FAILED = -103, //!< COPP_EC_EXTERNAL_ERASE_FAILED
COPP_EC_IN_RESET = -104, //!< COPP_EC_IN_RESET
COPP_EC_EXTERNAL_ERASE_FAILED = -103, //!< COPP_EC_EXTERNAL_ERASE_FAILED
COPP_EC_IN_RESET = -104, //!< COPP_EC_IN_RESET

COPP_EC_ALLOC_STACK_FAILED = -201, //!< COPP_EC_ALLOC_STACK_FAILED

COPP_EC_NOT_INITED = -1001, //!< COPP_EC_NOT_INITED
COPP_EC_ALREADY_INITED = -1002, //!< COPP_EC_ALREADY_INITED
COPP_EC_NOT_INITED = -1001, //!< COPP_EC_NOT_INITED
COPP_EC_ALREADY_INITED = -1002, //!< COPP_EC_ALREADY_INITED
COPP_EC_ACCESS_VIOLATION = -1003, //!< COPP_EC_ACCESS_VIOLATION
COPP_EC_NOT_READY = -1004, //!< COPP_EC_NOT_READY
COPP_EC_NOT_RUNNING = -1005, //!< COPP_EC_NOT_RUNNING
COPP_EC_IS_RUNNING = -1006, //!< COPP_EC_IS_RUNNING
COPP_EC_NOT_READY = -1004, //!< COPP_EC_NOT_READY
COPP_EC_NOT_RUNNING = -1005, //!< COPP_EC_NOT_RUNNING
COPP_EC_IS_RUNNING = -1006, //!< COPP_EC_IS_RUNNING
COPP_EC_ALREADY_FINISHED = -1007, //!< COPP_EC_ALREADY_FINISHED
COPP_EC_NOT_FOUND = -1008, //!< COPP_EC_NOT_FOUND
COPP_EC_ALREADY_EXIST = -1009, //!< COPP_EC_ALREADY_EXIST
COPP_EC_ARGS_ERROR = -1010, //!< COPP_EC_ARGS_ERROR
COPP_EC_CAST_FAILED = -1011, //!< COPP_EC_CAST_FAILED
COPP_EC_NOT_FOUND = -1008, //!< COPP_EC_NOT_FOUND
COPP_EC_ALREADY_EXIST = -1009, //!< COPP_EC_ALREADY_EXIST
COPP_EC_ARGS_ERROR = -1010, //!< COPP_EC_ARGS_ERROR
COPP_EC_CAST_FAILED = -1011, //!< COPP_EC_CAST_FAILED

COPP_EC_FCONTEXT_MAKE_FAILED = -2001, //!< COPP_EC_FCONTEXT_MAKE_FAILED

COPP_EC_TASK_CAN_NOT_WAIT_SELF = -3001, //!< COPP_EC_TASK_CAN_NOT_WAIT_SELF
COPP_EC_TASK_IS_EXITING = -3002, //!< COPP_EC_TASK_IS_EXITING
COPP_EC_TASK_ADD_NEXT_FAILED = -3003, //!< COPP_EC_TASK_ADD_NEXT_FAILED
COPP_EC_TASK_NOT_IN_ACTION = -3004, //!< COPP_EC_TASK_NOT_IN_ACTION
};
} // namespace copp

Expand Down
6 changes: 3 additions & 3 deletions include/libcotask/impl/task_action_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#ifndef COTASK_IMPL_TASK_ACTION_IMPL_H
#define COTASK_IMPL_TASK_ACTION_IMPL_H
#pragma once
#pragma once

namespace cotask {

Expand All @@ -23,8 +23,8 @@ namespace cotask {
virtual int operator()(void *) = 0;
virtual int on_finished(task_impl &) { return 0; }
};
}
}
} // namespace impl
} // namespace cotask


#endif /* _COTASK_IMPL_TASK_ACTION_IMPL_H_ */
7 changes: 7 additions & 0 deletions include/libcotask/impl/task_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ namespace cotask {
*/
static task_impl *this_task();

/**
* @brief get raw action pointer
* @note this function is provided just for debug or show some information, it may return the inner type created by cotask
* @return pointer to task_action instance
*/
inline action_ptr_t get_raw_action() const UTIL_CONFIG_NOEXCEPT { return action_; }

protected:
void _set_action(action_ptr_t action);
action_ptr_t _get_action();
Expand Down
80 changes: 66 additions & 14 deletions include/libcotask/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,26 @@
#include <algorithm>
#include <stdint.h>


#include <libcopp/stack/stack_traits.h>
#include <libcopp/utils/errno.h>
#include <libcotask/task_macros.h>
#include <libcotask/this_task.h>


namespace cotask {

template <typename TCO_MACRO = macro_coroutine, typename TTASK_MACRO = macro_task>
class task : public impl::task_impl {
public:
typedef task<TCO_MACRO, TTASK_MACRO> self_t;
typedef std::intrusive_ptr<self_t> ptr_t;
typedef TCO_MACRO macro_coroutine_t;
typedef TTASK_MACRO macro_task_t;
typedef std::intrusive_ptr<self_t> ptr_t;
typedef TCO_MACRO macro_coroutine_t;
typedef TTASK_MACRO macro_task_t;

typedef typename macro_coroutine_t::coroutine_t coroutine_t;
typedef typename macro_coroutine_t::coroutine_t coroutine_t;
typedef typename macro_coroutine_t::stack_allocator_t stack_allocator_t;

typedef typename macro_task_t::id_t id_t;
typedef typename macro_task_t::id_t id_t;
typedef typename macro_task_t::id_allocator_t id_allocator_t;


Expand Down Expand Up @@ -79,7 +80,7 @@ namespace cotask {
}

size_t action_size = coroutine_t::align_address_size(sizeof(a_t));
size_t task_size = coroutine_t::align_address_size(sizeof(self_t));
size_t task_size = coroutine_t::align_address_size(sizeof(self_t));

if (stack_size <= sizeof(impl::task_impl *) + private_buffer_size + action_size + task_size) {
return ptr_t();
Expand All @@ -92,7 +93,7 @@ namespace cotask {
}

void *action_addr = sub_buffer_offset(coroutine.get(), action_size);
void *task_addr = sub_buffer_offset(action_addr, task_size);
void *task_addr = sub_buffer_offset(action_addr, task_size);

// placement new task
ptr_t ret(new (task_addr) self_t());
Expand All @@ -101,7 +102,7 @@ namespace cotask {
}

*(reinterpret_cast<impl::task_impl **>(coroutine->get_private_buffer())) = ret.get();
ret->coroutine_obj_ = coroutine;
ret->coroutine_obj_ = coroutine;
ret->coroutine_obj_->set_flags(impl::task_impl::ext_coroutine_flag_t::EN_ECFT_COTASK);

// placement new action
Expand Down Expand Up @@ -220,11 +221,62 @@ namespace cotask {
}
#endif


/**
* @brief await another cotask to finish
* @note please not to make tasks refer to each other. [it will lead to memory leak]
* @note [don't do that] ptr_t a = ..., b = ...; a.await(b); b.await(a);
* @param wait_task which stack to wait for
* @return 0 or error code
*/
inline int await(ptr_t wait_task) {
if (!wait_task) {
return copp::COPP_EC_ARGS_ERROR;
}

if (this == wait_task.get()) {
return copp::COPP_EC_TASK_CAN_NOT_WAIT_SELF;
}

// if target is exiting or completed, just return
if (wait_task->is_exiting() || wait_task->is_completed()) {
return copp::COPP_EC_TASK_IS_EXITING;
}

if (is_exiting()) {
return copp::COPP_EC_TASK_IS_EXITING;
}

if (this_task() != this) {
return copp::COPP_EC_TASK_NOT_IN_ACTION;
}

// add to next list failed
if (wait_task->next(ptr_t(this)).get() != this) {
return copp::COPP_EC_TASK_ADD_NEXT_FAILED;
}

int ret = 0;
while (!(wait_task->is_exiting() || wait_task->is_completed())) {
if (is_exiting()) {
return copp::COPP_EC_TASK_IS_EXITING;
}

ret = yield();
}

return ret;
}

template <typename TTask>
inline int await(TTask *wait_task) {
return await(ptr_t(wait_task));
}

/**
* @brief add next task to run when task finished
* @note please not to make tasks refer to each other. [it will lead to memory leak]
* @note [don't do that] ptr_t a = ..., b = ...; a.next(b); b.next(a);
* @see impl::task_impl::next
* @param next_task next stack
* @param priv_data priv_data passed to resume or start next stack
* @return next_task if success , or self if failed
Expand Down Expand Up @@ -340,7 +392,7 @@ namespace cotask {
id_alloc_.deallocate(id_);
}

inline typename coroutine_t::ptr_t &get_coroutine_context() UTIL_CONFIG_NOEXCEPT { return coroutine_obj_; }
inline typename coroutine_t::ptr_t & get_coroutine_context() UTIL_CONFIG_NOEXCEPT { return coroutine_obj_; }
inline const typename coroutine_t::ptr_t &get_coroutine_context() const UTIL_CONFIG_NOEXCEPT { return coroutine_obj_; }

inline id_t get_id() const UTIL_CONFIG_NOEXCEPT { return id_; }
Expand Down Expand Up @@ -575,16 +627,16 @@ namespace cotask {
}

private:
id_t id_;
id_t id_;
typename coroutine_t::ptr_t coroutine_obj_;
task_group next_list_;
task_group next_list_;

// ============== action information ==============
void (*action_destroy_fn_)(void *);

#if !defined(PROJECT_DISABLE_MT) || !(PROJECT_DISABLE_MT)
util::lock::atomic_int_type<size_t> ref_count_; /** ref_count **/
util::lock::spin_lock next_list_lock_;
util::lock::spin_lock next_list_lock_;
#else
util::lock::atomic_int_type<util::lock::unsafe_int_type<size_t> > ref_count_; /** ref_count **/
#endif
Expand Down
Loading

0 comments on commit 378d0d2

Please sign in to comment.