Use only one lock to acces zim library.

Search can now use reader. So there is no need to have to different locks.
This commit is contained in:
Matthieu Gautier 2017-07-12 18:45:12 +02:00
parent 08f2373360
commit 4b9dc39c49

View File

@ -92,10 +92,9 @@ static bool verboseFlag = false;
static std::map<std::string, std::string> extMimeTypes;
static std::map<std::string, kiwix::Reader*> readers;
static std::map<std::string, kiwix::Searcher*> searchers;
static pthread_mutex_t readerLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t zimLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mapLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t welcomeLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t searcherLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t compressorLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t verboseFlagLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mimeTypeLock = PTHREAD_MUTEX_INITIALIZER;
@ -259,31 +258,31 @@ ssize_t callback_reader_from_blob(void* cls,
size_t max)
{
zim::Blob* blob = static_cast<zim::Blob*>(cls);
pthread_mutex_lock(&readerLock);
pthread_mutex_lock(&zimLock);
size_t max_size_to_set = min<size_t>(max, blob->size() - pos);
if (max_size_to_set <= 0) {
pthread_mutex_unlock(&readerLock);
pthread_mutex_unlock(&zimLock);
return MHD_CONTENT_READER_END_WITH_ERROR;
}
memcpy(buf, blob->data() + pos, max_size_to_set);
pthread_mutex_unlock(&readerLock);
pthread_mutex_unlock(&zimLock);
return max_size_to_set;
}
void callback_free_blob(void* cls)
{
zim::Blob* blob = static_cast<zim::Blob*>(cls);
pthread_mutex_lock(&readerLock);
pthread_mutex_lock(&zimLock);
delete blob;
pthread_mutex_unlock(&readerLock);
pthread_mutex_unlock(&zimLock);
}
static struct MHD_Response* build_callback_response_from_blob(
zim::Blob& blob, const std::string& mimeType)
{
pthread_mutex_lock(&readerLock);
pthread_mutex_lock(&zimLock);
zim::Blob* p_blob = new zim::Blob(blob);
struct MHD_Response* response
= MHD_create_response_from_callback(blob.size(),
@ -291,7 +290,7 @@ static struct MHD_Response* build_callback_response_from_blob(
callback_reader_from_blob,
p_blob,
callback_free_blob);
pthread_mutex_unlock(&readerLock);
pthread_mutex_unlock(&zimLock);
/* Tell the client that byte ranges are accepted */
MHD_add_response_header(response, MHD_HTTP_HEADER_ACCEPT_RANGES, "bytes");
@ -333,6 +332,7 @@ static struct MHD_Response* handle_suggest(
std::cout << "Searching suggestions for: \"" << term << "\"" << endl;
}
pthread_mutex_lock(&zimLock);
/* Get the suggestions */
content = "[";
reader->searchSuggestionsSmart(term, maxSuggestionCount);
@ -343,6 +343,7 @@ static struct MHD_Response* handle_suggest(
+ "\"}";
suggestionCount++;
}
pthread_mutex_unlock(&zimLock);
/* Propose the fulltext search if possible */
if (searcher != NULL) {
@ -399,12 +400,12 @@ static struct MHD_Response* handle_search(
std::vector<std::string> variants = reader->getTitleVariants(patternString);
std::vector<std::string>::iterator variantsItr = variants.begin();
pthread_mutex_lock(&readerLock);
pthread_mutex_lock(&zimLock);
while (patternCorrespondingUrl.empty() && variantsItr != variants.end()) {
reader->getPageUrlFromTitle(*variantsItr, patternCorrespondingUrl);
variantsItr++;
}
pthread_mutex_unlock(&readerLock);
pthread_mutex_unlock(&zimLock);
/* If article found then redirect directly to it */
if (!patternCorrespondingUrl.empty()) {
@ -425,16 +426,14 @@ static struct MHD_Response* handle_search(
unsigned int endNumber = end != NULL ? atoi(end) : 25;
/* Get the results */
pthread_mutex_lock(&searcherLock);
pthread_mutex_lock(&readerLock);
pthread_mutex_lock(&zimLock);
try {
searcher->search(patternString, startNumber, endNumber, isVerbose());
content = searcher->getHtml();
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
pthread_mutex_unlock(&readerLock);
pthread_mutex_unlock(&searcherLock);
pthread_mutex_unlock(&zimLock);
} else {
content = "<!DOCTYPE html>\n<html><head><meta content=\"text/html;charset=UTF-8\" http-equiv=\"content-type\" /><title>Fulltext search unavailable</title></head><body><h1>Not Found</h1><p>There is no article with the title <b>\"" + kiwix::encodeDiples(patternString) + "\"</b> and the fulltext search engine is not available for this content.</p></body></html>";
httpResponseCode = MHD_HTTP_NOT_FOUND;
@ -465,9 +464,9 @@ static struct MHD_Response* handle_random(
std::string httpRedirection;
httpResponseCode = MHD_HTTP_FOUND;
if (reader != NULL) {
pthread_mutex_lock(&readerLock);
pthread_mutex_lock(&zimLock);
std::string randomUrl = reader->getRandomPageUrl();
pthread_mutex_unlock(&readerLock);
pthread_mutex_unlock(&zimLock);
httpRedirection
= "/" + humanReadableBookId + "/" + kiwix::urlEncode(randomUrl);
}
@ -489,7 +488,7 @@ static struct MHD_Response* handle_content(
bool found = false;
zim::Article article;
pthread_mutex_lock(&readerLock);
pthread_mutex_lock(&zimLock);
try {
found = reader->getArticleObjectByDecodedUrl(urlStr, article);
@ -508,7 +507,7 @@ static struct MHD_Response* handle_content(
std::cerr << e.what() << std::endl;
found = false;
}
pthread_mutex_unlock(&readerLock);
pthread_mutex_unlock(&zimLock);
if (!found) {
if (isVerbose())
@ -530,9 +529,9 @@ static struct MHD_Response* handle_content(
}
try {
pthread_mutex_lock(&readerLock);
pthread_mutex_lock(&zimLock);
mimeType = article.getMimeType();
pthread_mutex_unlock(&readerLock);
pthread_mutex_unlock(&zimLock);
} catch (exception& e) {
mimeType = "application/octet-stream";
}
@ -542,16 +541,16 @@ static struct MHD_Response* handle_content(
cout << "mimeType: " << mimeType << endl;
}
pthread_mutex_lock(&readerLock);
pthread_mutex_lock(&zimLock);
zim::Blob raw_content = article.getData();
pthread_mutex_unlock(&readerLock);
pthread_mutex_unlock(&zimLock);
if (mimeType.find("text/") != string::npos
|| mimeType.find("application/javascript") != string::npos
|| mimeType.find("application/json") != string::npos) {
pthread_mutex_lock(&readerLock);
pthread_mutex_lock(&zimLock);
content = string(raw_content.data(), raw_content.size());
pthread_mutex_unlock(&readerLock);
pthread_mutex_unlock(&zimLock);
/* Special rewrite URL in case of ZIM file use intern *asbolute* url like
* /A/Kiwix */
@ -1001,10 +1000,9 @@ int main(int argc, char** argv)
#endif
/* Mutex init */
pthread_mutex_init(&readerLock, NULL);
pthread_mutex_init(&zimLock, NULL);
pthread_mutex_init(&mapLock, NULL);
pthread_mutex_init(&welcomeLock, NULL);
pthread_mutex_init(&searcherLock, NULL);
pthread_mutex_init(&compressorLock, NULL);
pthread_mutex_init(&verboseFlagLock, NULL);
pthread_mutex_init(&mimeTypeLock, NULL);
@ -1147,8 +1145,7 @@ int main(int argc, char** argv)
MHD_stop_daemon(daemon);
/* Mutex destroy */
pthread_mutex_destroy(&readerLock);
pthread_mutex_destroy(&searcherLock);
pthread_mutex_destroy(&zimLock);
pthread_mutex_destroy(&compressorLock);
pthread_mutex_destroy(&mapLock);
pthread_mutex_destroy(&welcomeLock);