From 1561c119cce1520d294d3ba0e3bed40b9641c400 Mon Sep 17 00:00:00 2001 From: Jaeyun Jung Date: Thu, 1 Feb 2024 12:25:53 +0900 Subject: [PATCH] [CodeClean] fix svace issue Code clean, fix svace issues about lock in single/pipeline handle. Signed-off-by: Jaeyun Jung --- c/src/ml-api-common.c | 6 ++++-- c/src/ml-api-inference-pipeline.c | 4 ---- c/src/ml-api-inference-single.c | 27 ++++++++++++++------------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/c/src/ml-api-common.c b/c/src/ml-api-common.c index 06b9cf8..d022a9d 100644 --- a/c/src/ml-api-common.c +++ b/c/src/ml-api-common.c @@ -671,10 +671,12 @@ _ml_tensors_data_destroy_internal (ml_tensors_data_h data, gboolean free_data) if (free_data) { if (_data->destroy) { status = _data->destroy (_data, _data->user_data); - if (status != ML_ERROR_NONE) + if (status != ML_ERROR_NONE) { + G_UNLOCK_UNLESS_NOLOCK (*_data); _ml_error_report_return_continue (status, "Tried to destroy internal user_data of the given parameter, data, with its destroy callback; however, it has failed with %d.", status); + } } else { for (i = 0; i < ML_TENSOR_SIZE_LIMIT; i++) { if (_data->tensors[i].data) { @@ -860,7 +862,7 @@ ml_tensors_data_create (const ml_tensors_info_h info, ml_tensors_data_h * data) status = ml_tensors_info_validate (info, &valid); if (status != ML_ERROR_NONE) _ml_error_report_return_continue (status, - "_ml_error_report_return_continue has reported that the parameter, info, is not NULL, but its contents are not valid. The user must provide a valid tensor information with it."); + "ml_tensors_info_validate() has reported that the parameter, info, is not NULL, but its contents are not valid. The user must provide a valid tensor information with it."); if (!valid) _ml_error_report_return (ML_ERROR_INVALID_PARAMETER, "The parameter, info, is not NULL, but its contents are not valid. The user must provide a valid tensor information with it. Probably, there is an entry that is not allocated or dimension/type information not available. The given info should have valid number of tensors, entries of every tensor along with its type and dimension info."); diff --git a/c/src/ml-api-inference-pipeline.c b/c/src/ml-api-inference-pipeline.c index b806777..c542bd3 100644 --- a/c/src/ml-api-inference-pipeline.c +++ b/c/src/ml-api-inference-pipeline.c @@ -1776,15 +1776,11 @@ static ml_pipeline_src_callbacks_s * get_app_src_callback (ml_pipeline_common_elem * src_h, void **data) { ml_pipeline_src_callbacks_s *src_cb = NULL; - ml_pipeline_element *elem; - elem = src_h->element; - g_mutex_lock (&elem->lock); if (src_h->callback_info) { src_cb = &src_h->callback_info->src_cb; *data = src_h->callback_info->src_pdata; } - g_mutex_unlock (&elem->lock); return src_cb; } diff --git a/c/src/ml-api-inference-single.c b/c/src/ml-api-inference-single.c index d0d1d57..f099d66 100644 --- a/c/src/ml-api-inference-single.c +++ b/c/src/ml-api-inference-single.c @@ -394,7 +394,8 @@ set_destroy_notify (ml_single * single_h, ml_tensors_data_s * data, * @brief Internal function to call subplugin's invoke */ static inline int -__invoke (ml_single * single_h, ml_tensors_data_h in, ml_tensors_data_h out) +__invoke (ml_single * single_h, ml_tensors_data_h in, ml_tensors_data_h out, + gboolean alloc_output) { ml_tensors_data_s *in_data, *out_data; int status = ML_ERROR_NONE; @@ -410,7 +411,7 @@ __invoke (ml_single * single_h, ml_tensors_data_h in, ml_tensors_data_h out) /* Invoke the thread. */ if (!single_h->klass->invoke (single_h->filter, in_data->tensors, - out_data->tensors, single_h->free_output)) { + out_data->tensors, alloc_output)) { const char *fw_name = _ml_get_nnfw_subplugin_name (single_h->nnfw); _ml_error_report ("Failed to invoke the tensors. The invoke callback of the tensor-filter subplugin '%s' has failed. Please contact the author of tensor-filter-%s (nnstreamer-%s) or review its source code. Note that this usually happens when the designated framework does not support the given model (e.g., trying to run tf-lite 2.6 model with tf-lite 1.13).", @@ -423,17 +424,13 @@ __invoke (ml_single * single_h, ml_tensors_data_h in, ml_tensors_data_h out) /** * @brief Internal function to post-process given output. + * @note Do not call this if single_h->free_output is false (output data is not allocated in single-shot). */ static inline void __process_output (ml_single * single_h, ml_tensors_data_h output) { ml_tensors_data_s *out_data; - if (!single_h->free_output) { - /* Do nothing. The output handle is not allocated in single-shot process. */ - return; - } - if (g_list_find (single_h->destroy_data_list, output)) { /** * Caller of the invoke thread has returned back with timeout. @@ -472,6 +469,7 @@ invoke_thread (void *arg) { ml_single *single_h; ml_tensors_data_h input, output; + gboolean alloc_output = FALSE; single_h = (ml_single *) arg; @@ -493,15 +491,16 @@ invoke_thread (void *arg) single_h->input = single_h->output = NULL; single_h->invoking = TRUE; + alloc_output = single_h->free_output; g_mutex_unlock (&single_h->mutex); - status = __invoke (single_h, input, output); + status = __invoke (single_h, input, output, alloc_output); g_mutex_lock (&single_h->mutex); /* Clear input data after invoke is done. */ ml_tensors_data_destroy (input); single_h->invoking = FALSE; if (status != ML_ERROR_NONE || single_h->state == JOIN_REQUESTED) { - if (single_h->free_output) { + if (alloc_output) { single_h->destroy_data_list = g_list_remove (single_h->destroy_data_list, output); ml_tensors_data_destroy (output); @@ -512,7 +511,8 @@ invoke_thread (void *arg) goto wait_for_next; } - __process_output (single_h, output); + if (alloc_output) + __process_output (single_h, output); /** loop over to wait for the next element */ wait_for_next: @@ -529,7 +529,7 @@ exit: if (single_h->input) ml_tensors_data_destroy (single_h->input); - if (single_h->free_output && single_h->output) { + if (alloc_output && single_h->output) { single_h->destroy_data_list = g_list_remove (single_h->destroy_data_list, single_h->output); ml_tensors_data_destroy (single_h->output); @@ -1461,7 +1461,7 @@ _ml_single_invoke_internal (ml_single_h single, * having yet another mutex for __invoke. */ single_h->invoking = TRUE; - status = __invoke (single_h, _in, _out); + status = __invoke (single_h, _in, _out, need_alloc); ml_tensors_data_destroy (_in); single_h->invoking = FALSE; single_h->state = IDLE; @@ -1472,7 +1472,8 @@ _ml_single_invoke_internal (ml_single_h single, goto exit; } - __process_output (single_h, _out); + if (need_alloc) + __process_output (single_h, _out); } exit: -- 2.7.4