summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTony Mak <tonymak@google.com>2019-04-08 13:20:02 +0100
committerTony Mak <tonymak@google.com>2019-04-08 14:58:48 +0000
commit3bfe9eb1178f416c997434cd2361e4c6bcf2aaf9 (patch)
tree4581328309a1dbddf8d66c730bfff820c2e845ac
parent842411f93909f60b1ca1cdf67cad2e7994576f9e (diff)
downloadlibtextclassifier-3bfe9eb1178f416c997434cd2361e4c6bcf2aaf9.tar.gz
Import libtextclassifier to fix thread safety issue in LangID
BUG: 130153160 Test: atest frameworks/base/core/tests/coretests/src/android/view/textclassifier/ Change-Id: I76c125a026e572cfde941372620450d6c5c3e7f3
-rw-r--r--lang_id/features/char-ngram-feature.cc5
-rw-r--r--lang_id/features/char-ngram-feature.h6
-rw-r--r--lang_id/lang-id.h2
3 files changed, 12 insertions, 1 deletions
diff --git a/lang_id/features/char-ngram-feature.cc b/lang_id/features/char-ngram-feature.cc
index e52b2f2..83d7588 100644
--- a/lang_id/features/char-ngram-feature.cc
+++ b/lang_id/features/char-ngram-feature.cc
@@ -123,6 +123,11 @@ int ContinuousBagOfNgramsFunction::ComputeNgramCounts(
void ContinuousBagOfNgramsFunction::Evaluate(const WorkspaceSet &workspaces,
const LightSentence &sentence,
FeatureVector *result) const {
+ // NOTE: we use std::* constructs (instead of absl::Mutex & co) to simplify
+ // porting to Android and to avoid pulling in absl (which increases our code
+ // size).
+ std::lock_guard<std::mutex> mlock(state_mutex_);
+
// Find the char ngram counts.
int total_count = ComputeNgramCounts(sentence);
diff --git a/lang_id/features/char-ngram-feature.h b/lang_id/features/char-ngram-feature.h
index 8280bca..db0f83e 100644
--- a/lang_id/features/char-ngram-feature.h
+++ b/lang_id/features/char-ngram-feature.h
@@ -17,6 +17,7 @@
#ifndef NLP_SAFT_COMPONENTS_LANG_ID_MOBILE_FEATURES_CHAR_NGRAM_FEATURE_H_
#define NLP_SAFT_COMPONENTS_LANG_ID_MOBILE_FEATURES_CHAR_NGRAM_FEATURE_H_
+#include <mutex> // NOLINT: see comments for state_mutex_
#include <string>
#include "lang_id/common/fel/feature-extractor.h"
@@ -69,6 +70,11 @@ class ContinuousBagOfNgramsFunction : public LightSentenceFeature {
// below), and returns the total ngram count.
int ComputeNgramCounts(const LightSentence &sentence) const;
+ // Guards counts_ and non_zero_count_indices_. NOTE: we use std::* constructs
+ // (instead of absl::Mutex & co) to simplify porting to Android and to avoid
+ // pulling in absl (which increases our code size).
+ mutable std::mutex state_mutex_;
+
// counts_[i] is the count of all ngrams with id i. Work data for Evaluate().
// NOTE: we declare this vector as a field, such that its underlying capacity
// stays allocated in between calls to Evaluate().
diff --git a/lang_id/lang-id.h b/lang_id/lang-id.h
index 848ac6e..94af0c3 100644
--- a/lang_id/lang-id.h
+++ b/lang_id/lang-id.h
@@ -53,7 +53,7 @@ struct LangIdResult {
// Note: this class does not handle the details of loading the actual model.
// Those details have been "outsourced" to the ModelProvider class.
//
-// Note: this class is thread-unsafe.
+// This class is thread safe.
class LangId {
public:
// Standard BCP-47 language code for Unknown/Undetermined language.