mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-03 06:35:08 -04:00
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:
parent
6bb4854925
commit
0a42b7899e
10 changed files with 67 additions and 31 deletions
|
@ -32,6 +32,7 @@
|
|||
#include "util/rsdir.h"
|
||||
#include "pqi/pqinotify.h"
|
||||
#include "retroshare/rstypes.h"
|
||||
#include "rsthreads.h"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
@ -526,7 +527,7 @@ bool RsDirUtil::hashFile(const std::string& filepath,
|
|||
#include <iomanip>
|
||||
|
||||
/* 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;
|
||||
int len;
|
||||
|
@ -549,14 +550,32 @@ bool RsDirUtil::getFileHash(const std::string& filepath, std::string &hash, uint
|
|||
size = ftello64(fd);
|
||||
fseeko64(fd, 0, SEEK_SET);
|
||||
|
||||
/* check if thread is running */
|
||||
bool isRunning = thread ? thread->isRunning() : true;
|
||||
int runningCheckCount = 0;
|
||||
|
||||
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);
|
||||
|
||||
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 */
|
||||
if (ferror(fd))
|
||||
if (ferror(fd))
|
||||
{
|
||||
delete sha_ctx;
|
||||
fclose(fd);
|
||||
|
@ -565,7 +584,7 @@ bool RsDirUtil::getFileHash(const std::string& filepath, std::string &hash, uint
|
|||
|
||||
SHA1_Final(&sha_buf[0], sha_ctx);
|
||||
|
||||
std::ostringstream tmpout;
|
||||
std::ostringstream tmpout;
|
||||
for(int i = 0; i < SHA_DIGEST_LENGTH; i++)
|
||||
{
|
||||
tmpout << std::setw(2) << std::setfill('0') << std::hex << (unsigned int) (sha_buf[i]);
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
#include <stdint.h>
|
||||
|
||||
class CRC32Map ;
|
||||
class RsThread;
|
||||
|
||||
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 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);
|
||||
|
|
|
@ -82,7 +82,7 @@ pthread_t createThread(RsThread &thread)
|
|||
|
||||
RsThread::RsThread ()
|
||||
{
|
||||
m_bRun = true;
|
||||
mIsRunning = true;
|
||||
|
||||
#ifdef WINDOWS_SYS
|
||||
memset (&mTid, 0, sizeof(mTid));
|
||||
|
@ -93,7 +93,8 @@ RsThread::RsThread ()
|
|||
|
||||
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;
|
||||
pthread_join(mTid, &ptr);
|
||||
|
@ -104,6 +105,12 @@ void RsThread::stop()
|
|||
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 )
|
||||
:mMinSleep(min), mMaxSleep(max), mRelaxFactor(relaxFactor)
|
||||
{
|
||||
|
@ -113,7 +120,7 @@ RsQueueThread::RsQueueThread(uint32_t min, uint32_t max, double relaxFactor )
|
|||
|
||||
void RsQueueThread::run()
|
||||
{
|
||||
while(m_bRun)
|
||||
while(isRunning())
|
||||
{
|
||||
bool doneWork = false;
|
||||
while(workQueued() && doWork())
|
||||
|
|
|
@ -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 stop(); /* calls pthread_exit() */
|
||||
|
||||
bool isRunning();
|
||||
|
||||
pthread_t mTid;
|
||||
RsMutex mMutex;
|
||||
protected:
|
||||
bool m_bRun;
|
||||
RsMutex mMutex;
|
||||
|
||||
private:
|
||||
bool mIsRunning;
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue