replaced usleep() by rstime::rs_usleep() which accepts times >= 1 sec. Should fix problems on windows

This commit is contained in:
csoler 2018-01-27 20:22:31 +01:00
parent c1fccef53b
commit d0039241d3
28 changed files with 134 additions and 100 deletions

View file

@ -33,6 +33,7 @@
#include "util/rsdir.h"
#include "util/rsstring.h"
#include "util/rsrandom.h"
#include "util/rstime.h"
#include "util/rsmemory.h"
#include "util/folderiterator.h"
#include "retroshare/rstypes.h"
@ -711,7 +712,7 @@ bool RsDirUtil::renameFile(const std::string& from, const std::string& to)
#endif
/* set errno? */
return false ;
usleep(100 * 1000); // 100 msec
rstime::rs_usleep(100 * 1000); // 100 msec
if (loops >= 30)
return false ;
@ -917,7 +918,7 @@ RsStackFileLock::RsStackFileLock(const std::string& file_path)
while(RsDirUtil::createLockFile(file_path,_file_handle))
{
std::cerr << "Cannot acquire file lock " << file_path << ", waiting 1 sec." << std::endl;
usleep(1 * 1000 * 1000) ; // 1 sec
rstime::rs_usleep(1 * 1000 * 1000) ; // 1 sec
}
#ifdef RSDIR_DEBUG
std::cerr << "Acquired file handle " << _file_handle << ", lock file:" << file_path << std::endl;
@ -1321,7 +1322,7 @@ bool RsDirUtil::renameWideFile(const std::wstring& from, const std::wstring& to)
#endif
/* set errno? */
return false ;
usleep(100 * 1000); //100 msec
rstime::rs_usleep(100 * 1000); //100 msec
if (loops >= 30)
return false ;

View file

@ -30,6 +30,8 @@
#include <iostream>
#include <time.h>
#include "util/rstime.h"
#ifdef __APPLE__
int __attribute__((weak)) pthread_setname_np(const char *__buf) ;
int RS_pthread_setname_np(pthread_t /*__target_thread*/, const char *__buf) {
@ -289,7 +291,7 @@ void RsQueueThread::data_tick()
THREAD_DEBUG << "RsQueueThread::data_tick() no work: sleeping for: " << mLastSleep << " ms" << std::endl;
#endif
}
usleep(mLastSleep * 1000); // mLastSleep msec
rstime::rs_usleep(mLastSleep * 1000); // mLastSleep msec
}
void RsMutex::unlock()

View file

@ -24,8 +24,28 @@
*/
#include <iostream>
#include <thread>
#include <sys/time.h>
#include "rsscopetimer.h"
#include <sys/unistd.h>
#include "rstime.h"
namespace rstime {
int rs_usleep(uint32_t micro_seconds)
{
while(micro_seconds >= 1000000)
{
// usleep cannot be called with 1000000 or more.
usleep(500000) ;
usleep(500000) ;
micro_seconds -= 1000000 ;
}
usleep(micro_seconds) ;
return 0 ;
}
RsScopeTimer::RsScopeTimer(const std::string& name)
{
@ -57,3 +77,5 @@ double RsScopeTimer::duration()
{
return currentTime() - _seconds;
}
}

View file

@ -23,29 +23,40 @@
*
*/
// Use this class to measure and display time duration of a given environment:
//
// {
// RsScopeTimer timer("callToMeasure()") ;
//
// callToMeasure() ;
// }
//
#include <string>
class RsScopeTimer
{
public:
RsScopeTimer(const std::string& name);
~RsScopeTimer();
namespace rstime {
void start();
double duration();
/*!
* \brief This is a cross-system definition of usleep, which accepts any 32 bits number of micro-seconds.
*/
static double currentTime();
int rs_usleep(uint32_t micro_seconds);
private:
std::string _name ;
double _seconds ;
};
/* Use this class to measure and display time duration of a given environment:
{
RsScopeTimer timer("callToMeasure()") ;
callToMeasure() ;
}
*/
class RsScopeTimer
{
public:
RsScopeTimer(const std::string& name);
~RsScopeTimer();
void start();
double duration();
static double currentTime();
private:
std::string _name ;
double _seconds ;
};
}