pthread compatibility issue across platforms

This commit is contained in:
Marcin Junczys-Dowmunt 2012-08-05 09:18:33 +02:00
parent e01b8e26d3
commit af5ca0fb79
2 changed files with 15 additions and 19 deletions

View File

@ -146,26 +146,12 @@ PhraseDictionaryCompact::~PhraseDictionaryCompact() {
//TO_STRING_BODY(PhraseDictionaryCompact)
TargetPhraseCollection*
PhraseDictionaryCompact::RetrieveFromCache(const Phrase &sourcePhrase) {
#ifdef WITH_THREADS
boost::mutex::scoped_lock lock(m_sentenceMutex);
PhraseCache &ref = m_sentenceCache[pthread_self()];
#else
PhraseCache &ref = m_sentenceCache;
#endif
PhraseCache::iterator it = ref.find(sourcePhrase);
if(it != ref.end())
return it->second;
else
return NULL;
}
void PhraseDictionaryCompact::CacheForCleanup(const Phrase &sourcePhrase,
TargetPhraseCollection* tpc) {
#ifdef WITH_THREADS
boost::mutex::scoped_lock lock(m_sentenceMutex);
m_sentenceCache[pthread_self()].insert(std::make_pair(sourcePhrase, tpc));
size_t threadId = findThreadId(pthread_self());
m_sentenceCache[threadId].insert(std::make_pair(sourcePhrase, tpc));
#else
m_sentenceCache.insert(std::make_pair(sourcePhrase, tpc));
#endif
@ -184,7 +170,8 @@ void PhraseDictionaryCompact::CleanUp() {
#ifdef WITH_THREADS
boost::mutex::scoped_lock lock(m_sentenceMutex);
PhraseCache &ref = m_sentenceCache[pthread_self()];
size_t threadId = findThreadId(pthread_self());
PhraseCache &ref = m_sentenceCache[threadId];
#else
PhraseCache &ref = m_sentenceCache;
#endif

View File

@ -54,7 +54,17 @@ protected:
typedef std::map<Phrase, TargetPhraseCollection*> PhraseCache;
#ifdef WITH_THREADS
boost::mutex m_sentenceMutex;
typedef std::map<_opaque_pthread_t*, PhraseCache> SentenceCache;
std::vector<pthread_t> m_threadStore;
size_t findThreadId(pthread_t t) {
size_t size = m_threadStore.size();
for(size_t i = 0; i < size; i++)
if(pthread_equal(t, m_threadStore[i]))
return i;
return size;
}
typedef std::map<size_t, PhraseCache> SentenceCache;
#else
typedef PhraseCache SentenceCache;
#endif
@ -100,7 +110,6 @@ public:
void InitializeForInput(const Moses::InputType&);
TargetPhraseCollection* RetrieveFromCache(const Phrase &sourcePhrase);
void CacheForCleanup(const Phrase &source, TargetPhraseCollection* tpc);
void CleanUp();