Fixed threading issue in Onigura Regex library

This commit is contained in:
Matt Hill
2015-05-04 22:59:28 -04:00
parent fc45861989
commit 5b19ad6f4d
2 changed files with 12 additions and 4 deletions

View File

@@ -20,14 +20,17 @@
#include "regexrule.h" #include "regexrule.h"
using namespace std; using namespace std;
tthread::mutex regexrule_mutex_m;
namespace alpr namespace alpr
{ {
RegexRule::RegexRule(string region, string pattern) RegexRule::RegexRule(string region, string pattern)
{ {
this->original = pattern; this->original = pattern;
this->region = region; this->region = region;
this->regex = "";
this->valid = false; this->valid = false;
string::iterator end_it = utf8::find_invalid(pattern.begin(), pattern.end()); string::iterator end_it = utf8::find_invalid(pattern.begin(), pattern.end());
@@ -90,12 +93,16 @@ namespace alpr
numchars++; numchars++;
} }
// Onigurama is not thread safe when compiling regex. Using a mutex to ensure that
// we don't crash
regexrule_mutex_m.lock();
UChar* cstr_pattern = (UChar* )this->regex.c_str(); UChar* cstr_pattern = (UChar* )this->regex.c_str();
OnigErrorInfo einfo; OnigErrorInfo einfo;
//cout << "Pattern: " << cstr_pattern << endl;
int r = onig_new(&onig_regex, cstr_pattern, cstr_pattern + strlen((char* )cstr_pattern), int r = onig_new(&onig_regex, cstr_pattern, cstr_pattern + strlen((char* )cstr_pattern),
ONIG_OPTION_DEFAULT, ONIG_ENCODING_UTF8, ONIG_SYNTAX_DEFAULT, &einfo); ONIG_OPTION_DEFAULT, ONIG_ENCODING_UTF8, ONIG_SYNTAX_DEFAULT, &einfo);
regexrule_mutex_m.unlock();
if (r != ONIG_NORMAL) { if (r != ONIG_NORMAL) {
//char s[ONIG_MAX_ERROR_MESSAGE_LEN]; //char s[ONIG_MAX_ERROR_MESSAGE_LEN];

View File

@@ -26,6 +26,7 @@
#include <vector> #include <vector>
#include "support/regex/oniguruma.h" #include "support/regex/oniguruma.h"
#include "support/utf8.h" #include "support/utf8.h"
#include "support/tinythread.h"
namespace alpr namespace alpr
{ {