void test_thread_wait_params(void) { TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_INVALID_ARGS, lagopus_thread_wait(NULL, WAIT_NSEC), "thread is NULL"); TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_INVALID_ARGS, lagopus_thread_wait(NULL, -1), "thread is NULL"); }
int MAIN(int argc, char *argv[]) { struct datapath_arg dparg; struct dpmgr *dpmgr; lagopus_thread_t *datapath; struct bridge *bridge; int portid; void *rv; int ret; dpmgr = dpmgr_alloc(); if (dpmgr == NULL) { rte_exit(EXIT_FAILURE, "failed to initialize datapath manager.\n"); } /* Create default bridge. */ ret = dpmgr_bridge_add(dpmgr, "br0", 0); if (ret < 0) { rte_exit(EXIT_FAILURE, "Adding br0 failed\n"); } /* register flow entries */ bridge = dpmgr_bridge_lookup(dpmgr, "br0"); register_flow(bridge->flowdb, 100000); flowdb_switch_mode_set(bridge->flowdb, SWITCH_MODE_OPENFLOW); /* Start datapath. */ dparg.dpmgr = dpmgr; dparg.argc = argc; dparg.argv = argv; datapath_initialize(&dparg, &datapath); /* register all physical ports */ for (portid = 0; portid < nb_ports; portid++) { if (lagopus_is_portid_enabled(portid) == true) { struct port nport; /* New port API. */ nport.ofp_port.port_no = (uint32_t)portid + 1; nport.ifindex = (uint32_t)portid; printf("port id %u\n", nport.ofp_port.port_no); snprintf(nport.ofp_port.name , sizeof(nport.ofp_port.name), "eth%d", portid); dpmgr_port_add(dpmgr, &nport); } } /* assign all ports to br0 */ for (portid = 0; portid < nb_ports; portid++) { dpmgr_bridge_port_add(dpmgr, "br0", (uint32_t)portid + 1); } datapath_start(); while (lagopus_thread_wait(datapath, 1000000) == LAGOPUS_RESULT_TIMEDOUT) { sleep(1); } return 0; }
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; }
static inline void s_destroy_all(void) { if (is_child == true) { s_stop_all(); (void)lagopus_mutex_lock(&stop_lock); { size_t i; test_thread_t tt; /* * Wait the threads done and delete it. */ if (tts != NULL) { for (i = 0; i < n_created_thds; i++) { tt = &(tts[i]); (void)lagopus_thread_wait((lagopus_thread_t *)&tt, -1LL); (void)lagopus_thread_destroy((lagopus_thread_t *)&tt); } free((void *)tts); tts = NULL; } if (polls != NULL) { for (i = 0; i < n_created_polls; i++) { lagopus_qmuxer_poll_destroy(&(polls[i])); } free((void *)polls); polls = NULL; } if (bbqs != NULL) { for (i = 0; i < n_created_bbqs; i++) { lagopus_bbq_destroy(&(bbqs[i]), true); } free((void *)bbqs); bbqs = NULL; } if (qmx != NULL) { lagopus_qmuxer_destroy(&qmx); qmx = NULL; } if (start_lock != NULL) { lagopus_mutex_destroy(&start_lock); start_lock = NULL; } } (void)lagopus_mutex_unlock(&stop_lock); if (stop_lock != NULL) { lagopus_mutex_destroy(&stop_lock); stop_lock = NULL; } } }
static inline bool s_check_wait(lagopus_thread_t *thd_ptr, lagopus_result_t require_ret) { bool result = false; lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; ret = lagopus_thread_wait(thd_ptr, WAIT_NSEC); if (ret != require_ret) { lagopus_perror(ret); TEST_FAIL_MESSAGE("wait failed"); result = false; } else { result = true; } return result; }
static lagopus_result_t s_shutdown_ofp_handler(void) { lagopus_result_t res = LAGOPUS_RESULT_ANY_FAILURES; res = ofp_handler_shutdown(SHUTDOWN_GRACEFULLY); if (res != LAGOPUS_RESULT_OK) { lagopus_perror(res); TEST_FAIL_MESSAGE("handler_test_utils.c: handler shutdown error"); } res = lagopus_thread_wait((lagopus_thread_t *) th, SHUTDOWN_TIMEOUT); TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, res, "wait error"); return res; }
lagopus_result_t dp_thread_shutdown(lagopus_thread_t *threadptr, lagopus_mutex_t *lockptr, bool *runptr, shutdown_grace_level_t level) { bool is_valid = false; lagopus_chrono_t nsec; lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; lagopus_msg_info("shutdown called\n"); lagopus_mutex_lock(lockptr); if (*runptr == false) { goto done; } ret = lagopus_thread_is_valid(threadptr, &is_valid); if (ret != LAGOPUS_RESULT_OK || is_valid == false) { lagopus_perror(ret); goto done; } /* XXX UP TO main() */ switch (level) { case SHUTDOWN_RIGHT_NOW: nsec = TIMEOUT_SHUTDOWN_RIGHT_NOW; break; case SHUTDOWN_GRACEFULLY: nsec = TIMEOUT_SHUTDOWN_GRACEFULLY; break; default: lagopus_msg_fatal("unknown shutdown level %d\n", level); ret = LAGOPUS_RESULT_ANY_FAILURES; goto done; } *runptr = false; ret = lagopus_thread_wait(threadptr, nsec); if (ret != LAGOPUS_RESULT_OK) { lagopus_perror(ret); goto done; } done: lagopus_mutex_unlock(lockptr); return ret; }
int main(int argc, char const *const argv[]) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; int cpu = 0; lagopus_thread_t thd = NULL; if (argc > 1) { int tmp; if (lagopus_str_parse_int32(argv[1], &tmp) == LAGOPUS_RESULT_OK && tmp > 0) { cpu = tmp; } } if ((ret = lagopus_thread_create(&thd, s_main_proc, NULL, NULL, (const char *) "thread", (void *) NULL)) == LAGOPUS_RESULT_OK) { if ((ret = lagopus_thread_set_cpu_affinity(&thd, -1)) == LAGOPUS_RESULT_OK && (ret = lagopus_thread_set_cpu_affinity(&thd, cpu)) == LAGOPUS_RESULT_OK) { if ((ret = lagopus_thread_start(&thd, false)) == LAGOPUS_RESULT_OK) { ret = lagopus_thread_wait(&thd, -1LL); } } } if (ret != LAGOPUS_RESULT_OK) { lagopus_perror(ret); return 1; } else { return 0; } }
int main(int argc, char const *const argv[]) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; int i; int n = 10000; lagopus_thread_t thread = NULL; pthread_t tid = LAGOPUS_INVALID_THREAD; lagopus_log_set_debug_level(10); if (argc > 1) { int tmp; if (lagopus_str_parse_int32(argv[1], &tmp) == LAGOPUS_RESULT_OK && tmp > 0) { n = tmp; } } /* create, start, wait, destroy */ for (i = 0; i < n; i++) { thread = NULL; if ((ret = lagopus_thread_create( (lagopus_thread_t *)&thread, (lagopus_thread_main_proc_t)s_main_proc, (lagopus_thread_finalize_proc_t)s_finalize_proc, (lagopus_thread_freeup_proc_t)s_freeup_proc, (const char *) "thread", (void *) NULL)) != LAGOPUS_RESULT_OK) { lagopus_perror(ret); lagopus_exit_fatal("Can't create a thread.\n"); } else { fprintf(stderr, "create a thread [%d]\n", i); if ((ret = lagopus_thread_get_pthread_id(&thread, &tid)) != LAGOPUS_RESULT_NOT_STARTED) { lagopus_perror(ret); goto done; } } if ((ret = lagopus_thread_start(&thread, false)) != LAGOPUS_RESULT_OK) { lagopus_perror(ret); lagopus_exit_fatal("Can't start the thread.\n"); } else { ret = lagopus_thread_get_pthread_id(&thread, &tid); if (ret == LAGOPUS_RESULT_OK || ret == LAGOPUS_RESULT_ALREADY_HALTED) { fprintf(stderr, " start thread: %lu\n", tid); } else { lagopus_perror(ret); goto done; } } s_check_cancelled(&thread, false); if ((ret = lagopus_thread_wait(&thread, 1000LL * 1000LL * 1000LL)) != LAGOPUS_RESULT_OK) { lagopus_perror(ret); lagopus_msg_error("Can't wait the thread.\n"); } else { if ((ret = lagopus_thread_get_pthread_id(&thread, &tid)) == LAGOPUS_RESULT_OK) { lagopus_msg_error( "Thread has been completed, but I can get thread ID, %lu\n", tid); } else if (ret != LAGOPUS_RESULT_ALREADY_HALTED) { lagopus_perror(ret); goto done; } } s_check_cancelled(&thread, false); lagopus_thread_destroy(&thread); fprintf(stderr, "destroy the thread [%d]\n", i); fprintf(stderr, "\n"); } /* create, start, cancel, destroy */ for (i = 0; i < n; i++) { thread = NULL; if ((ret = lagopus_thread_create( (lagopus_thread_t *)&thread, (lagopus_thread_main_proc_t)s_main_proc_with_sleep, (lagopus_thread_finalize_proc_t)s_finalize_proc, (lagopus_thread_freeup_proc_t)s_freeup_proc, (const char *) "thread", (void *) NULL)) != LAGOPUS_RESULT_OK) { lagopus_perror(ret); lagopus_exit_fatal("Can't create a thread.\n"); } else { fprintf(stderr, "create a thread [%d]\n", i); if ((ret = lagopus_thread_get_pthread_id(&thread, &tid)) != LAGOPUS_RESULT_NOT_STARTED) { lagopus_perror(ret); goto done; } } if ((ret = lagopus_thread_start(&thread, false)) != LAGOPUS_RESULT_OK) { lagopus_perror(ret); lagopus_exit_fatal("Can't start the thread.\n"); } else { ret = lagopus_thread_get_pthread_id(&thread, &tid); if (ret == LAGOPUS_RESULT_OK || ret == LAGOPUS_RESULT_ALREADY_HALTED) { fprintf(stderr, " start thread: %lu\n", tid); } else { lagopus_perror(ret); goto done; } } s_check_cancelled(&thread, false); if ((ret = lagopus_thread_cancel(&thread)) != LAGOPUS_RESULT_OK) { lagopus_perror(ret); lagopus_msg_error("Can't cancel the thread.\n"); } else { if ((ret = lagopus_thread_get_pthread_id(&thread, &tid)) == LAGOPUS_RESULT_OK) { lagopus_msg_error( "Thread has been canceled, but I can get thread ID, %lu\n", tid); } else if (ret != LAGOPUS_RESULT_NOT_STARTED && ret != LAGOPUS_RESULT_ALREADY_HALTED) { lagopus_perror(ret); goto done; } } s_check_cancelled(&thread, true); lagopus_thread_destroy(&thread); fprintf(stderr, "destroy the thread [%d]\n", i); fprintf(stderr, "\n"); } /* create, start, destroy */ for (i = 0; i < n; i++) { thread = NULL; if ((ret = lagopus_thread_create( (lagopus_thread_t *)&thread, (lagopus_thread_main_proc_t)s_main_proc_with_sleep, (lagopus_thread_finalize_proc_t)s_finalize_proc, (lagopus_thread_freeup_proc_t)s_freeup_proc, (const char *) "thread", (void *) NULL)) != LAGOPUS_RESULT_OK) { lagopus_perror(ret); lagopus_exit_fatal("Can't create a thread.\n"); } else { fprintf(stderr, "create a thread [%d]\n", i); if ((ret = lagopus_thread_get_pthread_id(&thread, &tid)) != LAGOPUS_RESULT_NOT_STARTED) { lagopus_perror(ret); goto done; } } if ((ret = lagopus_thread_start(&thread, false)) != LAGOPUS_RESULT_OK) { lagopus_perror(ret); lagopus_exit_fatal("Can't start the thread.\n"); } else { ret = lagopus_thread_get_pthread_id(&thread, &tid); if (ret == LAGOPUS_RESULT_OK || ret == LAGOPUS_RESULT_ALREADY_HALTED) { fprintf(stderr, " start thread: %lu\n", tid); } else { lagopus_perror(ret); goto done; } } lagopus_thread_destroy(&thread); fprintf(stderr, "destroy the thread [%d]\n", i); fprintf(stderr, "\n"); } done: return (ret == LAGOPUS_RESULT_OK) ? 0 : 1; }
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; }