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

Fix for client listener threads async callback deadlock #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 24 additions & 1 deletion src/lib/bus/bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,9 @@ void Bus_BackpressureDelay(struct bus *b, size_t backpressure, uint8_t shift) {
}
}

static void box_execute_cb(void *udata) {
/* Function executes the callback in a separate thread
to not tie up kinetic listener threads */
static void* thread_execute_cb(void *udata) {
boxed_msg *box = (boxed_msg *)udata;

void *out_udata = box->udata;
Expand All @@ -566,6 +568,27 @@ static void box_execute_cb(void *udata) {

free(box);
cb(&res, out_udata);
return NULL;
}

static void box_execute_cb(void *udata) {
pthread_t thread;
boxed_msg *box = (boxed_msg *)udata;
/* Allocate & copy boxed_msg so that box_cleanup_cb can't interfere
with thread execution. If box_cleanup_cb was called during
thread_execute_cb execution (before cb is called), would get a segfault */
boxed_msg *thread_box = malloc(sizeof(*thread_box));
memcpy(thread_box, box, sizeof(*thread_box));

if(pthread_create(&thread, NULL, thread_execute_cb, (void*)thread_box) != 0) {
fprintf(stderr, "pthread_create: %s", strerror(errno));
errno = 0;
}
if(pthread_detach(thread) != 0) {
fprintf(stderr, "pthread_detach: %s", strerror(errno));
errno = 0;
}
free(box);
}

static void box_cleanup_cb(void *udata) {
Expand Down