Skip to content

Commit

Permalink
examples/watcher: get the thread name using pthread_getname_np
Browse files Browse the repository at this point in the history
Signed-off-by: yinshengkai <[email protected]>
  • Loading branch information
Gary-Hobson committed Aug 10, 2023
1 parent 712d008 commit 3dd6a0e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 32 deletions.
42 changes: 11 additions & 31 deletions examples/watcher/task_mn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

/****************************************************************************
Expand Down Expand Up @@ -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 */
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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
{
Expand All @@ -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 */

Expand All @@ -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);
}
}

Expand Down
1 change: 0 additions & 1 deletion examples/watcher/task_mn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3dd6a0e

Please sign in to comment.