From e56c91ff6eecdb1663bb1722a4081ef2f190e9c0 Mon Sep 17 00:00:00 2001 From: uphantom Date: Fri, 28 Aug 2015 18:37:12 +0800 Subject: suport multi-thread reader --- htk_io/src/cwrapper.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) (limited to 'htk_io/src/cwrapper.cpp') diff --git a/htk_io/src/cwrapper.cpp b/htk_io/src/cwrapper.cpp index b7ce2d5..66cde23 100644 --- a/htk_io/src/cwrapper.cpp +++ b/htk_io/src/cwrapper.cpp @@ -8,6 +8,7 @@ extern "C" { #include "cwrapper.h" #include "string.h" +#include "pthread.h" #include "nerv/common.h" extern Matrix *nerv_matrix_host_float_create(long nrow, long ncol, Status *status); @@ -29,6 +30,7 @@ extern "C" { const char* cvn_mask; const char* cvg_file; TNet::Matrix feats_host; /* KaldiLib implementation */ + int refcount; }; TNetFeatureRepo *tnet_feature_repo_new(const char *p_script, const char *config, int context) { @@ -53,6 +55,18 @@ extern "C" { return repo; } + TNetFeatureRepo *tnet_feature_repo_newWithId(long id) + { + TNetFeatureRepo *repo = (TNetFeatureRepo*)id; + __sync_fetch_and_add(&repo->refcount, 1); + return repo; + } + + long tnet_feature_repo_id(TNetFeatureRepo *repo) + { + return (long)(repo); + } + Matrix *tnet_feature_repo_read_utterance(TNetFeatureRepo *repo, lua_State *L, int debug) { Matrix *mat; /* nerv implementation */ repo->feature_repo.ReadFullMatrix(repo->feats_host); @@ -93,12 +107,19 @@ extern "C" { } void tnet_feature_repo_destroy(TNetFeatureRepo *repo) { - if (repo->cmn_mask) - free(repo->cmn_path); - if (repo->cvn_mask) - free(repo->cvn_path); - free(repo->p_deriv_win_lenghts); - delete repo; + if (NULL != repo) + { + if(__sync_fetch_and_add(&repo->refcount, -1) == 1) + { + if (repo->cmn_mask) + free(repo->cmn_path); + if (repo->cvn_mask) + free(repo->cvn_path); + free(repo->p_deriv_win_lenghts); + delete repo; + repo = NULL; + } + } } struct TNetLabelRepo { @@ -114,6 +135,16 @@ extern "C" { return repo; } + TNetLabelRepo *tnet_label_repo_newWithId(long id) + { + return (TNetLabelRepo*)id; + } + + long tnet_label_repo_id(TNetLabelRepo *repo) + { + return (long)(repo); + } + Matrix *tnet_label_repo_read_utterance(TNetLabelRepo *repo, size_t frames, size_t sample_rate, @@ -143,6 +174,10 @@ extern "C" { } void tnet_label_repo_destroy(TNetLabelRepo *repo) { - delete repo; + if (NULL != repo) + { + delete repo; + repo = NULL; + } } } -- cgit v1.2.3