From 3dd6a0e7c955cb55d9be6188a6006ab646a3afb6 Mon Sep 17 00:00:00 2001 From: yinshengkai Date: Thu, 10 Aug 2023 18:28:20 +0800 Subject: [PATCH] examples/watcher: get the thread name using pthread_getname_np Signed-off-by: yinshengkai --- 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 9543eef1ac..973cdd1e5b 100644 --- a/examples/watcher/task_mn.c +++ b/examples/watcher/task_mn.c @@ -23,17 +23,15 @@ ****************************************************************************/ #include -#include #include #include #include #include #include #include -#include +#include #include -#include #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 d20b095057..d2439fedcd 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);