diff --git a/include/neuron/adapter.h b/include/neuron/adapter.h index 905eb77e..6466742c 100644 --- a/include/neuron/adapter.h +++ b/include/neuron/adapter.h @@ -820,23 +820,27 @@ static inline void neu_resp_read_free(neu_resp_read_group_t *resp) utarray_free(resp->tags); } +typedef struct { + uint16_t index; + pthread_mutex_t mtx; +} neu_reqresp_trans_data_ctx_t; + typedef struct { char driver[NEU_NODE_NAME_LEN]; char group[NEU_GROUP_NAME_LEN]; - uint16_t index; - pthread_mutex_t mtx; - UT_array * tags; // neu_resp_tag_value_meta_t + neu_reqresp_trans_data_ctx_t *ctx; + UT_array * tags; // neu_resp_tag_value_meta_t } neu_reqresp_trans_data_t; static inline void neu_trans_data_free(neu_reqresp_trans_data_t *data) { - pthread_mutex_lock(&data->mtx); - if (data->index > 0) { - data->index -= 1; + pthread_mutex_lock(&data->ctx->mtx); + if (data->ctx->index > 0) { + data->ctx->index -= 1; } - if (data->index == 0) { + if (data->ctx->index == 0) { utarray_foreach(data->tags, neu_resp_tag_value_meta_t *, tag_value) { if (tag_value->value.type == NEU_TYPE_PTR) { @@ -844,10 +848,11 @@ static inline void neu_trans_data_free(neu_reqresp_trans_data_t *data) } } utarray_free(data->tags); - pthread_mutex_unlock(&data->mtx); - pthread_mutex_destroy(&data->mtx); + pthread_mutex_unlock(&data->ctx->mtx); + pthread_mutex_destroy(&data->ctx->mtx); + free(data->ctx); } else { - pthread_mutex_unlock(&data->mtx); + pthread_mutex_unlock(&data->ctx->mtx); } } diff --git a/src/adapter/driver/driver.c b/src/adapter/driver/driver.c index e18708b3..0a5a65e1 100644 --- a/src/adapter/driver/driver.c +++ b/src/adapter/driver/driver.c @@ -225,9 +225,10 @@ static void update_im(neu_adapter_t *adapter, const char *group, if (find != NULL) { pthread_mutex_lock(&find->apps_mtx); - data->index = utarray_len(find->apps); - if (data->index > 0) { - pthread_mutex_init(&data->mtx, NULL); + data->ctx = calloc(1, sizeof(neu_reqresp_trans_data_ctx_t)); + data->ctx->index = utarray_len(find->apps); + if (data->ctx->index > 0) { + pthread_mutex_init(&data->ctx->mtx, NULL); utarray_foreach(find->apps, sub_app_t *, app) { @@ -238,6 +239,7 @@ static void update_im(neu_adapter_t *adapter, const char *group, } } else { utarray_free(data->tags); + free(data->ctx); } pthread_mutex_unlock(&find->apps_mtx); @@ -1271,10 +1273,11 @@ static int report_callback(void *usr_data) if (utarray_len(data->tags) > 0) { pthread_mutex_lock(&group->apps_mtx); - data->index = utarray_len(group->apps); + data->ctx = calloc(1, sizeof(neu_reqresp_trans_data_ctx_t)); + data->ctx->index = utarray_len(group->apps); - if (data->index > 0) { - pthread_mutex_init(&data->mtx, NULL); + if (data->ctx->index > 0) { + pthread_mutex_init(&data->ctx->mtx, NULL); utarray_foreach(group->apps, sub_app_t *, app) { @@ -1286,6 +1289,7 @@ static int report_callback(void *usr_data) } } else { utarray_free(data->tags); + free(data->ctx); } pthread_mutex_unlock(&group->apps_mtx);