Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples/watcher: get the thread name using pthread_getname_np #1942

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading