From 7a69c840bc347e7d78a610b230721f40153dddfd Mon Sep 17 00:00:00 2001 From: Swann Perarnau Date: Wed, 28 Mar 2018 16:31:45 -0500 Subject: [PATCH] [feature] make dma_linux_par thread-safe Add mutex to make request creation and destruction thread-safe. Same as dma_linux_seq, the changes are quite simple, as we only need to protect modifications to the requests array. --- src/aml.h | 1 + src/dma_linux_par.c | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/src/aml.h b/src/aml.h index 731e8dc..8701d72 100644 --- a/src/aml.h +++ b/src/aml.h @@ -635,6 +635,7 @@ struct aml_dma_request_linux_par { struct aml_dma_linux_par_data { size_t nbthreads; struct aml_vector requests; + pthread_mutex_t lock; }; struct aml_dma_linux_par_ops { diff --git a/src/dma_linux_par.c b/src/dma_linux_par.c index 4e9575e..2774ac4 100644 --- a/src/dma_linux_par.c +++ b/src/dma_linux_par.c @@ -149,7 +149,9 @@ int aml_dma_linux_par_create_request(struct aml_dma_data *d, struct aml_dma_request_linux_par *req; + pthread_mutex_lock(&dma->data.lock); req = aml_vector_add(&dma->data.requests); + pthread_mutex_unlock(&dma->data.lock); /* init the request */ if(type == AML_DMA_REQUEST_TYPE_COPY) @@ -211,7 +213,9 @@ int aml_dma_linux_par_destroy_request(struct aml_dma_data *d, else if(req->type == AML_DMA_REQUEST_TYPE_MOVE) aml_dma_request_linux_par_move_destroy(req); + pthread_mutex_lock(&dma->data.lock); aml_vector_remove(&dma->data.requests, req); + pthread_mutex_unlock(&dma->data.lock); return 0; } @@ -233,7 +237,9 @@ int aml_dma_linux_par_wait_request(struct aml_dma_data *d, else if(req->type == AML_DMA_REQUEST_TYPE_MOVE) aml_dma_request_linux_par_move_destroy(req); + pthread_mutex_lock(&dma->data.lock); aml_vector_remove(&dma->data.requests, req); + pthread_mutex_unlock(&dma->data.lock); return 0; } @@ -288,6 +294,7 @@ int aml_dma_linux_par_vinit(struct aml_dma *d, va_list ap) req->thread_data = calloc(dma->data.nbthreads, sizeof(struct aml_dma_linux_par_thread_data)); } + pthread_mutex_init(&dma->data.lock, NULL); return 0; } int aml_dma_linux_par_init(struct aml_dma *d, ...) @@ -310,5 +317,6 @@ int aml_dma_linux_par_destroy(struct aml_dma *d) free(req->thread_data); } aml_vector_destroy(&dma->data.requests); + pthread_mutex_destroy(&dma->data.lock); return 0; } -- 2.26.2