mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-03 14:45:12 -04:00
created 2 subclasses of RsThread, one for ticking services, and one for single shot jobs. Now all threads use the same base code.
git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@8288 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
f2d4a237ca
commit
e9b9dce9f5
28 changed files with 317 additions and 335 deletions
|
@ -71,7 +71,7 @@ void RsServer::ConfigFinalSave()
|
|||
mConfigMgr->completeConfiguration();
|
||||
}
|
||||
|
||||
void RsServer::startServiceThread(RsThread *t)
|
||||
void RsServer::startServiceThread(RsTickingThread *t)
|
||||
{
|
||||
t->start() ;
|
||||
mRegisteredServiceThreads.push_back(t) ;
|
||||
|
@ -87,13 +87,13 @@ void RsServer::rsGlobalShutDown()
|
|||
|
||||
mNetMgr->shutdown(); /* Handles UPnP */
|
||||
|
||||
join();
|
||||
fullstop() ;
|
||||
|
||||
// kill all registered service threads
|
||||
// kill all registered service threads
|
||||
|
||||
for(std::list<RsThread*>::iterator it= mRegisteredServiceThreads.begin();it!=mRegisteredServiceThreads.end();++it)
|
||||
for(std::list<RsTickingThread*>::iterator it= mRegisteredServiceThreads.begin();it!=mRegisteredServiceThreads.end();++it)
|
||||
{
|
||||
(*it)->join() ;
|
||||
(*it)->fullstop() ;
|
||||
}
|
||||
// #ifdef RS_ENABLE_GXS
|
||||
// // We should automate this.
|
||||
|
|
|
@ -49,6 +49,25 @@ int rsserverzone = 101;
|
|||
****/
|
||||
|
||||
#define WARN_BIG_CYCLE_TIME (0.2)
|
||||
#ifdef WINDOWS_SYS
|
||||
#include <time.h>
|
||||
#include <sys/timeb.h>
|
||||
#endif
|
||||
|
||||
static double getCurrentTS()
|
||||
{
|
||||
|
||||
#ifndef WINDOWS_SYS
|
||||
struct timeval cts_tmp;
|
||||
gettimeofday(&cts_tmp, NULL);
|
||||
double cts = (cts_tmp.tv_sec) + ((double) cts_tmp.tv_usec) / 1000000.0;
|
||||
#else
|
||||
struct _timeb timebuf;
|
||||
_ftime( &timebuf);
|
||||
double cts = (timebuf.time) + ((double) timebuf.millitm) / 1000.0;
|
||||
#endif
|
||||
return cts;
|
||||
}
|
||||
|
||||
|
||||
RsServer::RsServer()
|
||||
|
@ -74,22 +93,20 @@ RsServer::RsServer()
|
|||
chatSrv = NULL;
|
||||
mStatusSrv = NULL;
|
||||
|
||||
/* caches (that need ticking) */
|
||||
mMin = 0;
|
||||
mLoop = 0;
|
||||
|
||||
mAvgTickRate = mTimeDelta;
|
||||
|
||||
mLastts = getCurrentTS();
|
||||
mLastSec = 0; /* for the slower ticked stuff */
|
||||
mTimeDelta = 0.25 ;
|
||||
|
||||
/* caches (that need ticking) */
|
||||
|
||||
/* Config */
|
||||
mConfigMgr = NULL;
|
||||
mGeneralConfig = NULL;
|
||||
|
||||
/* GXS - Amazingly we can still initialise these
|
||||
* even without knowing the data-types (they are just pointers???)
|
||||
*/
|
||||
// mPhoto = NULL;
|
||||
// mWiki = NULL;
|
||||
// mPosted = NULL;
|
||||
// mGxsCircles = NULL;
|
||||
// mGxsIdService = NULL;
|
||||
// mGxsForums = NULL;
|
||||
// mWire = NULL;
|
||||
}
|
||||
|
||||
RsServer::~RsServer()
|
||||
|
@ -101,182 +118,143 @@ RsServer::~RsServer()
|
|||
----> MUST BE LOCKED!
|
||||
*/
|
||||
|
||||
#ifdef WINDOWS_SYS
|
||||
#include <time.h>
|
||||
#include <sys/timeb.h>
|
||||
#endif
|
||||
|
||||
static double getCurrentTS()
|
||||
{
|
||||
|
||||
#ifndef WINDOWS_SYS
|
||||
struct timeval cts_tmp;
|
||||
gettimeofday(&cts_tmp, NULL);
|
||||
double cts = (cts_tmp.tv_sec) + ((double) cts_tmp.tv_usec) / 1000000.0;
|
||||
#else
|
||||
struct _timeb timebuf;
|
||||
_ftime( &timebuf);
|
||||
double cts = (timebuf.time) + ((double) timebuf.millitm) / 1000.0;
|
||||
#endif
|
||||
return cts;
|
||||
}
|
||||
|
||||
|
||||
/* Thread Fn: Run the Core */
|
||||
void RsServer::run()
|
||||
void RsServer::data_tick()
|
||||
{
|
||||
|
||||
double timeDelta = 0.25;
|
||||
double minTimeDelta = 0.1; // 25;
|
||||
double maxTimeDelta = 0.5;
|
||||
double kickLimit = 0.15;
|
||||
|
||||
double avgTickRate = timeDelta;
|
||||
|
||||
double lastts, ts;
|
||||
lastts = ts = getCurrentTS();
|
||||
|
||||
long lastSec = 0; /* for the slower ticked stuff */
|
||||
|
||||
int min = 0;
|
||||
int loop = 0;
|
||||
|
||||
while(isRunning())
|
||||
{
|
||||
#ifndef WINDOWS_SYS
|
||||
usleep((int) (timeDelta * 1000000));
|
||||
usleep((int) (mTimeDelta * 1000000));
|
||||
#else
|
||||
Sleep((int) (timeDelta * 1000));
|
||||
Sleep((int) (mTimeDelta * 1000));
|
||||
#endif
|
||||
|
||||
ts = getCurrentTS();
|
||||
double delta = ts - lastts;
|
||||
double ts = getCurrentTS();
|
||||
double delta = ts - mLastts;
|
||||
|
||||
/* for the fast ticked stuff */
|
||||
if (delta > timeDelta)
|
||||
{
|
||||
/* for the fast ticked stuff */
|
||||
if (delta > mTimeDelta)
|
||||
{
|
||||
#ifdef DEBUG_TICK
|
||||
std::cerr << "Delta: " << delta << std::endl;
|
||||
std::cerr << "Time Delta: " << timeDelta << std::endl;
|
||||
std::cerr << "Avg Tick Rate: " << avgTickRate << std::endl;
|
||||
std::cerr << "Delta: " << delta << std::endl;
|
||||
std::cerr << "Time Delta: " << mTimeDelta << std::endl;
|
||||
std::cerr << "Avg Tick Rate: " << mAvgTickRate << std::endl;
|
||||
#endif
|
||||
|
||||
lastts = ts;
|
||||
mLastts = ts;
|
||||
|
||||
/******************************** RUN SERVER *****************/
|
||||
lockRsCore();
|
||||
/******************************** RUN SERVER *****************/
|
||||
lockRsCore();
|
||||
|
||||
int moreToTick = pqih->tick();
|
||||
int moreToTick = pqih->tick();
|
||||
|
||||
#ifdef DEBUG_TICK
|
||||
std::cerr << "RsServer::run() ftserver->tick(): moreToTick: " << moreToTick << std::endl;
|
||||
std::cerr << "RsServer::run() ftserver->tick(): moreToTick: " << moreToTick << std::endl;
|
||||
#endif
|
||||
|
||||
unlockRsCore();
|
||||
unlockRsCore();
|
||||
|
||||
/* tick the Managers */
|
||||
mPeerMgr->tick();
|
||||
mLinkMgr->tick();
|
||||
mNetMgr->tick();
|
||||
/******************************** RUN SERVER *****************/
|
||||
/* tick the Managers */
|
||||
mPeerMgr->tick();
|
||||
mLinkMgr->tick();
|
||||
mNetMgr->tick();
|
||||
/******************************** RUN SERVER *****************/
|
||||
|
||||
/* adjust tick rate depending on whether there is more.
|
||||
*/
|
||||
/* adjust tick rate depending on whether there is more.
|
||||
*/
|
||||
|
||||
avgTickRate = 0.2 * timeDelta + 0.8 * avgTickRate;
|
||||
mAvgTickRate = 0.2 * mTimeDelta + 0.8 * mAvgTickRate;
|
||||
|
||||
if (1 == moreToTick)
|
||||
{
|
||||
timeDelta = 0.9 * avgTickRate;
|
||||
if (timeDelta > kickLimit)
|
||||
{
|
||||
/* force next tick in one sec
|
||||
* if we are reading data.
|
||||
*/
|
||||
timeDelta = kickLimit;
|
||||
avgTickRate = kickLimit;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
timeDelta = 1.1 * avgTickRate;
|
||||
}
|
||||
if (1 == moreToTick)
|
||||
{
|
||||
mTimeDelta = 0.9 * mAvgTickRate;
|
||||
if (mTimeDelta > kickLimit)
|
||||
{
|
||||
/* force next tick in one sec
|
||||
* if we are reading data.
|
||||
*/
|
||||
mTimeDelta = kickLimit;
|
||||
mAvgTickRate = kickLimit;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mTimeDelta = 1.1 * mAvgTickRate;
|
||||
}
|
||||
|
||||
/* limiter */
|
||||
if (timeDelta < minTimeDelta)
|
||||
{
|
||||
timeDelta = minTimeDelta;
|
||||
}
|
||||
else if (timeDelta > maxTimeDelta)
|
||||
{
|
||||
timeDelta = maxTimeDelta;
|
||||
}
|
||||
/* limiter */
|
||||
if (mTimeDelta < minTimeDelta)
|
||||
{
|
||||
mTimeDelta = minTimeDelta;
|
||||
}
|
||||
else if (mTimeDelta > maxTimeDelta)
|
||||
{
|
||||
mTimeDelta = maxTimeDelta;
|
||||
}
|
||||
|
||||
/* Fast Updates */
|
||||
/* Fast Updates */
|
||||
|
||||
|
||||
/* now we have the slow ticking stuff */
|
||||
/* stuff ticked once a second (but can be slowed down) */
|
||||
if ((int) ts > lastSec)
|
||||
{
|
||||
lastSec = (int) ts;
|
||||
/* now we have the slow ticking stuff */
|
||||
/* stuff ticked once a second (but can be slowed down) */
|
||||
if ((int) ts > mLastSec)
|
||||
{
|
||||
mLastSec = (int) ts;
|
||||
|
||||
// Every second! (UDP keepalive).
|
||||
//tou_tick_stunkeepalive();
|
||||
// Every second! (UDP keepalive).
|
||||
//tou_tick_stunkeepalive();
|
||||
|
||||
// every five loops (> 5 secs)
|
||||
if (loop % 5 == 0)
|
||||
{
|
||||
// update_quick_stats();
|
||||
// every five loops (> 5 secs)
|
||||
if (mLoop % 5 == 0)
|
||||
{
|
||||
// update_quick_stats();
|
||||
|
||||
// Update All Every 5 Seconds.
|
||||
// These Update Functions do the locking themselves.
|
||||
// Update All Every 5 Seconds.
|
||||
// These Update Functions do the locking themselves.
|
||||
#ifdef DEBUG_TICK
|
||||
std::cerr << "RsServer::run() Updates()" << std::endl;
|
||||
std::cerr << "RsServer::run() Updates()" << std::endl;
|
||||
#endif
|
||||
|
||||
mConfigMgr->tick(); /* saves stuff */
|
||||
mConfigMgr->tick(); /* saves stuff */
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// every 60 loops (> 1 min)
|
||||
if (++loop >= 60)
|
||||
{
|
||||
loop = 0;
|
||||
// every 60 loops (> 1 min)
|
||||
if (++mLoop >= 60)
|
||||
{
|
||||
mLoop = 0;
|
||||
|
||||
/* force saving FileTransferStatus TODO */
|
||||
//ftserver->saveFileTransferStatus();
|
||||
/* force saving FileTransferStatus TODO */
|
||||
//ftserver->saveFileTransferStatus();
|
||||
|
||||
/* see if we need to resave certs */
|
||||
//AuthSSL::getAuthSSL()->CheckSaveCertificates();
|
||||
/* see if we need to resave certs */
|
||||
//AuthSSL::getAuthSSL()->CheckSaveCertificates();
|
||||
|
||||
/* hour loop */
|
||||
if (++min >= 60)
|
||||
{
|
||||
min = 0;
|
||||
}
|
||||
}
|
||||
/* hour loop */
|
||||
if (++mMin >= 60)
|
||||
{
|
||||
mMin = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Tick slow services */
|
||||
if(rsPlugins)
|
||||
rsPlugins->slowTickPlugins((time_t)ts);
|
||||
/* Tick slow services */
|
||||
if(rsPlugins)
|
||||
rsPlugins->slowTickPlugins((time_t)ts);
|
||||
|
||||
// slow update tick as well.
|
||||
// update();
|
||||
} // end of slow tick.
|
||||
// slow update tick as well.
|
||||
// update();
|
||||
} // end of slow tick.
|
||||
|
||||
} // end of only once a second.
|
||||
} // end of only once a second.
|
||||
|
||||
double endCycleTs = getCurrentTS();
|
||||
double cycleTime = endCycleTs - ts;
|
||||
if (cycleTime > WARN_BIG_CYCLE_TIME)
|
||||
{
|
||||
std::string out;
|
||||
rs_sprintf(out, "RsServer::run() WARNING Excessively Long Cycle Time: %g secs => Please DEBUG", cycleTime);
|
||||
std::cerr << out << std::endl;
|
||||
double endCycleTs = getCurrentTS();
|
||||
double cycleTime = endCycleTs - ts;
|
||||
if (cycleTime > WARN_BIG_CYCLE_TIME)
|
||||
{
|
||||
std::string out;
|
||||
rs_sprintf(out, "RsServer::run() WARNING Excessively Long Cycle Time: %g secs => Please DEBUG", cycleTime);
|
||||
std::cerr << out << std::endl;
|
||||
|
||||
rslog(RSL_ALERT, rsserverzone, out);
|
||||
}
|
||||
}
|
||||
return;
|
||||
rslog(RSL_ALERT, rsserverzone, out);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -74,7 +74,7 @@ class RsPluginManager;
|
|||
//int InitRetroShare(int argc, char **argv, RsInit *config);
|
||||
//int LoadCertificates(RsInit *config);
|
||||
|
||||
class RsServer: public RsControl, public RsThread
|
||||
class RsServer: public RsControl, public RsTickingThread
|
||||
{
|
||||
public:
|
||||
/****************************************/
|
||||
|
@ -88,7 +88,7 @@ class RsServer: public RsControl, public RsThread
|
|||
virtual ~RsServer();
|
||||
|
||||
/* Thread Fn: Run the Core */
|
||||
virtual void run();
|
||||
virtual void data_tick();
|
||||
|
||||
/* locking stuff */
|
||||
void lockRsCore()
|
||||
|
@ -120,7 +120,7 @@ class RsServer: public RsControl, public RsThread
|
|||
/* Config */
|
||||
|
||||
virtual void ConfigFinalSave( );
|
||||
virtual void startServiceThread(RsThread *t) ;
|
||||
virtual void startServiceThread(RsTickingThread *t) ;
|
||||
|
||||
/************* Rs shut down function: in upnp 'port lease time' bug *****************/
|
||||
|
||||
|
@ -162,7 +162,7 @@ class RsServer: public RsControl, public RsThread
|
|||
|
||||
// This list contains all threaded services. It will be used to shut them down properly.
|
||||
|
||||
std::list<RsThread*> mRegisteredServiceThreads ;
|
||||
std::list<RsTickingThread*> mRegisteredServiceThreads ;
|
||||
|
||||
/* GXS */
|
||||
// p3Wiki *mWiki;
|
||||
|
@ -184,6 +184,16 @@ class RsServer: public RsControl, public RsThread
|
|||
|
||||
// Worker Data.....
|
||||
|
||||
int mMin ;
|
||||
int mLoop ;
|
||||
int mLastts ;
|
||||
long mLastSec ;
|
||||
double mAvgTickRate ;
|
||||
double mTimeDelta ;
|
||||
|
||||
static const double minTimeDelta = 0.1; // 25;
|
||||
static const double maxTimeDelta = 0.5;
|
||||
static const double kickLimit = 0.15;
|
||||
};
|
||||
|
||||
/* Helper function to convert windows paths
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue