You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2023/08/10 14:51:05 UTC

[nuttx-apps] branch master updated: examples/watcher: get the thread name using pthread_getname_np

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new d090da6b9 examples/watcher: get the thread name using pthread_getname_np
d090da6b9 is described below

commit d090da6b916b5dad686f6273992ac1259c584510
Author: yinshengkai <yi...@xiaomi.com>
AuthorDate: Thu Aug 10 18:28:20 2023 +0800

    examples/watcher: get the thread name using pthread_getname_np
    
    Signed-off-by: yinshengkai <yi...@xiaomi.com>
---
 examples/watcher/task_mn.c | 42 +++++++++++-------------------------------
 examples/watcher/task_mn.h |  1 -
 2 files changed, 11 insertions(+), 32 deletions(-)

diff --git a/examples/watcher/task_mn.c b/examples/watcher/task_mn.c
index 9543eef1a..973cdd1e5 100644
--- a/examples/watcher/task_mn.c
+++ b/examples/watcher/task_mn.c
@@ -23,17 +23,15 @@
  ****************************************************************************/
 
 #include <nuttx/config.h>
-#include <sys/boardctl.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <sys/ioctl.h>
+#include <pthread.h>
 #include <unistd.h>
 
-#include <nuttx/note/noteram_driver.h>
 #include "task_mn.h"
 
 /****************************************************************************
@@ -62,7 +60,6 @@ struct task_list_s watched_tasks =
 void task_mn_print_tasks_status(void)
 {
   int notefd;
-  struct noteram_get_taskname_s task;
   struct task_node_s *node;
 
   /* If the list is not empty */
@@ -82,15 +79,15 @@ void task_mn_print_tasks_status(void)
 
       for (node = watched_tasks.head; node != NULL; node = node->next)
         {
-          task.pid = node->task_id;
-          ioctl(notefd, NOTERAM_GETTASKNAME, (unsigned long)&task);
+          char taskname[CONFIG_NAME_MAX];
+          pthread_getname_np(node->task_id, taskname, sizeof(taskname));
           if (node->reset)
             {
-              printf("%s fed the dog.\n", task.taskname);
+              printf("%s fed the dog.\n", taskname);
             }
           else
             {
-              printf("%s starved the dog.\n", task.taskname);
+              printf("%s starved the dog.\n", taskname);
             }
         }
 
@@ -241,32 +238,16 @@ void task_mn_remove_from_list(pid_t id)
   fprintf(stderr, "watcher daemon: This node is not in the list.\n");
 }
 
-void task_mn_get_task_name(struct noteram_get_taskname_s *task)
-{
-  int notefd;
-
-  notefd = open("/dev/note/ram", O_RDONLY);
-  if (notefd < 0)
-    {
-      fprintf(stderr, "trace: cannot open /dev/note/ram\n");
-      return;
-    }
-
-  ioctl(notefd, NOTERAM_GETTASKNAME, (unsigned long)task);
-  close(notefd);
-}
-
 void task_mn_subscribe(pid_t id)
 {
-  struct noteram_get_taskname_s task;
+  char taskname[CONFIG_NAME_MAX];
 
   /* Verify if the task exists in the list */
 
   if (task_mn_is_task_subscribed(id) != NULL)
     {
-      task.pid = id;
-      task_mn_get_task_name(&task);
-      printf("Task %s was already subscribed\n", task.taskname);
+      pthread_getname_np(id, taskname, sizeof(taskname));
+      printf("Task %s was already subscribed\n", taskname);
     }
   else
     {
@@ -278,7 +259,7 @@ void task_mn_subscribe(pid_t id)
 
 void task_mn_unsubscribe(pid_t id)
 {
-  struct noteram_get_taskname_s task;
+  char taskname[CONFIG_NAME_MAX];
 
   /* Verify if the task exists in the list */
 
@@ -290,9 +271,8 @@ void task_mn_unsubscribe(pid_t id)
     }
   else
     {
-      task.pid = id;
-      task_mn_get_task_name(&task);
-      printf("Task %s is not subscribed\n", task.taskname);
+      pthread_getname_np(id, taskname, sizeof(taskname));
+      printf("Task %s is not subscribed\n", taskname);
     }
 }
 
diff --git a/examples/watcher/task_mn.h b/examples/watcher/task_mn.h
index d20b09505..d2439fedc 100644
--- a/examples/watcher/task_mn.h
+++ b/examples/watcher/task_mn.h
@@ -71,7 +71,6 @@ void task_mn_reset_all(void);
 struct task_node_s *task_mn_is_task_subscribed(pid_t id);
 void task_mn_add_to_list(pid_t id);
 void task_mn_remove_from_list(pid_t id);
-void task_mn_get_task_name(struct noteram_get_taskname_s *task);
 void task_mn_subscribe(pid_t id);
 void task_mn_unsubscribe(pid_t id);
 bool task_mn_all_tasks_fed(void);