Skip to content

Commit

Permalink
fix BUG owent#4 owent#4
Browse files Browse the repository at this point in the history
  • Loading branch information
龚灵放 committed Jan 3, 2017
1 parent 0fde2b9 commit 015d385
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 31 deletions.
56 changes: 36 additions & 20 deletions src/libcopp/coroutine/coroutine_context_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ namespace copp {

// already running
if (status_t::EN_CRS_RUNNING == from_status) {
return COPP_EC_SUCCESS;
return COPP_EC_IS_RUNNING;
}
}
} while (true);
Expand All @@ -137,6 +137,18 @@ namespace copp {
jump_to(callee_, callee_stack_, callee_stack_, jump_data);
#endif

// [BUG #4](https://github.com/owt5008137/libcopp/issues/4)
// Move changing status to the end of start(private data)
{
int from_status = status_.load();
if (status_t::EN_CRS_RUNNING == from_status) {
status_.compare_exchange_strong(from_status, status_t::EN_CRS_READY);
} else if (status_t::EN_CRS_FINISHED == from_status) {
// if in finished status, change it to exited
status_.store(status_t::EN_CRS_EXITED);
}
}

return COPP_EC_SUCCESS;
}

Expand Down Expand Up @@ -187,8 +199,8 @@ namespace copp {

copp::fcontext::transfer_t res;
jump_src_data_t *jump_src;
int from_status;
bool swap_success;
//int from_status;
//bool swap_success;
// can not use any more stack now
// can not initialize those vars here

Expand Down Expand Up @@ -236,28 +248,31 @@ namespace copp {

if (UTIL_CONFIG_NULLPTR != jump_src->from_co) {
jump_src->from_co->callee_ = res.fctx;
from_status = jump_src->from_co->status_.load();
if (status_t::EN_CRS_RUNNING == from_status) {
jump_src->from_co->status_.compare_exchange_strong(from_status, status_t::EN_CRS_READY);
} else if (status_t::EN_CRS_FINISHED == from_status) {
// if in finished status, change it to exited
jump_src->from_co->status_.store(status_t::EN_CRS_EXITED);
}
// [BUG #4](https://github.com/owt5008137/libcopp/issues/4)
// from_status = jump_src->from_co->status_.load();
// if (status_t::EN_CRS_RUNNING == from_status) {
// jump_src->from_co->status_.compare_exchange_strong(from_status, status_t::EN_CRS_READY);
// } else if (status_t::EN_CRS_FINISHED == from_status) {
// // if in finished status, change it to exited
// jump_src->from_co->status_.store(status_t::EN_CRS_EXITED);
// }
}

// private data
jump_transfer.priv_data = jump_src->priv_data;

// this_coroutine
set_this_coroutine_context(jump_transfer.from_co);
// resume running status of from_co
if (NULL != jump_transfer.from_co) {
from_status = jump_transfer.from_co->status_.load();
swap_success = false;
while (!swap_success && status_t::EN_CRS_READY == from_status) {
swap_success = jump_transfer.from_co->status_.compare_exchange_strong(from_status, status_t::EN_CRS_RUNNING);
}
}

// [BUG #4](https://github.com/owt5008137/libcopp/issues/4)
// // resume running status of from_co
// if (NULL != jump_transfer.from_co) {
// from_status = jump_transfer.from_co->status_.load();
// swap_success = false;
// while (!swap_success && status_t::EN_CRS_READY == from_status) {
// swap_success = jump_transfer.from_co->status_.compare_exchange_strong(from_status, status_t::EN_CRS_RUNNING);
// }
// }
}

void coroutine_context_base::coroutine_context_callback(::copp::fcontext::transfer_t src_ctx) {
Expand All @@ -284,8 +299,9 @@ namespace copp {
// save from_co's fcontext and switch status
if (UTIL_CONFIG_NULLPTR != jump_src.from_co) {
jump_src.from_co->callee_ = src_ctx.fctx;
int from_status = status_t::EN_CRS_RUNNING; // from coroutine change status from running to ready
jump_src.from_co->status_.compare_exchange_strong(from_status, status_t::EN_CRS_READY);
// [BUG #4](https://github.com/owt5008137/libcopp/issues/4)
// int from_status = status_t::EN_CRS_RUNNING; // from coroutine change status from running to ready
// jump_src.from_co->status_.compare_exchange_strong(from_status, status_t::EN_CRS_READY);
}

// this_coroutine
Expand Down
10 changes: 5 additions & 5 deletions test/case/coroutine_context_base_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class test_context_base_foo_runner : public copp::coroutine_runnable_base

CASE_TEST(coroutine, context_base)
{
char* stack_buff = new char[64 * 1024];
char* stack_buff = new char[128 * 1024];
g_test_coroutine_base_status = 0;
++ g_test_coroutine_base_status;
CASE_EXPECT_EQ(g_test_coroutine_base_status, 1);
Expand All @@ -38,9 +38,9 @@ CASE_TEST(coroutine, context_base)
test_context_base_foo_runner runner;
runner.call_times = 0;

co.get_allocator().attach(stack_buff, 64 * 1024);
co.create(&runner, 64 * 1024);
int res = co.create(&runner, 64 * 1024); // can not be created muli times
co.get_allocator().attach(stack_buff, 128 * 1024);
co.create(&runner, 128 * 1024);
int res = co.create(&runner, 128 * 1024); // can not be created muli times
CASE_EXPECT_EQ(res, copp::COPP_EC_ALREADY_INITED);

co.start();
Expand All @@ -58,7 +58,7 @@ CASE_TEST(coroutine, context_base)

CASE_TEST(coroutine, shared_runner)
{
const int stack_len = 16 * 1024;
const int stack_len = 128 * 1024;
char* stack_buff = new char[4 * stack_len];
g_test_coroutine_base_status = 0;
++ g_test_coroutine_base_status;
Expand Down
48 changes: 44 additions & 4 deletions test/case/coroutine_context_this_coroutine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ CASE_TEST(this_context, get_coroutine) {

co_type co_arr[5];
for (co_type &co : co_arr) {
co.create(new test_this_context_get_cotoutine_runner(), 32 * 1024);
co.create(new test_this_context_get_cotoutine_runner(), 128 * 1024);

th_pool.push_back(std::thread(std::bind(test_this_context_thread_func, std::ref(co))));
}
Expand Down Expand Up @@ -137,7 +137,7 @@ CASE_TEST(this_context, yield_) {
typedef copp::coroutine_context_default co_type;

co_type co;
co.create(new test_this_context_yield_runner(), 32 * 1024);
co.create(new test_this_context_yield_runner(), 128 * 1024);
co.set_private_data(&co);

CASE_EXPECT_EQ(NULL, copp::this_coroutine::get_coroutine());
Expand Down Expand Up @@ -209,8 +209,8 @@ CASE_TEST(this_context, start_in_co) {
co_type co1, co2;
test_this_context_rec_runner cor1(&co1), cor2(&co2);

co1.create(&cor1, 32 * 1024);
co2.create(&cor2, 32 * 1024);
co1.create(&cor1, 128 * 1024);
co2.create(&cor2, 128 * 1024);
co1.set_private_data(co_startup);
co2.set_private_data(co_startup);

Expand All @@ -231,5 +231,45 @@ CASE_TEST(this_context, start_in_co) {
CASE_EXPECT_EQ(&co2, co_startup[1]);
}

struct test_this_context_start_into_yield_runner : public copp::coroutine_runnable_base {
typedef copp::coroutine_context_default value_type;

test_this_context_start_into_yield_runner(): is_start(false) {}
int operator()() {
copp::detail::coroutine_context_base *ptr = copp::this_coroutine::get_coroutine();

value_type *co_jump = reinterpret_cast<value_type *>(ptr->get_private_data());

if (is_start) {
CASE_EXPECT_NE(NULL, co_jump);
co_jump->start();
} else {
CASE_EXPECT_NE(NULL, co_jump);
int res = co_jump->start(); // this should be COPP_EC_IS_RUNNING
CASE_EXPECT_EQ(copp::COPP_EC_IS_RUNNING, res);
}

// finished and return to caller
return 0;
}

bool is_start;
};

CASE_TEST(this_context, start_turn_into_yield) {
typedef test_this_context_start_into_yield_runner::value_type co_type;

co_type co1, co2;
test_this_context_start_into_yield_runner cor1, cor2;

co1.create(&cor1, 128 * 1024);
co2.create(&cor2, 128 * 1024);
co1.set_private_data(&co2);
co2.set_private_data(&co1);

cor1.is_start = true;
co1.start();
}


#endif
4 changes: 2 additions & 2 deletions test/case/fcontext_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@


copp::fcontext::fcontext_t test_core_fcontext_main_func, test_core_fcontext_a_func, test_core_fcontext_b_func;
char test_core_fcontext_stack_a[64 * 1024] = { 0 };
char test_core_fcontext_stack_b[64 * 1024] = { 0 };
char test_core_fcontext_stack_a[128 * 1024] = { 0 };
char test_core_fcontext_stack_b[128 * 1024] = { 0 };
int g_test_core_fcontext_status = 0;

void test_core_fcontext_func_a(::copp::fcontext::transfer_t src)
Expand Down

0 comments on commit 015d385

Please sign in to comment.