Exemplo n.º 1
0
void* CPrefetchThreadOld::Main(void)
{
    do {
        TPrefetchQueue::TItemHandle handle = m_Queue.GetHandle();
        CRef<CPrefetchTokenOld_Impl> token = handle->GetRequest();
        {{
                CFastMutexGuard guard(m_Lock);
                if (m_Stop) {
                    return 0;
                }
                _ASSERT( handle );
                if ( token->IsEmpty() ) {
                    // Token may have been canceled
                    continue;
                }
            }
        }
        bool release_token = false;
        for (size_t i = 0; ; ++i) {
            {{
                    CFastMutexGuard guard(m_Lock);
                    if (m_Stop) {
                        return 0;
                    }
                }
            }
            CSeq_id_Handle id;
            token->m_TSESemaphore.Wait();
            {{
                    // m_Ids may be cleaned up by the token, check size
                    // on every iteration.
                    CFastMutexGuard guard(token->m_Lock);
                    i = max(i, token->m_CurrentId);
                    if (i >= token->m_Ids.size()) {
                        // Can not release token now - mutex is still locked
                        release_token = true;
                        break;
                    }
                    id = token->m_Ids[i];
                }
            }
            try {
                SSeqMatch_DS match = m_DataSource.BestResolve(id);
                if ( match ) {
                    token->AddResolvedId(i, match.m_TSE_Lock);
                }
            } catch ( exception& ) {
                // BestResolve() failed, go to the next id.
            }
        }
        if (release_token) {
            token.Reset();
        }
    } while (true);
    return 0;
}