- #include <stdio.h>
- #include <errno.h>
-
- #include <kc.h>
- #include <kc_ut.h>
- #include <kc_assert.h>
- #include <kc_memory.h>
- #include <kc_threads.h>
-
- #include "ut.h"
-
- // プロトタイプ宣言
- static void test_thread_new(void);
- // static void test_thread_sleep(void);
- static void test_thread_memory_error(void);
-
- /**
- * KcThread 単体テストスイート
- */
- void suite_thread(void)
- {
- KcUt *ut = KcUt_get_instance();
- ut->add(ut, UT_TESTCASE, "thread new/delete", test_thread_new);
- // ut->add(ut, UT_TESTCASE, "thread sleep", test_thread_sleep);
- ut->add(ut, UT_TESTCASE, "thread memory error", test_thread_memory_error);
- }
-
- static int pass_value = 0;
- static int test_func(void *args)
- {
- int *value = (int *)args;
- if (*value == 1)
- {
- pass_value &= 0x01;
- }
- if (*value == 2)
- {
- pass_value &= 0x02;
- }
- for (int i = 0; i < 10; i++)
- {
- printf("Thread %d : %03d\n", *value, i);
- KcThread_msleep(1, true);
- }
- return 0;
- }
-
- /**
- * Thread 生成/破棄。
- *
- * @process KcThread_new を実行する。。
- * @result KcThread が生成されること。
- *
- * @process KcThread_delete にて Thread を破棄する。
- * @result Thread が破棄されること。
- */
- static void test_thread_new(void)
- {
- KcThread *thread_1 = KcThread_new(test_func);
- KcThread *thread_2 = KcThread_new(test_func);
-
- pass_value = 0;
- bool is_alive_1 = thread_1->is_alive(thread_1);
- bool is_alive_2 = thread_2->is_alive(thread_2);
- assert_false(is_alive_1);
- assert_false(is_alive_2);
- int val_1 = 1;
- int val_2 = 2;
- thread_1->start(thread_1, &val_1);
- thread_2->start(thread_2, &val_2);
- KcThread_msleep(10, false);
- is_alive_1 = thread_1->is_alive(thread_1);
- is_alive_2 = thread_2->is_alive(thread_2);
- assert_true(is_alive_1);
- assert_true(is_alive_2);
- thread_1->join(thread_1);
- thread_2->join(thread_2);
- KcThread_delete(thread_1);
- KcThread_delete(thread_2);
- }
-
- // for シグナル処理
- #if 0
- static volatile int test_thread_interrupted = 0;
- static void test_thread_sig_handler(int sig)
- {
- test_thread_interrupted = 1;
- }
- int test_sleep_func(void *args)
- {
- signal(SIGUSR1, test_thread_sig_handler);
- int *value = (int *)args;
- if (*value == 1)
- {
- KcThread_msleep(200, true);
- }
- else
- {
- KcThread_msleep(200, false);
- }
- return 0;
- }
-
- /**
- * Thread sleep 中のシグナル発生。
- */
- static void test_thread_sleep(void)
- {
-
- KcThread *thread = KcThread_new(test_sleep_func);
- int value = 1;
- test_thread_interrupted = 0;
- thread->start(thread, &value);
- KcThread_msleep(10, false);
-
- thread->join(thread);
- KcThread_delete(thread);
- }
- #endif
-
- /**
- * メモリ確保エラー
- */
- static void test_thread_memory_error(void)
- {
- ut_alloc_control(0)
- {
- KcThread *thread = KcThread_new(test_func);
- assert_null(thread);
- }
- }