The working (hashing) thread FileIndexMonitor is now stopped when RetroShare is closed.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@4072 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2011-03-03 23:30:08 +00:00
parent 6bb4854925
commit 0a42b7899e
10 changed files with 67 additions and 31 deletions

View File

@ -453,13 +453,13 @@ void FileIndexMonitor::run()
{ {
updateCycle(); updateCycle();
while(m_bRun) while(isRunning())
{ {
for(int i = 0; i < updatePeriod; i++) for(int i = 0; i < updatePeriod; i++)
{ {
if (m_bRun == false) { if (isRunning() == false) {
return; return;
} }
@ -515,7 +515,7 @@ void FileIndexMonitor::updateCycle()
cache_is_new = useHashCache && hashCache.empty() ; cache_is_new = useHashCache && hashCache.empty() ;
} }
while(moretodo) while(isRunning() && moretodo)
{ {
/* sleep a bit for each loop */ /* sleep a bit for each loop */
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ /********************************** WINDOWS/UNIX SPECIFIC PART ******************/
@ -571,8 +571,8 @@ void FileIndexMonitor::updateCycle()
#endif #endif
/* check for the dir existance */ /* check for the dir existance */
librs::util::FolderIterator dirIt(realpath); librs::util::FolderIterator dirIt(realpath);
if (!dirIt.isValid()) if (!dirIt.isValid())
{ {
#ifdef FIM_DEBUG #ifdef FIM_DEBUG
std::cerr << "FileIndexMonitor::updateCycle()"; std::cerr << "FileIndexMonitor::updateCycle()";
@ -617,20 +617,20 @@ void FileIndexMonitor::updateCycle()
to_hash.back().realpath = realpath ; to_hash.back().realpath = realpath ;
to_hash.back().dirpath = dirpath ; to_hash.back().dirpath = dirpath ;
while(dirIt.readdir()) while(isRunning() && dirIt.readdir())
{ {
/* check entry type */ /* check entry type */
std::string fname; std::string fname;
dirIt.d_name(fname); dirIt.d_name(fname);
std::string fullname = realpath + "/" + fname; std::string fullname = realpath + "/" + fname;
#ifdef FIM_DEBUG #ifdef FIM_DEBUG
std::cerr << "calling stats on " << fullname <<std::endl; std::cerr << "calling stats on " << fullname <<std::endl;
#endif #endif
#ifdef WINDOWS_SYS #ifdef WINDOWS_SYS
std::wstring wfullname; std::wstring wfullname;
librs::util::ConvertUtf8ToUtf16(fullname, wfullname); librs::util::ConvertUtf8ToUtf16(fullname, wfullname);
if (-1 != _wstati64(wfullname.c_str(), &buf)) if (-1 != _wstati64(wfullname.c_str(), &buf))
#else #else
if (-1 != stat64(fullname.c_str(), &buf)) if (-1 != stat64(fullname.c_str(), &buf))
#endif #endif
@ -738,7 +738,7 @@ void FileIndexMonitor::updateCycle()
// Now, hash all files at once. // Now, hash all files at once.
// //
if(!to_hash.empty()) if(isRunning() && !to_hash.empty())
hashFiles(to_hash) ; hashFiles(to_hash) ;
cb->notifyHashingInfo(NOTIFY_HASHTYPE_FINISH, "") ; cb->notifyHashingInfo(NOTIFY_HASHTYPE_FINISH, "") ;
@ -827,9 +827,12 @@ void FileIndexMonitor::hashFiles(const std::vector<DirContentToHash>& to_hash)
uint64_t hashed_size=0 ; uint64_t hashed_size=0 ;
uint64_t last_save_size=0 ; uint64_t last_save_size=0 ;
// check if thread is running
bool running = isRunning();
/* update files */ /* update files */
for(uint32_t i=0;i<to_hash.size();++i) for(uint32_t i=0;running && i<to_hash.size();++i)
for(uint32_t j=0;j<to_hash[i].fentries.size();++j,++cnt) for(uint32_t j=0;running && j<to_hash[i].fentries.size();++j,++cnt)
{ {
// This is a very basic progress notification. To be more complete and user friendly, one would // This is a very basic progress notification. To be more complete and user friendly, one would
// rather send a completion ratio based on the size of files vs/ total size. // rather send a completion ratio based on the size of files vs/ total size.
@ -846,7 +849,7 @@ void FileIndexMonitor::hashFiles(const std::vector<DirContentToHash>& to_hash)
// //
if(useHashCache && hashCache.find(real_path,fe.size,fe.modtime,fe.hash)) if(useHashCache && hashCache.find(real_path,fe.size,fe.modtime,fe.hash))
fi.updateFileEntry(to_hash[i].dirpath,fe,stamp); fi.updateFileEntry(to_hash[i].dirpath,fe,stamp);
else if(RsDirUtil::getFileHash(real_path, fe.hash,fe.size)) // not found, then hash it. else if(RsDirUtil::getFileHash(real_path, fe.hash,fe.size, this)) // not found, then hash it.
{ {
RsStackMutex stack(fiMutex); /**** LOCKED DIRS ****/ RsStackMutex stack(fiMutex); /**** LOCKED DIRS ****/
@ -893,6 +896,9 @@ void FileIndexMonitor::hashFiles(const std::vector<DirContentToHash>& to_hash)
if(useHashCache) if(useHashCache)
hashCache.save() ; hashCache.save() ;
} }
// check if thread is running
running = isRunning();
} }
cb->notifyListChange(NOTIFY_LIST_DIRLIST_LOCAL, 0); cb->notifyListChange(NOTIFY_LIST_DIRLIST_LOCAL, 0);

View File

@ -209,7 +209,7 @@ void ftController::run()
/* check the queues */ /* check the queues */
uint32_t cnt = 0 ; uint32_t cnt = 0 ;
while(m_bRun) while(isRunning())
{ {
#ifdef WIN32 #ifdef WIN32
Sleep(1000); Sleep(1000);

View File

@ -49,7 +49,7 @@ void ftExtraList::run()
time_t cleanup = 0; time_t cleanup = 0;
time_t now = 0; time_t now = 0;
while (m_bRun) while (isRunning())
{ {
#ifdef DEBUG_ELIST #ifdef DEBUG_ELIST
//std::cerr << "ftExtraList::run() Iteration"; //std::cerr << "ftExtraList::run() Iteration";

View File

@ -225,7 +225,7 @@ CacheTransfer *ftServer::getCacheTransfer()
void ftServer::run() void ftServer::run()
{ {
while(m_bRun) while(isRunning())
{ {
mFtDataplex->deleteUnusedServers() ; mFtDataplex->deleteUnusedServers() ;
#ifdef WIN32 #ifdef WIN32

View File

@ -358,7 +358,7 @@ void AuthGPGimpl::run()
{ {
int count = 0; int count = 0;
while (m_bRun) while (isRunning())
{ {
#ifdef WIN32 #ifdef WIN32
Sleep(100); Sleep(100);

View File

@ -110,7 +110,7 @@ void RsServer::run()
int min = 0; int min = 0;
int loop = 0; int loop = 0;
while(m_bRun) while(isRunning())
{ {
#ifndef WINDOWS_SYS #ifndef WINDOWS_SYS
usleep((int) (timeDelta * 1000000)); usleep((int) (timeDelta * 1000000));

View File

@ -32,6 +32,7 @@
#include "util/rsdir.h" #include "util/rsdir.h"
#include "pqi/pqinotify.h" #include "pqi/pqinotify.h"
#include "retroshare/rstypes.h" #include "retroshare/rstypes.h"
#include "rsthreads.h"
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
@ -526,7 +527,7 @@ bool RsDirUtil::hashFile(const std::string& filepath,
#include <iomanip> #include <iomanip>
/* Function to hash, and get details of a file */ /* Function to hash, and get details of a file */
bool RsDirUtil::getFileHash(const std::string& filepath, std::string &hash, uint64_t &size) bool RsDirUtil::getFileHash(const std::string& filepath, std::string &hash, uint64_t &size, RsThread *thread /*= NULL*/)
{ {
FILE *fd; FILE *fd;
int len; int len;
@ -549,14 +550,32 @@ bool RsDirUtil::getFileHash(const std::string& filepath, std::string &hash, uint
size = ftello64(fd); size = ftello64(fd);
fseeko64(fd, 0, SEEK_SET); fseeko64(fd, 0, SEEK_SET);
/* check if thread is running */
bool isRunning = thread ? thread->isRunning() : true;
int runningCheckCount = 0;
SHA1_Init(sha_ctx); SHA1_Init(sha_ctx);
while((len = fread(gblBuf,1, 512, fd)) > 0) while(isRunning && (len = fread(gblBuf,1, 512, fd)) > 0)
{ {
SHA1_Update(sha_ctx, gblBuf, len); SHA1_Update(sha_ctx, gblBuf, len);
if (thread && ++runningCheckCount > (10 * 1024)) {
/* check all 50MB if thread is running */
isRunning = thread->isRunning();
runningCheckCount = 0;
}
}
/* Thread has ended */
if (isRunning == false)
{
delete sha_ctx;
fclose(fd);
return false;
} }
/* reading failed for some reason */ /* reading failed for some reason */
if (ferror(fd)) if (ferror(fd))
{ {
delete sha_ctx; delete sha_ctx;
fclose(fd); fclose(fd);
@ -565,7 +584,7 @@ bool RsDirUtil::getFileHash(const std::string& filepath, std::string &hash, uint
SHA1_Final(&sha_buf[0], sha_ctx); SHA1_Final(&sha_buf[0], sha_ctx);
std::ostringstream tmpout; std::ostringstream tmpout;
for(int i = 0; i < SHA_DIGEST_LENGTH; i++) for(int i = 0; i < SHA_DIGEST_LENGTH; i++)
{ {
tmpout << std::setw(2) << std::setfill('0') << std::hex << (unsigned int) (sha_buf[i]); tmpout << std::setw(2) << std::setfill('0') << std::hex << (unsigned int) (sha_buf[i]);

View File

@ -33,6 +33,7 @@
#include <stdint.h> #include <stdint.h>
class CRC32Map ; class CRC32Map ;
class RsThread;
namespace RsDirUtil { namespace RsDirUtil {
@ -64,7 +65,7 @@ bool checkCreateDirectory(const std::string& dir);
bool cleanupDirectory(const std::string& dir, const std::list<std::string> &keepFiles); bool cleanupDirectory(const std::string& dir, const std::list<std::string> &keepFiles);
bool hashFile(const std::string& filepath, std::string &name, std::string &hash, uint64_t &size); bool hashFile(const std::string& filepath, std::string &name, std::string &hash, uint64_t &size);
bool getFileHash(const std::string& filepath,std::string &hash, uint64_t &size); bool getFileHash(const std::string& filepath,std::string &hash, uint64_t &size, RsThread *thread = NULL);
std::wstring getWideTopDir(std::wstring); std::wstring getWideTopDir(std::wstring);

View File

@ -82,7 +82,7 @@ pthread_t createThread(RsThread &thread)
RsThread::RsThread () RsThread::RsThread ()
{ {
m_bRun = true; mIsRunning = true;
#ifdef WINDOWS_SYS #ifdef WINDOWS_SYS
memset (&mTid, 0, sizeof(mTid)); memset (&mTid, 0, sizeof(mTid));
@ -93,7 +93,8 @@ RsThread::RsThread ()
void RsThread::join() /* waits for the the mTid thread to stop */ void RsThread::join() /* waits for the the mTid thread to stop */
{ {
m_bRun = false; // do we need a mutex for this ?
mIsRunning = false;
void *ptr; void *ptr;
pthread_join(mTid, &ptr); pthread_join(mTid, &ptr);
@ -104,6 +105,12 @@ void RsThread::stop()
pthread_exit(NULL); pthread_exit(NULL);
} }
bool RsThread::isRunning()
{
// do we need a mutex for this ?
return mIsRunning;
}
RsQueueThread::RsQueueThread(uint32_t min, uint32_t max, double relaxFactor ) RsQueueThread::RsQueueThread(uint32_t min, uint32_t max, double relaxFactor )
:mMinSleep(min), mMaxSleep(max), mRelaxFactor(relaxFactor) :mMinSleep(min), mMaxSleep(max), mRelaxFactor(relaxFactor)
{ {
@ -113,7 +120,7 @@ RsQueueThread::RsQueueThread(uint32_t min, uint32_t max, double relaxFactor )
void RsQueueThread::run() void RsQueueThread::run()
{ {
while(m_bRun) while(isRunning())
{ {
bool doneWork = false; bool doneWork = false;
while(workQueued() && doWork()) while(workQueued() && doWork())

View File

@ -118,10 +118,13 @@ virtual void run() = 0; /* called once the thread is started */
virtual void join(); /* waits for the the mTid thread to stop */ virtual void join(); /* waits for the the mTid thread to stop */
virtual void stop(); /* calls pthread_exit() */ virtual void stop(); /* calls pthread_exit() */
bool isRunning();
pthread_t mTid; pthread_t mTid;
RsMutex mMutex; RsMutex mMutex;
protected:
bool m_bRun; private:
bool mIsRunning;
}; };