thread_tc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-09.01 yangjie the firet version
  9. * 2021-10.11 mazhiyuan add idle, yield, suspend, control, priority, delay_until
  10. */
  11. #include <rtthread.h>
  12. #include <stdlib.h>
  13. #include "utest.h"
  14. #define THREAD_STACK_SIZE 512
  15. #define THREAD_TIMESLICE 10
  16. ALIGN(RT_ALIGN_SIZE)
  17. static char thread2_stack[1024];
  18. static struct rt_thread thread2;
  19. #ifdef RT_USING_HEAP
  20. static rt_thread_t tid1 = RT_NULL;
  21. static rt_thread_t tid3 = RT_NULL;
  22. static rt_thread_t tid4 = RT_NULL;
  23. static rt_thread_t tid5 = RT_NULL;
  24. static rt_thread_t tid6 = RT_NULL;
  25. static rt_thread_t tid7 = RT_NULL;
  26. #endif /* RT_USING_HEAP */
  27. static rt_uint32_t tid3_delay_pass_flag = 0;
  28. static rt_uint32_t tid3_finish_flag = 0;
  29. static rt_uint32_t tid4_finish_flag = 0;
  30. static rt_uint32_t tid6_finish_flag = 0;
  31. static rt_uint32_t thread5_source = 0;
  32. #ifndef RT_USING_SMP
  33. static rt_uint32_t thread_yield_flag = 0;
  34. #endif
  35. static rt_uint32_t entry_idle_hook_times = 0;
  36. static rt_thread_t __current_thread;
  37. static rt_uint8_t change_priority;
  38. static rt_uint32_t count = 0;
  39. void thread1_entry(void *param)
  40. {
  41. while (1);
  42. }
  43. static void test_dynamic_thread(void)
  44. {
  45. rt_err_t ret_startup = -RT_ERROR;
  46. rt_err_t ret_delete = -RT_ERROR;
  47. tid1 = rt_thread_create("thread1",
  48. thread1_entry,
  49. (void *)1,
  50. THREAD_STACK_SIZE,
  51. __current_thread->current_priority + 1,
  52. THREAD_TIMESLICE - 5);
  53. if (tid1 == RT_NULL)
  54. {
  55. uassert_false(tid1 == RT_NULL);
  56. goto __exit;
  57. }
  58. ret_startup = rt_thread_startup(tid1);
  59. if (ret_startup != RT_EOK)
  60. {
  61. uassert_false(ret_startup != RT_EOK);
  62. goto __exit;
  63. }
  64. ret_delete = rt_thread_delete(tid1);
  65. if (ret_delete != RT_EOK)
  66. {
  67. uassert_false(ret_delete != RT_EOK);
  68. goto __exit;
  69. }
  70. uassert_true(tid1 != RT_NULL && ret_startup == RT_EOK && ret_delete == RT_EOK);
  71. __exit:
  72. if (tid1 != RT_NULL && ret_delete != RT_EOK)
  73. {
  74. rt_thread_delete(tid1);
  75. }
  76. return;
  77. }
  78. void thread2_entry(void *param)
  79. {
  80. while (1);
  81. }
  82. static void test_static_thread(void)
  83. {
  84. rt_err_t ret_init = -RT_ERROR;
  85. rt_err_t ret_startup = -RT_ERROR;
  86. rt_err_t ret_detach = - RT_ERROR;
  87. ret_init = rt_thread_init(&thread2,
  88. "thread2",
  89. thread2_entry,
  90. (void *)2,
  91. &thread2_stack[0],
  92. sizeof(thread2_stack),
  93. __current_thread->current_priority + 1,
  94. THREAD_TIMESLICE);
  95. if (ret_init != RT_EOK)
  96. {
  97. uassert_false(ret_init != RT_EOK);
  98. goto __exit;
  99. }
  100. ret_startup = rt_thread_startup(&thread2);
  101. if (ret_startup != RT_EOK)
  102. {
  103. uassert_false(ret_startup != RT_EOK);
  104. goto __exit;
  105. }
  106. ret_detach = rt_thread_detach(&thread2);
  107. if (ret_detach != RT_EOK)
  108. {
  109. uassert_false(ret_detach != RT_EOK);
  110. goto __exit;
  111. }
  112. uassert_true(ret_init == RT_EOK && ret_startup == RT_EOK && ret_detach == RT_EOK);
  113. __exit:
  114. if (ret_init == RT_EOK && ret_detach != RT_EOK)
  115. {
  116. rt_thread_detach(&thread2);
  117. }
  118. return;
  119. }
  120. static void thread3_entry(void *parameter)
  121. {
  122. rt_tick_t tick;
  123. tick = rt_tick_get();
  124. rt_thread_delay(15);
  125. if (rt_tick_get() - tick > 16)
  126. {
  127. tid3_finish_flag = 1;
  128. tid3_delay_pass_flag = 0;
  129. return;
  130. }
  131. tid3_delay_pass_flag = 1;
  132. tid3_finish_flag = 1;
  133. }
  134. static void test_thread_delay(void)
  135. {
  136. rt_err_t ret_startup = -RT_ERROR;
  137. tid3 = rt_thread_create("thread3",
  138. thread3_entry,
  139. RT_NULL,
  140. THREAD_STACK_SIZE,
  141. __current_thread->current_priority - 1,
  142. THREAD_TIMESLICE);
  143. if (tid3 == RT_NULL)
  144. {
  145. LOG_E("rt_thread_create failed!");
  146. uassert_false(tid3 == RT_NULL);
  147. goto __exit;
  148. }
  149. ret_startup = rt_thread_startup(tid3);
  150. if (ret_startup != RT_EOK)
  151. {
  152. LOG_E("rt_thread_startup failed!");
  153. uassert_false(1);
  154. goto __exit;
  155. }
  156. while (tid3_finish_flag != 1);
  157. uassert_true(tid3_delay_pass_flag == 1);
  158. __exit:
  159. return;
  160. }
  161. static void idle_hook(void)
  162. {
  163. entry_idle_hook_times ++;
  164. }
  165. static void thread4_entry(void *parameter)
  166. {
  167. rt_uint32_t delay_times = 5;
  168. while (delay_times --)
  169. {
  170. rt_thread_mdelay(300);
  171. }
  172. rt_thread_idle_delhook(idle_hook);
  173. tid4_finish_flag = 1;
  174. }
  175. static void test_idle_hook(void)
  176. {
  177. rt_err_t ret_startup = -RT_ERROR;
  178. rt_thread_idle_sethook(idle_hook);
  179. tid4 = rt_thread_create("thread4",
  180. thread4_entry,
  181. RT_NULL,
  182. THREAD_STACK_SIZE,
  183. __current_thread->current_priority - 1,
  184. THREAD_TIMESLICE);
  185. if (tid4 == RT_NULL)
  186. {
  187. LOG_E("rt_thread_create failed!");
  188. uassert_false(tid4 == RT_NULL);
  189. goto __exit;
  190. }
  191. ret_startup = rt_thread_startup(tid4);
  192. if (ret_startup != RT_EOK)
  193. {
  194. LOG_E("rt_thread_startup failed!");
  195. uassert_false(1);
  196. goto __exit;
  197. }
  198. while (tid4_finish_flag != 1)
  199. {
  200. rt_thread_mdelay(200);
  201. }
  202. uassert_true(entry_idle_hook_times > 0);
  203. __exit:
  204. return;
  205. }
  206. static void thread5_entry(void *parameter)
  207. {
  208. while (1)
  209. {
  210. thread5_source ++;
  211. rt_thread_delay(5);
  212. if (thread5_source == 5)
  213. {
  214. rt_thread_yield();
  215. }
  216. }
  217. }
  218. static void thread6_entry(void *parameter)
  219. {
  220. while (++ thread5_source <= 9);
  221. tid6_finish_flag = 1;
  222. }
  223. static void test_thread_yield(void)
  224. {
  225. rt_err_t ret_startup = -RT_ERROR;
  226. tid5 = rt_thread_create("thread5",
  227. thread5_entry,
  228. RT_NULL,
  229. THREAD_STACK_SIZE,
  230. __current_thread->current_priority - 1,
  231. THREAD_TIMESLICE);
  232. if (tid5 == RT_NULL)
  233. {
  234. LOG_E("rt_thread_create failed!");
  235. uassert_false(tid5 == RT_NULL);
  236. goto __exit;
  237. }
  238. ret_startup = rt_thread_startup(tid5);
  239. if (ret_startup != RT_EOK)
  240. {
  241. LOG_E("rt_thread_startup failed!");
  242. uassert_false(1);
  243. goto __exit;
  244. }
  245. tid6 = rt_thread_create("thread6",
  246. thread6_entry,
  247. RT_NULL,
  248. THREAD_STACK_SIZE,
  249. __current_thread->current_priority - 1,
  250. THREAD_TIMESLICE);
  251. if (tid6 == RT_NULL)
  252. {
  253. LOG_E("rt_thread_create failed!");
  254. uassert_false(tid6 == RT_NULL);
  255. goto __exit;
  256. }
  257. ret_startup = rt_thread_startup(tid6);
  258. if (ret_startup != RT_EOK)
  259. {
  260. LOG_E("rt_thread_startup failed!");
  261. uassert_false(1);
  262. goto __exit;
  263. }
  264. while (tid6_finish_flag != 1);
  265. uassert_true(thread5_source == 10);
  266. __exit:
  267. if (tid5 != RT_NULL)
  268. {
  269. rt_thread_delete(tid5);
  270. }
  271. return;
  272. }
  273. static void thread7_entry(void *parameter)
  274. {
  275. while (1);
  276. }
  277. static void test_thread_control(void)
  278. {
  279. rt_err_t ret_control = -RT_ERROR;
  280. rt_err_t rst_delete = -RT_ERROR;
  281. tid7 = rt_thread_create("thread7",
  282. thread7_entry,
  283. RT_NULL,
  284. THREAD_STACK_SIZE,
  285. __current_thread->current_priority + 1,
  286. THREAD_TIMESLICE);
  287. if (tid7 == RT_NULL)
  288. {
  289. LOG_E("rt_thread_create failed!");
  290. uassert_false(tid7 == RT_NULL);
  291. goto __exit;
  292. }
  293. ret_control = rt_thread_control(tid7, RT_THREAD_CTRL_STARTUP, RT_NULL);
  294. if (ret_control != RT_EOK)
  295. {
  296. LOG_E("rt_thread_control failed!");
  297. uassert_false(1);
  298. goto __exit;
  299. }
  300. rt_thread_mdelay(200);
  301. rt_thread_control(tid7, RT_THREAD_CTRL_CHANGE_PRIORITY, &change_priority);
  302. if (tid7->current_priority != change_priority)
  303. {
  304. LOG_E("rt_thread_control failed!");
  305. uassert_false(1);
  306. goto __exit;
  307. }
  308. rst_delete = rt_thread_control(tid7, RT_THREAD_CTRL_CLOSE, RT_NULL);
  309. if (rst_delete != RT_EOK)
  310. {
  311. LOG_E("rt_thread_control failed!");
  312. uassert_false(rst_delete != RT_EOK);
  313. goto __exit;
  314. }
  315. uassert_true(1);
  316. __exit:
  317. if (tid7 != RT_NULL && rst_delete != RT_EOK)
  318. {
  319. rt_thread_delete(tid7);
  320. }
  321. return;
  322. }
  323. static void thread8_entry(void *parameter)
  324. {
  325. for (; count < 10; count ++);
  326. }
  327. static void test_thread_priority(void)
  328. {
  329. rt_err_t ret_startup = -RT_ERROR;
  330. rt_thread_t tid8 = RT_NULL;
  331. tid8 = rt_thread_create("thread8",
  332. thread8_entry,
  333. RT_NULL,
  334. THREAD_STACK_SIZE,
  335. __current_thread->current_priority - 1,
  336. THREAD_TIMESLICE);
  337. if (tid8 == RT_NULL)
  338. {
  339. LOG_E("rt_thread_create failed!");
  340. uassert_false(tid8 == RT_NULL);
  341. return;
  342. }
  343. count = 0;
  344. ret_startup = rt_thread_startup(tid8);
  345. if (ret_startup != RT_EOK)
  346. {
  347. uassert_false(ret_startup != RT_EOK);
  348. return ;
  349. }
  350. uassert_true(count == 10);
  351. return;
  352. }
  353. static void test_delay_until(void)
  354. {
  355. rt_tick_t tick;
  356. rt_tick_t check_tick = 0;
  357. rt_tick_t delta = 0;
  358. tick = rt_tick_get();
  359. check_tick = tick;
  360. rt_thread_delay_until(&tick, 100);
  361. delta = rt_tick_get() - check_tick;
  362. rt_kprintf("delta[100] -> %d\n", delta);
  363. uassert_int_equal(delta, 100);
  364. check_tick = tick;
  365. rt_thread_delay(2);
  366. rt_thread_delay_until(&tick, 200);
  367. delta = rt_tick_get() - check_tick;
  368. rt_kprintf("delta[200] -> %d\n", delta);
  369. uassert_int_equal(delta, 200);
  370. check_tick = tick;
  371. rt_thread_delay(2);
  372. rt_thread_delay_until(&tick, 300);
  373. delta = rt_tick_get() - check_tick;
  374. rt_kprintf("delta[300] -> %d\n", delta);
  375. uassert_int_equal(delta, 300);
  376. check_tick = tick;
  377. rt_thread_delay(2);
  378. rt_thread_delay_until(&tick, 100);
  379. delta = rt_tick_get() - check_tick;
  380. uassert_int_equal(delta, 100);
  381. check_tick = tick;
  382. rt_thread_delay(2);
  383. rt_thread_delay_until(&tick, 50);
  384. delta = rt_tick_get() - check_tick;
  385. rt_kprintf("delta[50] -> %d\n", delta);
  386. uassert_int_equal(delta, 50);
  387. check_tick = tick;
  388. rt_thread_delay(2);
  389. rt_thread_delay_until(&tick, 20);
  390. delta = rt_tick_get() - check_tick;
  391. rt_kprintf("delta[20] -> %d\n", delta);
  392. uassert_int_equal(delta, 20);
  393. check_tick = tick;
  394. rt_thread_delay(2);
  395. rt_thread_delay_until(&tick, 10);
  396. delta = rt_tick_get() - check_tick;
  397. rt_kprintf("delta[10] -> %d\n", delta);
  398. uassert_int_equal(delta, 10);
  399. }
  400. #ifndef RT_USING_SMP
  401. static volatile rt_uint32_t yield_count;
  402. static void test_thread_yield_inc_entry(void *parameter)
  403. {
  404. rt_uint32_t loop = 0;
  405. while (1)
  406. {
  407. if (loop++ > 10001)
  408. break;
  409. yield_count++;
  410. rt_thread_yield();
  411. }
  412. }
  413. static void test_thread_yield_entry(void *parameter)
  414. {
  415. rt_err_t ret_startup = -RT_ERROR;
  416. rt_thread_t tid;
  417. rt_uint32_t loop = 0;
  418. rt_uint32_t count_before;
  419. tid = rt_thread_create("inc", test_thread_yield_inc_entry, RT_NULL,
  420. 2048, 1, 10);
  421. if (!tid)
  422. {
  423. LOG_E("rt_thread_create failed!");
  424. return;
  425. }
  426. ret_startup = rt_thread_startup(tid);
  427. if (ret_startup != RT_EOK)
  428. {
  429. LOG_E("rt_thread_startup failed!");
  430. uassert_false(1);
  431. return ;
  432. }
  433. while (1)
  434. {
  435. if (loop++ > 10000)
  436. break;
  437. count_before = yield_count;
  438. rt_thread_yield();
  439. if (yield_count == count_before)
  440. {
  441. LOG_E("yield error!");
  442. return;
  443. }
  444. }
  445. thread_yield_flag = 1;
  446. }
  447. void test_thread_yield_nosmp(void)
  448. {
  449. rt_err_t ret_startup = -RT_ERROR;
  450. rt_thread_t tid;
  451. yield_count = 0;
  452. tid = rt_thread_create("chkcnt", test_thread_yield_entry, RT_NULL,
  453. 2048, 1, 10);
  454. if (!tid)
  455. {
  456. LOG_E("rt_thread_create failed!");
  457. return;
  458. }
  459. ret_startup = rt_thread_startup(tid);
  460. if (ret_startup != RT_EOK)
  461. {
  462. LOG_E("rt_thread_startup failed!");
  463. uassert_false(1);
  464. return ;
  465. }
  466. uassert_true(thread_yield_flag == 1);
  467. }
  468. static rt_uint32_t thread9_count = 0;
  469. static void thread9_entry(void *parameter)
  470. {
  471. while (1)
  472. {
  473. thread9_count ++;
  474. }
  475. }
  476. static void test_thread_suspend(void)
  477. {
  478. static rt_thread_t tid;
  479. rt_err_t ret_startup = -RT_ERROR;
  480. uint32_t count_before_suspend, count_before_resume, count_after_resume;
  481. tid = rt_thread_create("thread9",
  482. thread9_entry,
  483. RT_NULL,
  484. THREAD_STACK_SIZE,
  485. __current_thread->current_priority + 1,
  486. THREAD_TIMESLICE);
  487. if (tid == RT_NULL)
  488. {
  489. LOG_E("rt_thread_create failed!");
  490. uassert_false(tid4 == RT_NULL);
  491. goto __exit;
  492. }
  493. ret_startup = rt_thread_startup(tid);
  494. if (ret_startup != RT_EOK)
  495. {
  496. LOG_E("rt_thread_startup failed!");
  497. uassert_false(1);
  498. goto __exit;
  499. }
  500. rt_thread_delay(5);
  501. rt_thread_suspend(tid);
  502. count_before_suspend = thread9_count;
  503. uassert_true(count_before_suspend != 0);
  504. rt_thread_delay(5);
  505. count_before_resume = thread9_count;
  506. uassert_true(count_before_suspend == count_before_resume);
  507. rt_thread_resume(tid);
  508. rt_thread_delay(5);
  509. count_after_resume = thread9_count;
  510. uassert_true(count_after_resume != count_before_resume);
  511. __exit:
  512. if (tid != RT_NULL)
  513. {
  514. rt_thread_delete(tid);
  515. }
  516. return;
  517. }
  518. #endif
  519. static rt_err_t utest_tc_init(void)
  520. {
  521. __current_thread = rt_thread_self();
  522. change_priority = __current_thread->current_priority + 5;
  523. tid3_delay_pass_flag = 0;
  524. tid3_finish_flag = 0;
  525. tid4_finish_flag = 0;
  526. tid6_finish_flag = 0;
  527. entry_idle_hook_times = 0;
  528. count = 0;
  529. return RT_EOK;
  530. }
  531. static rt_err_t utest_tc_cleanup(void)
  532. {
  533. return RT_EOK;
  534. }
  535. static void testcase(void)
  536. {
  537. /* init, detach */
  538. UTEST_UNIT_RUN(test_static_thread);
  539. /* create, delete */
  540. UTEST_UNIT_RUN(test_dynamic_thread);
  541. /* delay */
  542. UTEST_UNIT_RUN(test_thread_delay);
  543. /* idle_sethook, idle_delhook */
  544. UTEST_UNIT_RUN(test_idle_hook);
  545. /* yield */
  546. UTEST_UNIT_RUN(test_thread_yield);
  547. #ifndef RT_USING_SMP
  548. /* yield_nosmp */
  549. UTEST_UNIT_RUN(test_thread_yield_nosmp);
  550. /* suspend, resume */
  551. UTEST_UNIT_RUN(test_thread_suspend);
  552. #endif
  553. /* control */
  554. UTEST_UNIT_RUN(test_thread_control);
  555. UTEST_UNIT_RUN(test_thread_priority);
  556. /* delay_until */
  557. UTEST_UNIT_RUN(test_delay_until);
  558. }
  559. UTEST_TC_EXPORT(testcase, "testcases.kernel.thread_tc", utest_tc_init, utest_tc_cleanup, 1000);
  560. /********************* end of file ************************/