Skip to content

Commit

Permalink
AuxTasks: unblock and join threads from cleanup, and call cleanup fro…
Browse files Browse the repository at this point in the history
…m destructor. Closes #400"
  • Loading branch information
LBDonovan committed Apr 8, 2018
1 parent 43303b9 commit 2f50c69
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
29 changes: 22 additions & 7 deletions core/AuxTaskNonRT.cpp
Expand Up @@ -6,6 +6,11 @@
#include <stdlib.h>
#include <errno.h>

AuxTaskNonRT::AuxTaskNonRT(){}
AuxTaskNonRT::~AuxTaskNonRT(){
cleanup();
}

void AuxTaskNonRT::create(std::string _name, void(*_callback)()){
name = _name;
empty_callback = _callback;
Expand Down Expand Up @@ -110,13 +115,12 @@ void AuxTaskNonRT::cleanup(){
rt_pipe_delete(&pipe);
#endif
#ifdef XENOMAI_SKIN_posix
// TODO: someone needs to be done to terminate the tasks appropriately
// Currently they are probably just hanging on the pipes
// However the three lines below cause a segfault after the first time they are run
// char ptr[1];
// int ret = __wrap_sendto(pipeSocket, ptr, 1, 0, NULL, 0); // unblock the pipe
// pthread_cancel(thread);
// also we should join them!
// unblock and join thread
schedule();
int ret = __wrap_pthread_join(thread, NULL);
if (ret < 0){
fprintf(stderr, "AuxTaskNonRT %s: unable to join thread: (%i) %s\n", name.c_str(), ret, strerror(ret));
}
#endif
}

Expand All @@ -139,6 +143,8 @@ void AuxTaskNonRT::empty_loop(){
void* buf = malloc(1);
while(!gShouldStop){
read(pipe_fd, buf, 1);
if (gShouldStop)
break;
empty_callback();
}
free(buf);
Expand All @@ -147,6 +153,8 @@ void AuxTaskNonRT::str_loop(){
void* buf = malloc(AUX_MAX_BUFFER_SIZE);
while(!gShouldStop){
read(pipe_fd, buf, AUX_MAX_BUFFER_SIZE);
if (gShouldStop)
break;
str_callback((const char*)buf);
}
free(buf);
Expand All @@ -155,6 +163,8 @@ void AuxTaskNonRT::buf_loop(){
void* buf = malloc(AUX_MAX_BUFFER_SIZE);
while(!gShouldStop){
ssize_t size = read(pipe_fd, buf, AUX_MAX_BUFFER_SIZE);
if (gShouldStop)
break;
buf_callback(buf, size);
}
free(buf);
Expand All @@ -163,6 +173,8 @@ void AuxTaskNonRT::ptr_loop(){
void* buf = malloc(1);
while(!gShouldStop){
read(pipe_fd, buf, 1);
if (gShouldStop)
break;
ptr_callback(pointer);
}
free(buf);
Expand All @@ -171,6 +183,8 @@ void AuxTaskNonRT::ptr_buf_loop(){
void* buf = malloc(AUX_MAX_BUFFER_SIZE);
while(!gShouldStop){
ssize_t size = read(pipe_fd, buf, AUX_MAX_BUFFER_SIZE);
if (gShouldStop)
break;
ptr_buf_callback(pointer, buf, size);
}
free(buf);
Expand All @@ -193,4 +207,5 @@ void AuxTaskNonRT::loop(void* ptr){
} else if (instance->mode == 4){
instance->ptr_buf_loop();
}
// printf("AuxTaskNonRT %s exiting\n", instance->name.c_str());
}
14 changes: 13 additions & 1 deletion core/AuxTaskRT.cpp
Expand Up @@ -4,6 +4,11 @@
#include <Bela.h>
#include <stdlib.h>

AuxTaskRT::AuxTaskRT(){}
AuxTaskRT::~AuxTaskRT(){
cleanup();
}

void AuxTaskRT::create(std::string _name, void (*_callback)(), int _priority){
name = _name;
priority = _priority;
Expand Down Expand Up @@ -116,7 +121,13 @@ void AuxTaskRT::cleanup(){
rt_queue_delete(&queue);
#endif
#ifdef XENOMAI_SKIN_posix
//pthread_cancel(thread);
// unblock and join thread
schedule();
int ret = __wrap_pthread_join(thread, NULL);
if (ret < 0){
fprintf(stderr, "AuxTaskNonRT %s: unable to join thread: (%i) %s\n", name.c_str(), ret, strerror(ret));
}

__wrap_mq_close(queueDesc);
__wrap_mq_unlink(queueName.c_str());
#endif
Expand Down Expand Up @@ -264,4 +275,5 @@ void AuxTaskRT::loop(void* ptr){
} else if (instance->mode == 4){
instance->ptr_buf_loop();
}
// printf("AuxTaskRT %s exiting\n", instance->name.c_str());
}
5 changes: 3 additions & 2 deletions include/AuxTaskNonRT.h
Expand Up @@ -18,7 +18,8 @@

class AuxTaskNonRT{
public:
AuxTaskNonRT(){}
AuxTaskNonRT();
~AuxTaskNonRT();

void create(std::string _name, void(*_callback)());
void create(std::string _name, void(*_callback)(const char* str));
Expand All @@ -30,9 +31,9 @@ class AuxTaskNonRT{
void schedule(const char* str);
void schedule();

private:
void cleanup();

private:
#ifdef XENOMAI_SKIN_native
RT_TASK task;
RT_PIPE pipe;
Expand Down
5 changes: 3 additions & 2 deletions include/AuxTaskRT.h
Expand Up @@ -22,7 +22,8 @@

class AuxTaskRT{
public:
AuxTaskRT(){}
AuxTaskRT();
~AuxTaskRT();

void create(std::string _name, void(*_callback)(), int _priority = BELA_AUDIO_PRIORITY-5);
void create(std::string _name, void(*_callback)(const char* str), int _priority = BELA_AUDIO_PRIORITY-5);
Expand All @@ -34,9 +35,9 @@ class AuxTaskRT{
void schedule(const char* str);
void schedule();

private:
void cleanup();

private:
#ifdef XENOMAI_SKIN_native
RT_TASK task;
RT_QUEUE queue;
Expand Down

0 comments on commit 2f50c69

Please sign in to comment.