Browse Source

Merge pull request #4334 from mysterywolf/exit

[libc][exit][abort] 重新实现exit和abort函数
Bernard Xiong 4 years ago
parent
commit
bfde38030b

+ 18 - 2
components/libc/compilers/armlibc/stubs.c

@@ -11,6 +11,7 @@
  *                             in msh.
  * 2020-08-05     Meco Man     fixed _sys_flen() compiling-warning when 
  *                             RT_USING_DFS is not defined
+ * 2020-02-13     Meco Man     re-implement exit() and abort()
  */
 
 #include <string.h>
@@ -255,8 +256,23 @@ void _ttywrch(int ch)
 
 RT_WEAK void _sys_exit(int return_code)
 {
-    /* TODO: perhaps exit the thread which is invoking this function */
-    while (1);
+    rt_thread_t self = rt_thread_self();
+
+#ifdef RT_USING_MODULE
+    if (dlmodule_self())
+    {
+        dlmodule_exit(return_code);
+    }
+#endif
+
+    if (self != RT_NULL)
+    {
+        rt_kprintf("thread:%-8.*s exit:%d!\n", RT_NAME_MAX, self->name, return_code);
+        rt_thread_suspend(self);
+        rt_schedule();
+    }
+
+    while(1); /* noreturn */
 }
 
 /**

+ 52 - 0
components/libc/compilers/dlib/syscalls.c

@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2021-02-13     Meco Man     implement exit() and abort()
+ */
+#include <rtthread.h>
+
+void exit (int status)
+{
+    rt_thread_t self = rt_thread_self();
+
+#ifdef RT_USING_MODULE
+    if (dlmodule_self())
+    {
+        dlmodule_exit(status);
+    }
+#endif
+
+    if (self != RT_NULL)
+    {
+        rt_kprintf("thread:%-8.*s exit:%d!\n", RT_NAME_MAX, self->name, status);
+        rt_thread_suspend(self);
+        rt_schedule();
+    }
+
+    while(1); /* noreturn */
+}
+
+void abort(void)
+{
+    rt_thread_t self = rt_thread_self();
+
+#ifdef RT_USING_MODULE
+    if (dlmodule_self())
+    {
+        dlmodule_exit(-1);
+    }
+#endif
+
+    if (self != RT_NULL)
+    {
+        rt_kprintf("thread:%-8.*s abort!\n", RT_NAME_MAX, self->name);
+        rt_thread_suspend(self);
+        rt_schedule();
+    }
+
+    while(1); /* noreturn */
+}

+ 0 - 10
components/libc/compilers/dlib/syscalls.h

@@ -1,10 +0,0 @@
-/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date           Author       Notes
- * 2015-01-28     Bernard      first version
- */
-

+ 20 - 7
components/libc/compilers/newlib/syscalls.c

@@ -6,6 +6,7 @@
  * Change Logs:
  * Date           Author       Notes
  * 2021-02-11     Meco Man     remove _gettimeofday_r() and _times_r()
+ * 2020-02-13     Meco Man     re-implement exit() and abort()
  */
 
 #include <reent.h>
@@ -288,6 +289,8 @@ _free_r (struct _reent *ptr, void *addr)
 void
 exit (int status)
 {
+    rt_thread_t self = rt_thread_self();
+
 #ifdef RT_USING_MODULE
     if (dlmodule_self())
     {
@@ -295,10 +298,14 @@ exit (int status)
     }
 #endif
 
-    rt_kprintf("thread:%s exit with %d\n", rt_thread_self()->name, status);
-    RT_ASSERT(0);
+    if (self != RT_NULL)
+    {
+        rt_kprintf("thread:%-8.*s exit:%d!\n", RT_NAME_MAX, self->name, status);
+        rt_thread_suspend(self);
+        rt_schedule();
+    }
 
-    while (1);
+    while(1); /* noreturn */
 }
 
 void
@@ -315,17 +322,23 @@ void __libc_init_array(void)
 
 void abort(void)
 {
-    if (rt_thread_self())
+    rt_thread_t self = rt_thread_self();
+
+#ifdef RT_USING_MODULE
+    if (dlmodule_self())
     {
-        rt_thread_t self = rt_thread_self();
+        dlmodule_exit(-1);
+    }
+#endif
 
+    if (self != RT_NULL)
+    {
         rt_kprintf("thread:%-8.*s abort!\n", RT_NAME_MAX, self->name);
         rt_thread_suspend(self);
-
         rt_schedule();
     }
 
-    while (1);
+    while(1); /* noreturn */
 }
 
 uid_t getuid(void)