lwp_pid.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. * 2019-10-16 zhangjun first version
  9. * 2021-02-20 lizhirui fix warning
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <dfs_posix.h>
  14. #include "lwp.h"
  15. #include "lwp_pid.h"
  16. #include "lwp_console.h"
  17. #ifdef RT_USING_USERSPACE
  18. #include "lwp_user_mm.h"
  19. #ifdef RT_USING_GDBSERVER
  20. #include <hw_breakpoint.h>
  21. #include <lwp_gdbserver.h>
  22. #endif
  23. #endif
  24. #define DBG_TAG "LWP_PID"
  25. #define DBG_LVL DBG_INFO
  26. #include <rtdbg.h>
  27. #define PID_CT_ASSERT(name, x) \
  28. struct assert_##name {char ary[2 * (x) - 1];}
  29. PID_CT_ASSERT(pid_max_nr, RT_LWP_MAX_NR > 1);
  30. struct rt_pid_struct
  31. {
  32. struct rt_lwp* pidmap[RT_LWP_MAX_NR];
  33. pid_t last_pid;
  34. };
  35. static struct rt_pid_struct pid_struct = {{0}, 1};
  36. int libc_stdio_get_console(void);
  37. static void __exit_files(struct rt_lwp *lwp)
  38. {
  39. int fd = lwp->fdt.maxfd - 1;
  40. while (fd >= 0)
  41. {
  42. struct dfs_fd *d;
  43. d = lwp->fdt.fds[fd];
  44. if (d)
  45. {
  46. dfs_file_close(d);
  47. fdt_fd_release(&lwp->fdt, fd);
  48. }
  49. fd--;
  50. }
  51. }
  52. struct rt_lwp* lwp_new(void)
  53. {
  54. uint32_t i;
  55. rt_base_t level;
  56. struct rt_lwp* lwp = RT_NULL;
  57. level = rt_hw_interrupt_disable();
  58. /* first scan */
  59. for (i = pid_struct.last_pid; i < RT_LWP_MAX_NR; i++)
  60. {
  61. if (!pid_struct.pidmap[i])
  62. {
  63. break;
  64. }
  65. }
  66. /* if first scan failed, scan the pidmap start with 0 */
  67. if (i >= RT_LWP_MAX_NR)
  68. {
  69. /* 0 is reserved */
  70. for (i = 1; i < pid_struct.last_pid; i++)
  71. {
  72. if (!pid_struct.pidmap[i])
  73. {
  74. break;
  75. }
  76. }
  77. }
  78. if (i >= RT_LWP_MAX_NR)
  79. {
  80. /* if second scan also failed */
  81. LOG_W("pidmap fulled\n");
  82. pid_struct.last_pid = 0;
  83. goto out;
  84. }
  85. pid_struct.last_pid = (i + 1) % RT_LWP_MAX_NR;
  86. if (pid_struct.last_pid == 0)
  87. {
  88. /* 0 is reserved */
  89. pid_struct.last_pid++;
  90. }
  91. lwp = (struct rt_lwp *)rt_malloc(sizeof(struct rt_lwp));
  92. if (lwp == RT_NULL)
  93. {
  94. LOG_E("no memory for lwp struct!\n");
  95. goto out;
  96. }
  97. rt_memset(lwp, 0, sizeof(*lwp));
  98. rt_list_init(&lwp->wait_list);
  99. lwp->pid = i;
  100. pid_struct.pidmap[i] = lwp;
  101. rt_list_init(&lwp->t_grp);
  102. rt_list_init(&lwp->object_list);
  103. lwp->address_search_head = RT_NULL;
  104. rt_wqueue_init(&lwp->wait_queue);
  105. lwp->ref = 1;
  106. out:
  107. rt_hw_interrupt_enable(level);
  108. return lwp;
  109. }
  110. static void lwp_user_obj_free(struct rt_lwp *lwp)
  111. {
  112. rt_base_t level = 0;
  113. struct rt_list_node *list = RT_NULL, *node = RT_NULL;
  114. struct rt_object *object = RT_NULL;
  115. list = &(lwp->object_list);
  116. level = rt_hw_interrupt_disable();
  117. while ((node = list->next) != list)
  118. {
  119. object = rt_list_entry(node, struct rt_object, lwp_obj_list);
  120. /* remove from kernel object list */
  121. switch (object->type)
  122. {
  123. case RT_Object_Class_Thread:
  124. {
  125. RT_ASSERT(0);
  126. break;
  127. }
  128. case RT_Object_Class_Semaphore:
  129. rt_sem_delete((rt_sem_t)object);
  130. break;
  131. case RT_Object_Class_Mutex:
  132. rt_mutex_delete((rt_mutex_t)object);
  133. break;
  134. case RT_Object_Class_Event:
  135. rt_event_delete((rt_event_t)object);
  136. break;
  137. case RT_Object_Class_MailBox:
  138. rt_mb_delete((rt_mailbox_t)object);
  139. break;
  140. case RT_Object_Class_MessageQueue:
  141. rt_mq_delete((rt_mq_t)object);
  142. break;
  143. case RT_Object_Class_Device:
  144. rt_device_close((rt_device_t)object);
  145. break;
  146. case RT_Object_Class_Timer:
  147. rt_timer_delete((rt_timer_t)object);
  148. break;
  149. case RT_Object_Class_Channel:
  150. /* remove from object list */
  151. rt_list_remove(&object->list);
  152. break;
  153. case RT_Object_Class_Custom:
  154. rt_custom_object_destroy(object);
  155. break;
  156. default:
  157. LOG_E("input object type(%d) error", object->type);
  158. /* remove from object list */
  159. rt_list_remove(&object->list);
  160. break;
  161. }
  162. }
  163. rt_hw_interrupt_enable(level);
  164. }
  165. void lwp_free(struct rt_lwp* lwp)
  166. {
  167. rt_base_t level;
  168. if (lwp == NULL) return ;
  169. LOG_D("lwp free: %p\n", lwp);
  170. level = rt_hw_interrupt_disable();
  171. lwp->finish = 1;
  172. if (lwp->args != RT_NULL)
  173. {
  174. #ifndef RT_USING_USERSPACE
  175. rt_free(lwp->args);
  176. #endif
  177. lwp->args = RT_NULL;
  178. }
  179. if (lwp->fdt.fds != RT_NULL)
  180. {
  181. /* auto clean fds */
  182. __exit_files(lwp);
  183. lwp_user_obj_free(lwp);
  184. rt_free(lwp->fdt.fds);
  185. lwp->fdt.fds = RT_NULL;
  186. }
  187. /* free data section */
  188. if (lwp->data_entry != RT_NULL)
  189. {
  190. rt_free_align(lwp->data_entry);
  191. lwp->data_entry = RT_NULL;
  192. }
  193. /* free text section */
  194. if (lwp->lwp_type == LWP_TYPE_DYN_ADDR)
  195. {
  196. if (lwp->text_entry)
  197. {
  198. LOG_D("lwp text free: %p", lwp->text_entry);
  199. #ifndef RT_USING_USERSPACE
  200. #ifdef RT_USING_CACHE
  201. rt_free_align(lwp->text_entry);
  202. #else
  203. rt_free(lwp->text_entry);
  204. #endif
  205. #endif
  206. lwp->text_entry = RT_NULL;
  207. }
  208. }
  209. #ifdef RT_USING_USERSPACE
  210. lwp_unmap_user_space(lwp);
  211. #endif
  212. /* for children */
  213. while (lwp->first_child)
  214. {
  215. struct rt_lwp *child;
  216. child = lwp->first_child;
  217. lwp->first_child = child->sibling;
  218. if (child->finish)
  219. {
  220. pid_struct.pidmap[lwp_to_pid(child)] = RT_NULL;
  221. rt_free(child);
  222. }
  223. else
  224. {
  225. child->sibling = RT_NULL;
  226. child->parent = RT_NULL;
  227. }
  228. }
  229. /* for parent */
  230. {
  231. struct rt_lwp *console_lwp;
  232. console_lwp = rt_console_get_foreground();
  233. if (lwp == console_lwp)
  234. {
  235. rt_console_set_foreground(lwp->parent);
  236. }
  237. if (lwp->parent)
  238. {
  239. struct rt_thread *thread;
  240. if (!rt_list_isempty(&lwp->wait_list))
  241. {
  242. thread = rt_list_entry(lwp->wait_list.next, struct rt_thread, tlist);
  243. thread->error = RT_EOK;
  244. thread->msg_ret = (void*)(rt_size_t)lwp->lwp_ret;
  245. rt_thread_resume(thread);
  246. }
  247. }
  248. else
  249. {
  250. pid_struct.pidmap[lwp_to_pid(lwp)] = RT_NULL;
  251. rt_free(lwp);
  252. }
  253. }
  254. rt_hw_interrupt_enable(level);
  255. }
  256. void lwp_ref_inc(struct rt_lwp *lwp)
  257. {
  258. rt_base_t level;
  259. level = rt_hw_interrupt_disable();
  260. lwp->ref++;
  261. rt_hw_interrupt_enable(level);
  262. }
  263. void lwp_ref_dec(struct rt_lwp *lwp)
  264. {
  265. rt_base_t level;
  266. int ref;
  267. level = rt_hw_interrupt_disable();
  268. if (lwp->ref)
  269. {
  270. lwp->ref--;
  271. ref = lwp->ref;
  272. if (!ref)
  273. {
  274. #ifdef RT_USING_GDBSERVER
  275. struct rt_channel_msg msg;
  276. if (lwp->debug)
  277. {
  278. memset(&msg, 0, sizeof msg);
  279. rt_raw_channel_send(gdb_get_server_channel(), &msg);
  280. }
  281. #endif
  282. lwp_free(lwp);
  283. }
  284. }
  285. rt_hw_interrupt_enable(level);
  286. }
  287. struct rt_lwp* lwp_from_pid(pid_t pid)
  288. {
  289. if ((pid <= 0) || (pid >= RT_LWP_MAX_NR))
  290. {
  291. return NULL;
  292. }
  293. return pid_struct.pidmap[pid];
  294. }
  295. pid_t lwp_to_pid(struct rt_lwp* lwp)
  296. {
  297. if (!lwp)
  298. {
  299. return -1;
  300. }
  301. return lwp->pid;
  302. }
  303. char* lwp_pid2name(int32_t pid)
  304. {
  305. struct rt_lwp* lwp;
  306. char* process_name = RT_NULL;
  307. lwp = lwp_from_pid(pid);
  308. if (lwp)
  309. {
  310. process_name = strrchr(lwp->cmd, '/');
  311. process_name = process_name? process_name + 1: lwp->cmd;
  312. }
  313. return process_name;
  314. }
  315. int32_t lwp_name2pid(const char* name)
  316. {
  317. uint32_t pid;
  318. rt_thread_t main_thread;
  319. char* process_name = RT_NULL;
  320. struct rt_lwp* lwp = RT_NULL;
  321. for (pid = 1; pid < RT_LWP_MAX_NR; pid++)
  322. {
  323. /* 0 is reserved */
  324. if (pid_struct.pidmap[pid])
  325. {
  326. lwp = pid_struct.pidmap[pid];
  327. process_name = strrchr(lwp->cmd, '/');
  328. process_name = process_name? process_name + 1: lwp->cmd;
  329. if (!rt_strncmp(name, process_name, RT_NAME_MAX))
  330. {
  331. main_thread = rt_list_entry(lwp->t_grp.prev, struct rt_thread, sibling);
  332. if (!(main_thread->stat & RT_THREAD_CLOSE))
  333. {
  334. return pid;
  335. }
  336. }
  337. }
  338. }
  339. return -1;
  340. }
  341. int lwp_getpid(void)
  342. {
  343. return ((struct rt_lwp *)rt_thread_self()->lwp)->pid;
  344. }
  345. pid_t waitpid(pid_t pid, int *status, int options)
  346. {
  347. pid_t ret = -1;
  348. rt_base_t level;
  349. struct rt_thread *thread;
  350. struct rt_lwp *lwp;
  351. struct rt_lwp *lwp_self;
  352. level = rt_hw_interrupt_disable();
  353. lwp = lwp_from_pid(pid);
  354. if (!lwp)
  355. {
  356. goto quit;
  357. }
  358. lwp_self = (struct rt_lwp *)rt_thread_self()->lwp;
  359. if (!lwp_self)
  360. {
  361. goto quit;
  362. }
  363. if (lwp->parent != lwp_self)
  364. {
  365. goto quit;
  366. }
  367. if (lwp->finish)
  368. {
  369. ret = pid;
  370. }
  371. else
  372. {
  373. if (!rt_list_isempty(&lwp->wait_list))
  374. {
  375. goto quit;
  376. }
  377. thread = rt_thread_self();
  378. rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
  379. rt_list_insert_before(&lwp->wait_list, &(thread->tlist));
  380. rt_schedule();
  381. if (thread->error == RT_EOK)
  382. {
  383. ret = pid;
  384. }
  385. }
  386. if (ret != -1)
  387. {
  388. struct rt_lwp **lwp_node;
  389. *status = lwp->lwp_ret;
  390. lwp_node = &lwp_self->first_child;
  391. while (*lwp_node != lwp)
  392. {
  393. RT_ASSERT(*lwp_node != RT_NULL);
  394. lwp_node = &(*lwp_node)->sibling;
  395. }
  396. (*lwp_node) = lwp->sibling;
  397. pid_struct.pidmap[pid] = RT_NULL;
  398. rt_free(lwp);
  399. }
  400. quit:
  401. rt_hw_interrupt_enable(level);
  402. return ret;
  403. }
  404. #ifdef RT_USING_FINSH
  405. /* copy from components/finsh/cmd.c */
  406. static void object_split(int len)
  407. {
  408. while (len--) rt_kprintf("-");
  409. }
  410. static void print_thread_info(struct rt_thread* thread, int maxlen)
  411. {
  412. rt_uint8_t *ptr;
  413. rt_uint8_t stat;
  414. #ifdef RT_USING_SMP
  415. if (thread->oncpu != RT_CPU_DETACHED)
  416. rt_kprintf("%-*.*s %3d %3d ", maxlen, RT_NAME_MAX, thread->name, thread->oncpu, thread->current_priority);
  417. else
  418. rt_kprintf("%-*.*s N/A %3d ", maxlen, RT_NAME_MAX, thread->name, thread->current_priority);
  419. #else
  420. rt_kprintf("%-*.*s %3d ", maxlen, RT_NAME_MAX, thread->name, thread->current_priority);
  421. #endif /*RT_USING_SMP*/
  422. stat = (thread->stat & RT_THREAD_STAT_MASK);
  423. if (stat == RT_THREAD_READY) rt_kprintf(" ready ");
  424. else if ((stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK) rt_kprintf(" suspend");
  425. else if (stat == RT_THREAD_INIT) rt_kprintf(" init ");
  426. else if (stat == RT_THREAD_CLOSE) rt_kprintf(" close ");
  427. else if (stat == RT_THREAD_RUNNING) rt_kprintf(" running");
  428. #if defined(ARCH_CPU_STACK_GROWS_UPWARD)
  429. ptr = (rt_uint8_t *)thread->stack_addr + thread->stack_size;
  430. while (*ptr == '#')ptr --;
  431. rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n",
  432. ((rt_uint32_t)thread->sp - (rt_uint32_t)thread->stack_addr),
  433. thread->stack_size,
  434. ((rt_uint32_t)ptr - (rt_uint32_t)thread->stack_addr) * 100 / thread->stack_size,
  435. thread->remaining_tick,
  436. thread->error);
  437. #else
  438. ptr = (rt_uint8_t *)thread->stack_addr;
  439. while (*ptr == '#')ptr ++;
  440. rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n",
  441. (thread->stack_size + (rt_uint32_t)(rt_size_t)thread->stack_addr - (rt_uint32_t)(rt_size_t)thread->sp),
  442. thread->stack_size,
  443. (thread->stack_size + (rt_uint32_t)(rt_size_t)thread->stack_addr - (rt_uint32_t)(rt_size_t)ptr) * 100
  444. / thread->stack_size,
  445. thread->remaining_tick,
  446. thread->error);
  447. #endif
  448. }
  449. long list_process(void)
  450. {
  451. int index;
  452. int maxlen;
  453. rt_ubase_t level;
  454. struct rt_lwp* lwp = RT_NULL;
  455. struct rt_thread *thread;
  456. struct rt_list_node *node, *list;
  457. const char *item_title = "thread";
  458. int count = 0;
  459. struct rt_thread **threads;
  460. maxlen = RT_NAME_MAX;
  461. #ifdef RT_USING_SMP
  462. rt_kprintf("%-*.s %-*.s %-*.s cpu pri status sp stack size max used left tick error\n", 4, "PID", maxlen, "CMD", maxlen, item_title);
  463. object_split(4);rt_kprintf(" ");object_split(maxlen);rt_kprintf(" ");object_split(maxlen);rt_kprintf(" ");
  464. rt_kprintf( "--- --- ------- ---------- ---------- ------ ---------- ---\n");
  465. #else
  466. rt_kprintf("%-*.s %-*.s %-*.s pri status sp stack size max used left tick error\n", 4, "PID", maxlen, "CMD", maxlen, item_title);
  467. object_split(4);rt_kprintf(" ");object_split(maxlen);rt_kprintf(" ");object_split(maxlen);rt_kprintf(" ");
  468. rt_kprintf( "--- ------- ---------- ---------- ------ ---------- ---\n");
  469. #endif /*RT_USING_SMP*/
  470. count = rt_object_get_length(RT_Object_Class_Thread);
  471. if (count > 0)
  472. {
  473. /* get thread pointers */
  474. threads = (struct rt_thread **)rt_calloc(count, sizeof(struct rt_thread *));
  475. if (threads)
  476. {
  477. index = rt_object_get_pointers(RT_Object_Class_Thread, (rt_object_t *)threads, count);
  478. if (index > 0)
  479. {
  480. for (index = 0; index <count; index ++)
  481. {
  482. struct rt_thread th;
  483. thread = threads[index];
  484. level = rt_hw_interrupt_disable();
  485. if ((thread->type & ~RT_Object_Class_Static) != RT_Object_Class_Thread)
  486. {
  487. rt_hw_interrupt_enable(level);
  488. continue;
  489. }
  490. rt_memcpy(&th, thread, sizeof(struct rt_thread));
  491. rt_hw_interrupt_enable(level);
  492. if (th.lwp == RT_NULL)
  493. {
  494. rt_kprintf(" %-*.*s ", maxlen, RT_NAME_MAX, "kernel");
  495. print_thread_info(&th, maxlen);
  496. }
  497. }
  498. }
  499. rt_free(threads);
  500. }
  501. }
  502. for (index = 0; index < RT_LWP_MAX_NR; index++)
  503. {
  504. if (pid_struct.pidmap[index])
  505. {
  506. lwp = pid_struct.pidmap[index];
  507. list = &lwp->t_grp;
  508. for (node = list->next; node != list; node = node->next)
  509. {
  510. thread = rt_list_entry(node, struct rt_thread, sibling);
  511. rt_kprintf("%4d %-*.*s ", lwp_to_pid(lwp), maxlen, RT_NAME_MAX, lwp->cmd);
  512. print_thread_info(thread, maxlen);
  513. }
  514. }
  515. }
  516. return 0;
  517. }
  518. MSH_CMD_EXPORT(list_process, list process);
  519. static void cmd_kill(int argc, char** argv)
  520. {
  521. int pid;
  522. int sig = 0;
  523. if (argc < 2)
  524. {
  525. rt_kprintf("kill pid or kill pid -s signal\n");
  526. return;
  527. }
  528. pid = atoi(argv[1]);
  529. if (argc >= 4)
  530. {
  531. if (argv[2][0] == '-' && argv[2][1] == 's')
  532. {
  533. sig = atoi(argv[3]);
  534. }
  535. }
  536. lwp_kill(pid, sig);
  537. }
  538. MSH_CMD_EXPORT_ALIAS(cmd_kill, kill, send a signal to a process);
  539. static void cmd_killall(int argc, char** argv)
  540. {
  541. int pid;
  542. if (argc < 2)
  543. {
  544. rt_kprintf("killall processes_name\n");
  545. return;
  546. }
  547. while((pid = lwp_name2pid(argv[1])) >= 0)
  548. {
  549. lwp_kill(pid, 0);
  550. rt_thread_mdelay(100);
  551. }
  552. }
  553. MSH_CMD_EXPORT_ALIAS(cmd_killall, killall, kill processes by name);
  554. #endif
  555. int lwp_check_exit_request(void)
  556. {
  557. rt_thread_t thread = rt_thread_self();
  558. if (!thread->lwp)
  559. {
  560. return 0;
  561. }
  562. if (thread->exit_request == LWP_EXIT_REQUEST_TRIGGERED)
  563. {
  564. thread->exit_request = LWP_EXIT_REQUEST_IN_PROCESS;
  565. return 1;
  566. }
  567. return 0;
  568. }
  569. static int found_thread(struct rt_lwp* lwp, rt_thread_t thread)
  570. {
  571. int found = 0;
  572. rt_base_t level;
  573. rt_list_t *list;
  574. level = rt_hw_interrupt_disable();
  575. list = lwp->t_grp.next;
  576. while (list != &lwp->t_grp)
  577. {
  578. rt_thread_t iter_thread;
  579. iter_thread = rt_list_entry(list, struct rt_thread, sibling);
  580. if (thread == iter_thread)
  581. {
  582. found = 1;
  583. break;
  584. }
  585. list = list->next;
  586. }
  587. rt_hw_interrupt_enable(level);
  588. return found;
  589. }
  590. void lwp_request_thread_exit(rt_thread_t thread_to_exit)
  591. {
  592. rt_thread_t main_thread;
  593. rt_base_t level;
  594. rt_list_t *list;
  595. struct rt_lwp *lwp;
  596. lwp = lwp_self();
  597. if ((!thread_to_exit) || (!lwp))
  598. {
  599. return;
  600. }
  601. level = rt_hw_interrupt_disable();
  602. main_thread = rt_list_entry(lwp->t_grp.prev, struct rt_thread, sibling);
  603. if (thread_to_exit == main_thread)
  604. {
  605. goto finish;
  606. }
  607. if ((struct rt_lwp *)thread_to_exit->lwp != lwp)
  608. {
  609. goto finish;
  610. }
  611. for (list = lwp->t_grp.next; list != &lwp->t_grp; list = list->next)
  612. {
  613. rt_thread_t thread;
  614. thread = rt_list_entry(list, struct rt_thread, sibling);
  615. if (thread != thread_to_exit)
  616. {
  617. continue;
  618. }
  619. if (thread->exit_request == LWP_EXIT_REQUEST_NONE)
  620. {
  621. thread->exit_request = LWP_EXIT_REQUEST_TRIGGERED;
  622. }
  623. if ((thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK)
  624. {
  625. thread->error = RT_EINTR;
  626. dsb();
  627. rt_thread_wakeup(thread);
  628. }
  629. break;
  630. }
  631. while (found_thread(lwp, thread_to_exit))
  632. {
  633. rt_thread_mdelay(10);
  634. }
  635. finish:
  636. rt_hw_interrupt_enable(level);
  637. return;
  638. }
  639. void lwp_terminate(struct rt_lwp *lwp)
  640. {
  641. rt_base_t level;
  642. rt_list_t *list;
  643. if (!lwp)
  644. {
  645. /* kernel thread not support */
  646. return;
  647. }
  648. level = rt_hw_interrupt_disable();
  649. for (list = lwp->t_grp.next; list != &lwp->t_grp; list = list->next)
  650. {
  651. rt_thread_t thread;
  652. thread = rt_list_entry(list, struct rt_thread, sibling);
  653. if (thread->exit_request == LWP_EXIT_REQUEST_NONE)
  654. {
  655. thread->exit_request = LWP_EXIT_REQUEST_TRIGGERED;
  656. }
  657. if ((thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK)
  658. {
  659. thread->error = RT_EINTR;
  660. dsb();
  661. rt_thread_wakeup(thread);
  662. }
  663. }
  664. rt_hw_interrupt_enable(level);
  665. }
  666. void lwp_wait_subthread_exit(void)
  667. {
  668. rt_base_t level;
  669. struct rt_lwp *lwp;
  670. rt_thread_t thread;
  671. rt_thread_t main_thread;
  672. lwp = lwp_self();
  673. if (!lwp) return;
  674. thread = rt_thread_self();
  675. main_thread = rt_list_entry(lwp->t_grp.prev, struct rt_thread, sibling);
  676. if (thread != main_thread)
  677. {
  678. return;
  679. }
  680. while (1)
  681. {
  682. int subthread_is_terminated;
  683. level = rt_hw_interrupt_disable();
  684. subthread_is_terminated = (int)(thread->sibling.prev == &lwp->t_grp);
  685. if (!subthread_is_terminated)
  686. {
  687. rt_thread_t sub_thread;
  688. rt_list_t *list;
  689. int all_subthread_in_init = 1;
  690. /* check all subthread is in init state */
  691. for (list = thread->sibling.prev; list != &lwp->t_grp; list = list->prev)
  692. {
  693. sub_thread = rt_list_entry(list, struct rt_thread, sibling);
  694. if ((sub_thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  695. {
  696. all_subthread_in_init = 0;
  697. break;
  698. }
  699. }
  700. if (all_subthread_in_init)
  701. {
  702. /* delete all subthread */
  703. while ((list = thread->sibling.prev) != &lwp->t_grp)
  704. {
  705. sub_thread = rt_list_entry(list, struct rt_thread, sibling);
  706. rt_list_remove(&sub_thread->sibling);
  707. rt_thread_delete(sub_thread);
  708. }
  709. subthread_is_terminated = 1;
  710. }
  711. }
  712. rt_hw_interrupt_enable(level);
  713. if (subthread_is_terminated)
  714. {
  715. break;
  716. }
  717. rt_thread_mdelay(10);
  718. }
  719. }