From edf8677c3703dcf599fed04c4b8c1a998582c3a1 Mon Sep 17 00:00:00 2001 From: Mark Charlebois Date: Fri, 10 Apr 2015 22:12:31 -0700 Subject: [PATCH] Linux: use string to store task name Converted px4_linux_tasks to C++ so the task struct can use a string. Sometimes the name string was in the stack of the calling function and goes out of scope. Signed-off-by: Mark Charlebois --- src/platforms/linux/px4_layer/module.mk | 2 +- ...{px4_linux_tasks.c => px4_linux_tasks.cpp} | 22 +++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) rename src/platforms/linux/px4_layer/{px4_linux_tasks.c => px4_linux_tasks.cpp} (93%) diff --git a/src/platforms/linux/px4_layer/module.mk b/src/platforms/linux/px4_layer/module.mk index aeb707b5c3..de6678eb92 100644 --- a/src/platforms/linux/px4_layer/module.mk +++ b/src/platforms/linux/px4_layer/module.mk @@ -37,7 +37,7 @@ SRCS = \ px4_linux_impl.cpp \ - px4_linux_tasks.c \ + px4_linux_tasks.cpp \ work_thread.c \ work_queue.c \ work_cancel.c \ diff --git a/src/platforms/linux/px4_layer/px4_linux_tasks.c b/src/platforms/linux/px4_layer/px4_linux_tasks.cpp similarity index 93% rename from src/platforms/linux/px4_layer/px4_linux_tasks.c rename to src/platforms/linux/px4_layer/px4_linux_tasks.cpp index 3c26acdcb5..a210f430ea 100644 --- a/src/platforms/linux/px4_layer/px4_linux_tasks.c +++ b/src/platforms/linux/px4_layer/px4_linux_tasks.cpp @@ -50,20 +50,22 @@ #include #include +#include #include #define MAX_CMD_LEN 100 #define PX4_MAX_TASKS 100 -typedef struct +struct task_entry { pthread_t pid; - const char *name; + std::string name; bool isused; -} task_entry; + task_entry() : isused(false) {} +}; -static task_entry taskmap[PX4_MAX_TASKS] = {}; +static task_entry taskmap[PX4_MAX_TASKS]; typedef struct { @@ -73,7 +75,7 @@ typedef struct // strings are allocated after the } pthdata_t; -static void entry_adapter ( void *ptr ) +static void *entry_adapter ( void *ptr ) { pthdata_t *data; data = (pthdata_t *) ptr; @@ -83,6 +85,8 @@ static void entry_adapter ( void *ptr ) printf("Before px4_task_exit\n"); px4_task_exit(0); printf("After px4_task_exit\n"); + + return NULL; } void @@ -156,12 +160,12 @@ px4_task_t px4_task_spawn_cmd(const char *name, int scheduler, int priority, int return (rv < 0) ? rv : -rv; } - rv = pthread_create (&task, &attr, (void *)&entry_adapter, (void *) taskdata); + rv = pthread_create (&task, &attr, &entry_adapter, (void *) taskdata); if (rv != 0) { if (rv == EPERM) { //printf("WARNING: NOT RUNING AS ROOT, UNABLE TO RUN REALTIME THREADS\n"); - rv = pthread_create (&task, NULL, (void *)&entry_adapter, (void *) taskdata); + rv = pthread_create (&task, NULL, &entry_adapter, (void *) taskdata); if (rv != 0) { printf("px4_task_spawn_cmd: failed to create thread %d %d\n", rv, errno); return (rv < 0) ? rv : -rv; @@ -225,7 +229,7 @@ void px4_task_exit(int ret) if (i>=PX4_MAX_TASKS) printf("px4_task_exit: self task not found!\n"); else - printf("px4_task_exit: %s\n", taskmap[i].name); + printf("px4_task_exit: %s\n", taskmap[i].name.c_str()); pthread_exit((void *)(unsigned long)ret); } @@ -267,7 +271,7 @@ void px4_show_tasks() for (idx=0; idx < PX4_MAX_TASKS; idx++) { if (taskmap[idx].isused) { - printf(" %-10s %lu\n", taskmap[idx].name, taskmap[idx].pid); + printf(" %-10s %lu\n", taskmap[idx].name.c_str(), taskmap[idx].pid); count++; } }