static inline bool s_check_is_canceled(lagopus_thread_t *thd_ptr, lagopus_result_t require_ret, bool require_result) { bool result = !require_result; lagopus_result_t ret; int i; for (i = 0; i < GET_TID_RETRY; i++) { ret = lagopus_thread_is_canceled(thd_ptr, &result); if (ret == require_ret) { break; } } if (ret == require_ret) { if (ret == LAGOPUS_RESULT_OK) { result = (result == require_result); } else { result = true; } } else { lagopus_perror(ret); TEST_FAIL_MESSAGE("is_canceled failed"); result = false; } return result; }
static inline lagopus_result_t s_wait_module(a_module *mptr, lagopus_chrono_t nsec) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; if (mptr != NULL) { if ((mptr->m_status == MODULE_STATE_CANCELLING || mptr->m_status == MODULE_STATE_STARTED) && mptr->m_thdptr != NULL) { ret = lagopus_thread_wait(mptr->m_thdptr, nsec); if (ret == LAGOPUS_RESULT_OK) { bool is_canceled = false; (void)lagopus_thread_is_canceled(mptr->m_thdptr, &is_canceled); mptr->m_status = (is_canceled == false) ? MODULE_STATE_SHUTDOWN : MODULE_STATE_STOPPED; } } else { if (mptr->m_status == MODULE_STATE_SHUTDOWN || mptr->m_status == MODULE_STATE_STOPPED) { ret = LAGOPUS_RESULT_OK; } else { ret = LAGOPUS_RESULT_INVALID_STATE_TRANSITION; } } } else { ret = LAGOPUS_RESULT_INVALID_ARGS; } return ret; }
lagopus_result_t dp_thread_stop(lagopus_thread_t *threadptr, bool *runptr) { bool is_canceled = false; lagopus_result_t ret; ret = lagopus_thread_is_canceled(threadptr, &is_canceled); if (ret == LAGOPUS_RESULT_OK && is_canceled == false) { ret = lagopus_thread_cancel(threadptr); } *runptr = false; return ret; }
static void s_check_cancelled(lagopus_thread_t *thread, bool require) { bool canceled = !require; lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; if ((ret = lagopus_thread_is_canceled(thread, &canceled)) != LAGOPUS_RESULT_OK) { lagopus_perror(ret); lagopus_msg_error("Can't check is_cancelled.\n"); } else if (canceled != require) { lagopus_msg_error( "is_cancelled() required %s, but %s.\n", BOOL_TO_STR(require), BOOL_TO_STR(canceled)); } }
void dp_thread_finalize(lagopus_thread_t *threadptr) { bool is_valid = false, is_canceled = false; lagopus_result_t ret; ret = lagopus_thread_is_valid(threadptr, &is_valid); if (ret != LAGOPUS_RESULT_OK || is_valid == false) { lagopus_perror(ret); return; } ret = lagopus_thread_is_canceled(threadptr, &is_canceled); if (ret == LAGOPUS_RESULT_OK && is_canceled == false) { global_state_cancel_janitor(); } lagopus_thread_destroy(threadptr); return; }
int main(int argc, const char *const argv[]) { test_thread_record ttr; test_thread_t tt; bool is_canceled; int ret = 1; (void)argc; (void)argv; fprintf(stderr, "Pthread wrapper check: start\n\n"); /* * Heap object */ fprintf(stderr, "Heap object, regular: start\n"); tt = NULL; if (test_thread_create(&tt, 100) != true) { lagopus_exit_fatal("Can't create a test thread object.\n"); } if (lagopus_thread_start((lagopus_thread_t *)&tt, false) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't start a thread.\n"); } lagopus_chrono_nanosleep(5LL * 1000LL * 1000LL * 1000LL, NULL); test_thread_request_stop(tt); if (lagopus_thread_wait((lagopus_thread_t *)&tt, -1) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't wait the thread.\n"); } lagopus_thread_destroy((lagopus_thread_t *)&tt); fprintf(stderr, "Heap object, regular: end\n\n"); /* * Stack object */ fprintf(stderr, "Stack object, regular: start\n"); tt = &ttr; if (test_thread_create(&tt, 100) != true) { lagopus_exit_fatal("Can't initialize a test thread object.\n"); } if (lagopus_thread_start((lagopus_thread_t *)&tt, false) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't start a thread.\n"); } lagopus_chrono_nanosleep(5LL * 1000LL * 1000LL * 1000LL, NULL); test_thread_request_stop(tt); if (lagopus_thread_wait((lagopus_thread_t *)&tt, -1) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't wait the thread.\n"); } lagopus_thread_destroy((lagopus_thread_t *)&tt); fprintf(stderr, "Stack object, regular: end\n\n"); /* * Cancel */ fprintf(stderr, "Stack object, cancel: start\n"); tt = &ttr; if (test_thread_create(&tt, 100) != true) { lagopus_exit_fatal("Can't initialize a test thread object.\n"); } if (lagopus_thread_start((lagopus_thread_t *)&tt, false) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't start a thread.\n"); } lagopus_chrono_nanosleep(5LL * 1000LL * 1000LL * 1000LL, NULL); if (lagopus_thread_cancel((lagopus_thread_t *)&tt) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't cancel the thread.\n"); } if (lagopus_thread_wait((lagopus_thread_t *)&tt, -1) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't wait the thread.\n"); } if (lagopus_thread_is_canceled((lagopus_thread_t *)&tt, &is_canceled) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("The returned value must be \"canceled\".\n"); } lagopus_thread_destroy((lagopus_thread_t *)&tt); fprintf(stderr, "Stack object, cancel: end\n\n"); /* * Heap object auto deletion */ fprintf(stderr, "Heap object, auto deletion: start\n"); s_is_deleted = false; tt = NULL; if (test_thread_create(&tt, 100) != true) { lagopus_exit_fatal("Can't initialize a test thread object.\n"); } if (lagopus_thread_start((lagopus_thread_t *)&tt, true) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't start a thread.\n"); } lagopus_chrono_nanosleep(5LL * 1000LL * 1000LL * 1000LL, NULL); test_thread_request_stop(tt); lagopus_chrono_nanosleep(2LL * 1000LL * 1000LL * 1000LL, NULL); if (s_is_deleted == false) { lagopus_exit_fatal("The thread object must be freed and " "the flag must be true.\n"); } fprintf(stderr, "Heap object, auto deletion: end\n\n"); /* * Stack object auto deletion */ fprintf(stderr, "Stack object, auto deletion: start\n"); s_is_deleted = false; tt = &ttr; if (test_thread_create(&tt, 100) != true) { lagopus_exit_fatal("Can't initialize a test thread object.\n"); } if (lagopus_thread_start((lagopus_thread_t *)&tt, true) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't start a thread.\n"); } lagopus_chrono_nanosleep(5LL * 1000LL * 1000LL * 1000LL, NULL); test_thread_request_stop(tt); lagopus_chrono_nanosleep(2LL * 1000LL * 1000LL * 1000LL, NULL); if (s_is_deleted == false) { lagopus_exit_fatal("The thread object must be freed and " "the flag must be true.\n"); } fprintf(stderr, "Stack object, auto deletion: end\n\n"); /* * Heap object cancel, auto deletion */ fprintf(stderr, "Heap object, cancel, auto deletion: start\n"); s_is_deleted = false; tt = NULL; if (test_thread_create(&tt, 100) != true) { lagopus_exit_fatal("Can't initialize a test thread object.\n"); } if (lagopus_thread_start((lagopus_thread_t *)&tt, true) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't start a thread.\n"); } lagopus_chrono_nanosleep(5LL * 1000LL * 1000LL * 1000LL, NULL); if (lagopus_thread_cancel((lagopus_thread_t *)&tt) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't cancel the thread.\n"); } lagopus_chrono_nanosleep(2LL * 1000LL * 1000LL * 1000LL, NULL); if (s_is_deleted == false) { lagopus_exit_fatal("The thread object must be freed and " "the flag must be true.\n"); } fprintf(stderr, "Heap object, cancel, auto deletion: end\n\n"); /* * Stack object cancel, auto deletion */ fprintf(stderr, "Stack object, cancel, auto deletion: start\n"); s_is_deleted = false; tt = &ttr; if (test_thread_create(&tt, 100) != true) { lagopus_exit_fatal("Can't initialize a test thread object.\n"); } if (lagopus_thread_start((lagopus_thread_t *)&tt, true) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't start a thread.\n"); } lagopus_chrono_nanosleep(5LL * 1000LL * 1000LL * 1000LL, NULL); if (lagopus_thread_cancel((lagopus_thread_t *)&tt) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't cancel the thread.\n"); } lagopus_chrono_nanosleep(2LL * 1000LL * 1000LL * 1000LL, NULL); if (s_is_deleted == false) { lagopus_exit_fatal("The thread object must be freed and " "the flag must be true.\n"); } fprintf(stderr, "Stack object, cancel, auto deletion: end\n\n"); /* * Timed wait */ fprintf(stderr, "Timed wait: start\n"); tt = &ttr; if (test_thread_create(&tt, 100) != true) { lagopus_exit_fatal("Can't initialize a test thread object.\n"); } if (lagopus_thread_start((lagopus_thread_t *)&tt, false) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't start a thread.\n"); } if (lagopus_thread_wait((lagopus_thread_t *)&tt, 5LL * 1000LL * 1000LL * 1000LL) != LAGOPUS_RESULT_TIMEDOUT) { lagopus_exit_fatal("Must be timed out.\n"); } test_thread_request_stop(tt); if (lagopus_thread_wait((lagopus_thread_t *)&tt, -1) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't wait the thread.\n"); } lagopus_thread_destroy((lagopus_thread_t *)&tt); fprintf(stderr, "Timed wait: end\n\n"); /* * Force destroy */ fprintf(stderr, "Force destroy: start\n"); s_is_deleted = false; tt = NULL; if (test_thread_create(&tt, 100) != true) { lagopus_exit_fatal("Can't create a test thread object.\n"); } if (lagopus_thread_start((lagopus_thread_t *)&tt, false) != LAGOPUS_RESULT_OK) { lagopus_exit_fatal("Can't start a thread.\n"); } lagopus_thread_destroy((lagopus_thread_t *)&tt); if (s_is_deleted == false) { lagopus_exit_fatal("The thread object must be freed and " "the flag must be true.\n"); } fprintf(stderr, "Force destroy: end\n\n"); /* * Force destroy, not started */ fprintf(stderr, "Force destroy, not started: start\n"); s_is_deleted = false; tt = NULL; if (test_thread_create(&tt, 100) != true) { lagopus_exit_fatal("Can't create a test thread object.\n"); } lagopus_thread_destroy((lagopus_thread_t *)&tt); if (s_is_deleted == false) { lagopus_exit_fatal("The thread object must be freed and " "the flag must be true.\n"); } fprintf(stderr, "Force destroy, not started: end\n\n"); ret = 0; fprintf(stderr, "Pthread wrapper check: end\n"); return ret; }