From c9323cd90185c4c3521e4680c54e37b9ca651a96 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Thu, 25 Jun 2009 13:42:41 +0200 Subject: [PATCH] BUGFIX: Fix a possible deadlock if an error occurs --- communication_techniques/src/communication/common.c | 7 ++++--- communication_techniques/src/communication/pipe.c | 6 +++++- communication_techniques/src/communication/shared_mem.c | 5 +++++ .../src/communication/shared_mem_opt.c | 5 +++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/communication_techniques/src/communication/common.c b/communication_techniques/src/communication/common.c index 4c4e39d..900f027 100644 --- a/communication_techniques/src/communication/common.c +++ b/communication_techniques/src/communication/common.c @@ -47,12 +47,13 @@ int get_thread_number(void) int init_producer_thread(void) { - int i_local; + int thread_num; - i_local = get_thread_number(); - if (init_thread_comm(&tcomms[i_local])) + thread_num = get_thread_number(); + if (init_thread_comm(&tcomms[thread_num])) { error = 1; + pthread_cond_signal(&init_cond); return -1; } pthread_mutex_lock(&init_lock); diff --git a/communication_techniques/src/communication/pipe.c b/communication_techniques/src/communication/pipe.c index 6172b79..e903954 100644 --- a/communication_techniques/src/communication/pipe.c +++ b/communication_techniques/src/communication/pipe.c @@ -17,7 +17,11 @@ int init_thread_comm(struct thread_comm *comm) { int flags; - pipe(pipefd); + if (pipe(pipefd)) + { + fprintf(stderr, "Unable to create a pipe for pipe communication\n"); + return -1; + } flags = fcntl(pipefd[READ_IDX], F_GETFL); fcntl(pipefd[READ_IDX], F_SETFL, flags | O_NONBLOCK); comm->pipefd = pipefd; diff --git a/communication_techniques/src/communication/shared_mem.c b/communication_techniques/src/communication/shared_mem.c index d0a13b3..e96c5f9 100644 --- a/communication_techniques/src/communication/shared_mem.c +++ b/communication_techniques/src/communication/shared_mem.c @@ -16,6 +16,11 @@ __thread volatile int prod_idx = 0; int init_thread_comm(struct thread_comm *comm) { shared_space = (volatile void **) malloc(SHARED_SPACE_SIZE); + if (shared_space == NULL) + { + fprintf(stderr, "Unable to allocate space for shared mem communication\n"); + return -1; + } comm->shared_space = shared_space; comm->cons_idx = &cons_idx; comm->prod_idx = &prod_idx; diff --git a/communication_techniques/src/communication/shared_mem_opt.c b/communication_techniques/src/communication/shared_mem_opt.c index 0be430a..df726c7 100644 --- a/communication_techniques/src/communication/shared_mem_opt.c +++ b/communication_techniques/src/communication/shared_mem_opt.c @@ -16,6 +16,11 @@ __thread volatile int prod_idx = 0; int init_thread_comm(struct thread_comm *comm) { shared_space = (volatile void **) malloc(SHARED_SPACE_SIZE); + if (shared_space == NULL) + { + fprintf(stderr, "Unable to allocate space for shared mem communication\n"); + return -1; + } comm->shared_space = shared_space; comm->cons_idx = &cons_idx; comm->prod_idx = &prod_idx;