Browse Source

[components/tty] fix bug on foreground app switch (#7726)

Shell 1 year ago
parent
commit
b66514686a
3 changed files with 18 additions and 12 deletions
  1. 2 2
      components/drivers/tty/include/tty.h
  2. 3 2
      components/drivers/tty/tty.c
  3. 13 8
      components/lwp/lwp.c

+ 2 - 2
components/drivers/tty/include/tty.h

@@ -179,8 +179,8 @@ enum
 #define TTY_DRIVER_TYPE_SYSTEM      0x0001
 #define TTY_DRIVER_TYPE_SYSTEM      0x0001
 #define TTY_DRIVER_TYPE_CONSOLE     0x0002
 #define TTY_DRIVER_TYPE_CONSOLE     0x0002
 #define TTY_DRIVER_TYPE_SERIAL      0x0003
 #define TTY_DRIVER_TYPE_SERIAL      0x0003
-#define TTY_DRIVER_TYPE_PTY     0x0004
-#define TTY_DRIVER_TYPE_SCC     0x0005  /* scc driver */
+#define TTY_DRIVER_TYPE_PTY         0x0004
+#define TTY_DRIVER_TYPE_SCC         0x0005  /* scc driver */
 #define TTY_DRIVER_TYPE_SYSCONS     0x0006
 #define TTY_DRIVER_TYPE_SYSCONS     0x0006
 
 
 /* tty magic number */
 /* tty magic number */

+ 3 - 2
components/drivers/tty/tty.c

@@ -5,7 +5,8 @@
  *
  *
  * Change Logs:
  * Change Logs:
  * Date           Author       Notes
  * Date           Author       Notes
- * 2021.12.07     linzhenxing      first version
+ * 2021-12-07     linzhenxing  first version
+ * 2023-06-26     WangXiaoyao  fix bug on foreground app switch
  */
  */
 #include <dfs_file.h>
 #include <dfs_file.h>
 #include <dfs_fs.h>
 #include <dfs_fs.h>
@@ -41,7 +42,7 @@ const struct termios tty_std_termios = {  /* for the benefit of tty drivers  */
 void tty_initstack(struct tty_node *node)
 void tty_initstack(struct tty_node *node)
 {
 {
     node->lwp = RT_NULL;
     node->lwp = RT_NULL;
-    node->next = node;
+    node->next = RT_NULL;
 }
 }
 
 
 static struct tty_node tty_node_cache = { RT_NULL, RT_NULL };
 static struct tty_node tty_node_cache = { RT_NULL, RT_NULL };

+ 13 - 8
components/lwp/lwp.c

@@ -10,6 +10,7 @@
  * 2021-02-03     lizhirui     add 64-bit arch support and riscv64 arch support
  * 2021-02-03     lizhirui     add 64-bit arch support and riscv64 arch support
  * 2021-08-26     linzhenxing  add lwp_setcwd\lwp_getcwd
  * 2021-08-26     linzhenxing  add lwp_setcwd\lwp_getcwd
  * 2023-02-20     wangxiaoyao  inv icache before new app startup
  * 2023-02-20     wangxiaoyao  inv icache before new app startup
+ * 2023-02-20     wangxiaoyao  fix bug on foreground app switch
  */
  */
 
 
 #include <rthw.h>
 #include <rthw.h>
@@ -1250,15 +1251,19 @@ pid_t lwp_execve(char *filename, int debug, int argc, char **argv, char **envp)
                     struct rt_lwp *old_lwp;
                     struct rt_lwp *old_lwp;
                     tty = (struct tty_struct *)console_tty_get();
                     tty = (struct tty_struct *)console_tty_get();
                     old_lwp = tty->foreground;
                     old_lwp = tty->foreground;
-                    rt_mutex_take(&tty->lock, RT_WAITING_FOREVER);
-                    ret = tty_push(&tty->head, old_lwp);
-                    rt_mutex_release(&tty->lock);
-                    if (ret < 0)
+                    if (old_lwp)
                     {
                     {
-                        lwp_tid_put(tid);
-                        lwp_ref_dec(lwp);
-                        LOG_E("malloc fail!\n");
-                        return -ENOMEM;
+                        rt_mutex_take(&tty->lock, RT_WAITING_FOREVER);
+                        ret = tty_push(&tty->head, old_lwp);
+                        rt_mutex_release(&tty->lock);
+
+                        if (ret < 0)
+                        {
+                            lwp_tid_put(tid);
+                            lwp_ref_dec(lwp);
+                            LOG_E("malloc fail!\n");
+                            return -ENOMEM;
+                        }
                     }
                     }
 
 
                     lwp->tty = tty;
                     lwp->tty = tty;