From d0039241d3af8b17d102dead03abec28ef3bbaed Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 27 Jan 2018 20:22:31 +0100 Subject: [PATCH] replaced usleep() by rstime::rs_usleep() which accepts times >= 1 sec. Should fix problems on windows --- libretroshare/src/crypto/chacha20.cpp | 10 ++-- .../src/file_sharing/directory_updater.cc | 3 +- libretroshare/src/file_sharing/hash_cache.cc | 10 ++-- libretroshare/src/ft/ftcontroller.cc | 3 +- libretroshare/src/ft/ftextralist.cc | 18 ++----- libretroshare/src/gxs/rsdataservice.cc | 14 ++--- libretroshare/src/gxs/rsgenexchange.cc | 3 +- libretroshare/src/gxs/rsgxsnetservice.cc | 3 +- libretroshare/src/libretroshare.pro | 4 +- libretroshare/src/pqi/authgpg.cc | 3 +- libretroshare/src/pqi/pqissludp.cc | 5 +- libretroshare/src/pqi/pqithreadstreamer.cc | 5 +- libretroshare/src/rsserver/p3face-server.cc | 7 +-- .../src/services/autoproxy/p3i2pbob.cc | 3 +- .../services/autoproxy/rsautoproxymonitor.cc | 3 +- libretroshare/src/services/p3idservice.cc | 13 ++--- libretroshare/src/util/rsdir.cc | 7 +-- libretroshare/src/util/rsthreads.cc | 4 +- .../src/util/{rsscopetimer.cc => rstime.cc} | 24 ++++++++- .../src/util/{rsscopetimer.h => rstime.h} | 53 +++++++++++-------- plugins/FeedReader/services/p3FeedReader.cc | 3 +- .../FeedReader/services/p3FeedReaderThread.cc | 8 ++- .../src/TorControl/TorControlWindow.cpp | 3 +- .../src/gui/chat/ChatLobbyDialog.cpp | 5 +- retroshare-gui/src/main.cpp | 3 +- retroshare-gui/src/util/HandleRichText.cpp | 4 +- retroshare-gui/src/util/imageutil.cpp | 6 +-- retroshare-nogui/src/retroshare.cc | 7 +-- 28 files changed, 134 insertions(+), 100 deletions(-) rename libretroshare/src/util/{rsscopetimer.cc => rstime.cc} (82%) rename libretroshare/src/util/{rsscopetimer.h => rstime.h} (61%) diff --git a/libretroshare/src/crypto/chacha20.cpp b/libretroshare/src/crypto/chacha20.cpp index 1240d0cb8..3f12b2086 100644 --- a/libretroshare/src/crypto/chacha20.cpp +++ b/libretroshare/src/crypto/chacha20.cpp @@ -38,7 +38,7 @@ #include "crypto/chacha20.h" #include "util/rsprint.h" #include "util/rsrandom.h" -#include "util/rsscopetimer.h" +#include "util/rstime.h" #define rotl(x,n) { x = (x << n) | (x >> (-n & 31)) ;} @@ -1385,27 +1385,27 @@ bool perform_tests() uint8_t received_tag[16] ; { - RsScopeTimer s("AEAD1") ; + rstime::RsScopeTimer s("AEAD1") ; chacha20_encrypt_rs(key, 1, nonce, ten_megabyte_data,SIZE) ; std::cerr << " Chacha20 encryption speed : " << SIZE / (1024.0*1024.0) / s.duration() << " MB/s" << std::endl; } { - RsScopeTimer s("AEAD2") ; + rstime::RsScopeTimer s("AEAD2") ; AEAD_chacha20_poly1305_rs(key,nonce,ten_megabyte_data,SIZE,aad,12,received_tag,true) ; std::cerr << " AEAD/poly1305 own encryption speed : " << SIZE / (1024.0*1024.0) / s.duration() << " MB/s" << std::endl; } #if OPENSSL_VERSION_NUMBER >= 0x010100000L && !defined(LIBRESSL_VERSION_NUMBER) { - RsScopeTimer s("AEAD3") ; + rstime::RsScopeTimer s("AEAD3") ; AEAD_chacha20_poly1305_openssl(key,nonce,ten_megabyte_data,SIZE,aad,12,received_tag,true) ; std::cerr << " AEAD/poly1305 openssl encryption speed: " << SIZE / (1024.0*1024.0) / s.duration() << " MB/s" << std::endl; } #endif { - RsScopeTimer s("AEAD4") ; + rstime::RsScopeTimer s("AEAD4") ; AEAD_chacha20_sha256(key,nonce,ten_megabyte_data,SIZE,aad,12,received_tag,true) ; std::cerr << " AEAD/sha256 encryption speed : " << SIZE / (1024.0*1024.0) / s.duration() << " MB/s" << std::endl; diff --git a/libretroshare/src/file_sharing/directory_updater.cc b/libretroshare/src/file_sharing/directory_updater.cc index 085f92ebb..6d95961ab 100644 --- a/libretroshare/src/file_sharing/directory_updater.cc +++ b/libretroshare/src/file_sharing/directory_updater.cc @@ -23,6 +23,7 @@ * */ #include "util/folderiterator.h" +#include "util/rstime.h" #include "rsserver/p3face.h" #include "directory_storage.h" @@ -92,7 +93,7 @@ void LocalDirectoryUpdater::data_tick() for(uint32_t i=0;i<10;++i) { - usleep(1*1000*1000); + rstime::rs_usleep(1*1000*1000); { if(mForceUpdate) diff --git a/libretroshare/src/file_sharing/hash_cache.cc b/libretroshare/src/file_sharing/hash_cache.cc index 89a4aec38..f0db680cf 100644 --- a/libretroshare/src/file_sharing/hash_cache.cc +++ b/libretroshare/src/file_sharing/hash_cache.cc @@ -24,7 +24,7 @@ */ #include "util/rsdir.h" #include "util/rsprint.h" -#include "util/rsscopetimer.h" +#include "util/rstime.h" #include "rsserver/p3face.h" #include "pqi/authssl.h" #include "hash_cache.h" @@ -125,7 +125,7 @@ void HashStorage::data_tick() std::cerr << "nothing to hash. Sleeping for " << st << " us" << std::endl; #endif - usleep(st); // when no files to hash, just wait for 2 secs. This avoids a dramatic loop. + rstime::rs_usleep(st); // when no files to hash, just wait for 2 secs. This avoids a dramatic loop. if(st > MAX_INACTIVITY_SLEEP_TIME) { @@ -163,7 +163,7 @@ void HashStorage::data_tick() if(paused) // we need to wait off mutex!! { - usleep(MAX_INACTIVITY_SLEEP_TIME) ; + rstime::rs_usleep(MAX_INACTIVITY_SLEEP_TIME) ; std::cerr << "Hashing process currently paused." << std::endl; return; } @@ -190,7 +190,7 @@ void HashStorage::data_tick() RsServer::notify()->notifyHashingInfo(NOTIFY_HASHTYPE_HASH_FILE, tmpout) ; - double seconds_origin = RsScopeTimer::currentTime() ; + double seconds_origin = rstime::RsScopeTimer::currentTime() ; if(RsDirUtil::getFileHash(job.full_path, hash,size, this)) { @@ -215,7 +215,7 @@ void HashStorage::data_tick() else std::cerr << "ERROR: cannot hash file " << job.full_path << std::endl; - mHashingTime += RsScopeTimer::currentTime() - seconds_origin ; + mHashingTime += rstime::RsScopeTimer::currentTime() - seconds_origin ; mHashedBytes += size ; if(mHashingTime > 3) diff --git a/libretroshare/src/ft/ftcontroller.cc b/libretroshare/src/ft/ftcontroller.cc index 382d13cb6..fb6d54bae 100644 --- a/libretroshare/src/ft/ftcontroller.cc +++ b/libretroshare/src/ft/ftcontroller.cc @@ -40,6 +40,7 @@ #endif #include "util/rsdiscspace.h" #include "util/rsmemory.h" +#include "util/rstime.h" #include "ft/ftcontroller.h" @@ -219,7 +220,7 @@ void ftController::data_tick() /* check the queues */ //Waiting 1 sec before start - usleep(1*1000*1000); // 1 sec + rstime::rs_usleep(1*1000*1000); // 1 sec #ifdef CONTROL_DEBUG //std::cerr << "ftController::run()"; diff --git a/libretroshare/src/ft/ftextralist.cc b/libretroshare/src/ft/ftextralist.cc index cd52f5ce5..b6fec4076 100644 --- a/libretroshare/src/ft/ftextralist.cc +++ b/libretroshare/src/ft/ftextralist.cc @@ -32,6 +32,7 @@ #include "ft/ftextralist.h" #include "rsitems/rsconfigitems.h" #include "util/rsdir.h" +#include "util/rstime.h" #include #include /* for (u)sleep() */ #include @@ -64,12 +65,8 @@ void ftExtraList::data_tick() /* Hash a file */ hashAFile(); -#ifdef WIN32 - Sleep(1); -#else /* microsleep */ - usleep(10); -#endif + rstime::rs_usleep(10); } else { @@ -513,16 +510,7 @@ bool ftExtraList::loadList(std::list& load) delete (*it); /* short sleep */ -#ifndef WINDOWS_SYS -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ - usleep(1000); /* 1000 per second */ - -#else -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ - Sleep(1); -#endif -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ - + rstime::rs_usleep(1000) ; } load.clear() ; return true; diff --git a/libretroshare/src/gxs/rsdataservice.cc b/libretroshare/src/gxs/rsdataservice.cc index c3252a27c..305f8dee1 100644 --- a/libretroshare/src/gxs/rsdataservice.cc +++ b/libretroshare/src/gxs/rsdataservice.cc @@ -35,7 +35,7 @@ #include #ifdef RS_DATA_SERVICE_DEBUG_TIME -#include +#include #endif #include "rsdataservice.h" @@ -1066,7 +1066,7 @@ bool RsDataService::validSize(RsNxsGrp* grp) const int RsDataService::retrieveNxsGrps(std::map &grp, bool withMeta, bool /* cache */) { #ifdef RS_DATA_SERVICE_DEBUG_TIME - RsScopeTimer timer(""); + rstime::RsScopeTimer timer(""); int resultCount = 0; int requestedGroups = grp.size(); #endif @@ -1166,7 +1166,7 @@ void RsDataService::locked_retrieveGroups(RetroCursor* c, std::vector int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, bool /* cache */, bool withMeta) { #ifdef RS_DATA_SERVICE_DEBUG_TIME - RsScopeTimer timer(""); + rstime::RsScopeTimer timer(""); int resultCount = 0; #endif @@ -1254,7 +1254,7 @@ int RsDataService::retrieveGxsMsgMetaData(const GxsMsgReq& reqIds, GxsMsgMetaRes RsStackMutex stack(mDbMutex); #ifdef RS_DATA_SERVICE_DEBUG_TIME - RsScopeTimer timer(""); + rstime::RsScopeTimer timer(""); int resultCount = 0; #endif @@ -1341,7 +1341,7 @@ int RsDataService::retrieveGxsGrpMetaData(RsGxsGrpMetaTemporaryMap& grp) RsStackMutex stack(mDbMutex); #ifdef RS_DATA_SERVICE_DEBUG_TIME - RsScopeTimer timer(""); + rstime::RsScopeTimer timer(""); int resultCount = 0; int requestedGroups = grp.size(); #endif @@ -1549,7 +1549,7 @@ int RsDataService::retrieveGroupIds(std::vector &grpIds) RsStackMutex stack(mDbMutex); #ifdef RS_DATA_SERVICE_DEBUG_TIME - RsScopeTimer timer(""); + rstime::RsScopeTimer timer(""); int resultCount = 0; #endif @@ -1586,7 +1586,7 @@ int RsDataService::retrieveGroupIds(std::vector &grpIds) int RsDataService::retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_vector& msgIds) { #ifdef RS_DATA_SERVICE_DEBUG_TIME - RsScopeTimer timer(""); + rstime::RsScopeTimer timer(""); int resultCount = 0; #endif diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index dd4691306..e1d15d614 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -31,6 +31,7 @@ #include "gxssecurity.h" #include "util/contentvalue.h" #include "util/rsprint.h" +#include "util/rstime.h" #include "retroshare/rsgxsflags.h" #include "retroshare/rsgxscircles.h" #include "retroshare/rsgrouter.h" @@ -138,7 +139,7 @@ void RsGenExchange::data_tick() static const double timeDelta = 0.1; // slow tick in sec tick(); - usleep((int) (timeDelta * 1000 *1000)); // timeDelta sec + rstime::rs_usleep((int) (timeDelta * 1000 *1000)); // timeDelta sec } void RsGenExchange::tick() diff --git a/libretroshare/src/gxs/rsgxsnetservice.cc b/libretroshare/src/gxs/rsgxsnetservice.cc index ec26e0081..8f055bc1b 100644 --- a/libretroshare/src/gxs/rsgxsnetservice.cc +++ b/libretroshare/src/gxs/rsgxsnetservice.cc @@ -210,6 +210,7 @@ #include "retroshare/rspeers.h" #include "pgp/pgpauxutils.h" #include "util/rsdir.h" +#include "util/rstime.h" #include "util/rsmemory.h" #include "util/stacktrace.h" @@ -1787,7 +1788,7 @@ void RsGxsNetService::data_tick() static const double timeDelta = 0.5; //Start waiting as nothing to do in runup - usleep((int) (timeDelta * 1000 * 1000)); // timeDelta sec + rstime::rs_usleep((int) (timeDelta * 1000 * 1000)); // timeDelta sec if(mUpdateCounter >= 120) // 60 seconds { diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 7093f273e..19d2dce27 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -547,7 +547,7 @@ HEADERS += util/folderiterator.h \ util/rsmemcache.h \ util/rstickevent.h \ util/rsrecogn.h \ - util/rsscopetimer.h \ + util/rstime.h \ util/stacktrace.h \ util/rsdeprecate.h \ util/cxx11retrocompat.h @@ -695,7 +695,7 @@ SOURCES += util/folderiterator.cc \ util/rsrandom.cc \ util/rstickevent.cc \ util/rsrecogn.cc \ - util/rsscopetimer.cc + util/rstime.cc upnp_miniupnpc { diff --git a/libretroshare/src/pqi/authgpg.cc b/libretroshare/src/pqi/authgpg.cc index 8553fc127..ccfb8a260 100644 --- a/libretroshare/src/pqi/authgpg.cc +++ b/libretroshare/src/pqi/authgpg.cc @@ -35,6 +35,7 @@ #include "pgp/pgphandler.h" #include +#include #include #include /* for (u)sleep() */ #include @@ -187,7 +188,7 @@ int AuthGPG::GPGInit(const RsPgpId &ownId) void AuthGPG::data_tick() { - usleep(100 * 1000); //100 msec + rstime::rs_usleep(100 * 1000); //100 msec /// every 100 milliseconds processServices(); diff --git a/libretroshare/src/pqi/pqissludp.cc b/libretroshare/src/pqi/pqissludp.cc index 29bd228c9..f14fb82e5 100644 --- a/libretroshare/src/pqi/pqissludp.cc +++ b/libretroshare/src/pqi/pqissludp.cc @@ -35,6 +35,7 @@ #include "util/rsdebug.h" #include "util/rsnet.h" +#include "util/rstime.h" #include "util/rsstring.h" #include "pqi/p3linkmgr.h" @@ -571,7 +572,7 @@ bool pqissludp::moretoread(uint32_t usec) { return true; } - usleep(usec); + rstime::rs_usleep(usec); } /* check for more to read first ... if nothing... check error @@ -655,7 +656,7 @@ bool pqissludp::cansend(uint32_t usec) return true; } - usleep(usec); + rstime::rs_usleep(usec); } rslog(RSL_DEBUG_ALL, pqissludpzone, diff --git a/libretroshare/src/pqi/pqithreadstreamer.cc b/libretroshare/src/pqi/pqithreadstreamer.cc index f99be143b..54ab925e1 100644 --- a/libretroshare/src/pqi/pqithreadstreamer.cc +++ b/libretroshare/src/pqi/pqithreadstreamer.cc @@ -24,6 +24,7 @@ */ +#include "util/rstime.h" #include "pqi/pqithreadstreamer.h" #include @@ -69,7 +70,7 @@ void pqithreadstreamer::data_tick() if (!isactive) { - usleep(DEFAULT_STREAMER_IDLE_SLEEP); + rstime::rs_usleep(DEFAULT_STREAMER_IDLE_SLEEP); return ; } @@ -92,7 +93,7 @@ void pqithreadstreamer::data_tick() if (sleep_period) { - usleep(sleep_period); + rstime::rs_usleep(sleep_period); } } diff --git a/libretroshare/src/rsserver/p3face-server.cc b/libretroshare/src/rsserver/p3face-server.cc index a7aad022c..d7578b8fd 100644 --- a/libretroshare/src/rsserver/p3face-server.cc +++ b/libretroshare/src/rsserver/p3face-server.cc @@ -25,6 +25,7 @@ */ +#include "util/rstime.h" #include "rsserver/p3face.h" #include "retroshare/rsplugin.h" @@ -134,11 +135,7 @@ RsServer::~RsServer() /* Thread Fn: Run the Core */ void RsServer::data_tick() { -#ifndef WINDOWS_SYS - usleep((int) (mTimeDelta * 1000000)); -#else - Sleep((int) (mTimeDelta * 1000)); -#endif + rstime::rs_usleep(mTimeDelta * 1000000); double ts = getCurrentTS(); double delta = ts - mLastts; diff --git a/libretroshare/src/services/autoproxy/p3i2pbob.cc b/libretroshare/src/services/autoproxy/p3i2pbob.cc index 32be374f4..917b8a8a7 100644 --- a/libretroshare/src/services/autoproxy/p3i2pbob.cc +++ b/libretroshare/src/services/autoproxy/p3i2pbob.cc @@ -8,6 +8,7 @@ #include "util/radix32.h" #include "util/radix64.h" #include "util/rsdebug.h" +#include "util/rstime.h" #include "util/rsprint.h" #include "util/rsrandom.h" @@ -39,7 +40,7 @@ static struct RsLog::logInfo i2pBobLogInfo = {RsLog::Default, "p3I2pBob"}; static const time_t selfCheckPeroid = 30; void doSleep(useconds_t timeToSleepMS) { - usleep((useconds_t) (timeToSleepMS * 1000)); + rstime::rs_usleep((useconds_t) (timeToSleepMS * 1000)); } p3I2pBob::p3I2pBob(p3PeerMgr *peerMgr) diff --git a/libretroshare/src/services/autoproxy/rsautoproxymonitor.cc b/libretroshare/src/services/autoproxy/rsautoproxymonitor.cc index 0f5eb22d5..d4d3f87ce 100644 --- a/libretroshare/src/services/autoproxy/rsautoproxymonitor.cc +++ b/libretroshare/src/services/autoproxy/rsautoproxymonitor.cc @@ -1,6 +1,7 @@ #include "rsautoproxymonitor.h" #include /* for usleep() */ +#include "util/rstime.h" rsAutoProxyMonitor *rsAutoProxyMonitor::mInstance = NULL; @@ -93,7 +94,7 @@ void rsAutoProxyMonitor::stopAllRSShutdown() // wait for shutdown of all services uint32_t t = 0, timeout = 15; do { - usleep(1000 * 1000); + rstime::rs_usleep(1000 * 1000); RS_STACK_MUTEX(mLock); std::cout << "(II) waiting for auto proxy service(s) to shut down " << t << "/" << timeout << " (remaining: " << mProxies.size() << ")" << std::endl; if (mProxies.empty()) diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index 2b37b181e..71b0e2274 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -34,6 +34,7 @@ #include "util/rsstring.h" #include "util/radix64.h" #include "util/rsdir.h" +#include "util/rstime.h" #include "crypto/hashstream.h" #include "gxs/gxssecurity.h" #include "retroshare/rspeers.h" @@ -1071,7 +1072,7 @@ bool p3IdService::signData(const uint8_t *data,uint32_t data_size,const RsGxsId& #ifdef DEBUG_IDS std::cerr << " Cannot get key. Waiting for caching. try " << i << "/6" << std::endl; #endif - usleep(500 * 1000) ; // sleep for 500 msec. + rstime::rs_usleep(500 * 1000) ; // sleep for 500 msec. } else break ; @@ -1110,7 +1111,7 @@ bool p3IdService::validateData(const uint8_t *data,uint32_t data_size,const RsTl #ifdef DEBUG_IDS std::cerr << " Cannot get key. Waiting for caching. try " << i << "/6" << std::endl; #endif - if(force_load) usleep(500 * 1000) ; // sleep for 500 msec. + if(force_load) rstime::rs_usleep(500 * 1000) ; // sleep for 500 msec. } else break ; @@ -1151,7 +1152,7 @@ bool p3IdService::encryptData( const uint8_t *decrypted_data, if(getKey(encryption_key_id,encryption_key)) break ; else - usleep(500*1000) ; // sleep half a sec. + rstime::rs_usleep(500*1000) ; // sleep half a sec. if(encryption_key.keyId.isNull()) { @@ -1222,7 +1223,7 @@ bool p3IdService::encryptData( const uint8_t* decrypted_data, } if(keyNotYetFoundIds.empty()) break; - else usleep(500*1000); + else rstime::rs_usleep(500*1000); } if(!keyNotYetFoundIds.empty()) @@ -1279,7 +1280,7 @@ bool p3IdService::decryptData( const uint8_t *encrypted_data, int maxRounds = force_load ? 6 : 1; for(int i=0; i= 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 ; diff --git a/libretroshare/src/util/rsthreads.cc b/libretroshare/src/util/rsthreads.cc index 8899ee01c..39eca594a 100644 --- a/libretroshare/src/util/rsthreads.cc +++ b/libretroshare/src/util/rsthreads.cc @@ -30,6 +30,8 @@ #include #include +#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() diff --git a/libretroshare/src/util/rsscopetimer.cc b/libretroshare/src/util/rstime.cc similarity index 82% rename from libretroshare/src/util/rsscopetimer.cc rename to libretroshare/src/util/rstime.cc index b3ce8fbc3..74808afb9 100644 --- a/libretroshare/src/util/rsscopetimer.cc +++ b/libretroshare/src/util/rstime.cc @@ -24,8 +24,28 @@ */ #include +#include #include -#include "rsscopetimer.h" +#include +#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; } + +} diff --git a/libretroshare/src/util/rsscopetimer.h b/libretroshare/src/util/rstime.h similarity index 61% rename from libretroshare/src/util/rsscopetimer.h rename to libretroshare/src/util/rstime.h index 875a8bb5e..4359a28ff 100644 --- a/libretroshare/src/util/rsscopetimer.h +++ b/libretroshare/src/util/rstime.h @@ -23,29 +23,40 @@ * */ -// Use this class to measure and display time duration of a given environment: -// -// { -// RsScopeTimer timer("callToMeasure()") ; -// -// callToMeasure() ; -// } -// - #include -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 ; + }; + +} diff --git a/plugins/FeedReader/services/p3FeedReader.cc b/plugins/FeedReader/services/p3FeedReader.cc index efa3b6688..256a1e285 100644 --- a/plugins/FeedReader/services/p3FeedReader.cc +++ b/plugins/FeedReader/services/p3FeedReader.cc @@ -26,6 +26,7 @@ #include "retroshare/rsiface.h" #include "retroshare/rsgxsforums.h" #include "util/rsstring.h" +#include "util/rstime.h" #include "gxs/rsgenexchange.h" #include @@ -2240,7 +2241,7 @@ bool p3FeedReader::waitForToken(uint32_t token) break; } - usleep(500 * 1000); // sleep for 500 msec + rstime::rs_usleep(500 * 1000); // sleep for 500 msec } return false; diff --git a/plugins/FeedReader/services/p3FeedReaderThread.cc b/plugins/FeedReader/services/p3FeedReaderThread.cc index 9ec1106ef..b8db8db46 100644 --- a/plugins/FeedReader/services/p3FeedReaderThread.cc +++ b/plugins/FeedReader/services/p3FeedReaderThread.cc @@ -22,6 +22,7 @@ #include "p3FeedReaderThread.h" #include "rsFeedReaderItems.h" #include "util/rsstring.h" +#include "util/rstime.h" #include "util/CURLWrapper.h" #include "util/XMLWrapper.h" #include "util/HTMLWrapper.h" @@ -51,11 +52,8 @@ p3FeedReaderThread::~p3FeedReaderThread() void p3FeedReaderThread::data_tick() { -#ifdef WIN32 - Sleep(1000); -#else - usleep(1000000); -#endif + rstime::rs_usleep(1000000); + /* every second */ switch (mType) { diff --git a/retroshare-gui/src/TorControl/TorControlWindow.cpp b/retroshare-gui/src/TorControl/TorControlWindow.cpp index 61c7b96ea..368537a95 100644 --- a/retroshare-gui/src/TorControl/TorControlWindow.cpp +++ b/retroshare-gui/src/TorControl/TorControlWindow.cpp @@ -7,6 +7,7 @@ #include #include +#include "util/rstime.h" #include "TorControlWindow.h" #include "TorManager.h" @@ -152,7 +153,7 @@ TorControlDialog::TorStatus TorControlDialog::checkForTor() { switch(mTorManager->control()->torStatus()) { - case Tor::TorControl::TorReady: usleep(1*1000*1000);return TOR_STATUS_OK ; + case Tor::TorControl::TorReady: rstime::rs_usleep(1*1000*1000);return TOR_STATUS_OK ; default: return TOR_STATUS_UNKNOWN ; } diff --git a/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp b/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp index 93028d20c..918939917 100644 --- a/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp +++ b/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp @@ -46,7 +46,8 @@ #include "util/HandleRichText.h" #include "util/QtVersion.h" -#include +#include "retroshare/rsnotify.h" +#include "util/rstime.h" #include #include @@ -339,7 +340,7 @@ void ChatLobbyDialog::init(const ChatId &/*id*/, const QString &/*title*/) if(rsIdentity->getIdDetails(gxs_id,details)) break ; else - usleep(1000*300) ; + rstime::rs_usleep(1000*300) ; ui.chatWidget->setName(QString::fromUtf8(details.mNickname.c_str())); //ui.chatWidget->addToolsAction(ui.actionChangeNickname); diff --git a/retroshare-gui/src/main.cpp b/retroshare-gui/src/main.cpp index df865e416..f64adb8ed 100644 --- a/retroshare-gui/src/main.cpp +++ b/retroshare-gui/src/main.cpp @@ -46,6 +46,7 @@ #include "lang/languagesupport.h" #include "util/RsGxsUpdateBroadcast.h" #include "util/rsdir.h" +#include "util/rstime.h" #ifdef RETROTOR #include "TorControl/TorManager.h" @@ -371,7 +372,7 @@ feenableexcept(FE_INVALID | FE_DIVBYZERO); while(tcd.checkForTor() != TorControlDialog::TOR_STATUS_OK || tcd.checkForHiddenService() != TorControlDialog::HIDDEN_SERVICE_STATUS_OK) // runs until some status is reached: either tor works, or it fails. { QCoreApplication::processEvents(); - usleep(0.2*1000*1000) ; + rstime::rs_usleep(0.2*1000*1000) ; } tcd.hide(); diff --git a/retroshare-gui/src/util/HandleRichText.cpp b/retroshare-gui/src/util/HandleRichText.cpp index cbe524cf7..2ab1dcb42 100644 --- a/retroshare-gui/src/util/HandleRichText.cpp +++ b/retroshare-gui/src/util/HandleRichText.cpp @@ -36,7 +36,7 @@ #include "gui/RetroShareLink.h" #include "util/ObjectPainter.h" #include "util/imageutil.h" -#include "util/rsscopetimer.h" +#include "util/rstime.h" #include @@ -1151,7 +1151,7 @@ bool RsHtml::makeEmbeddedImage(const QString &fileName, QString &embeddedImage, /** Converts image to embedded image HTML fragment **/ bool RsHtml::makeEmbeddedImage(const QImage &originalImage, QString &embeddedImage, const int maxPixels, const int maxBytes) { - RsScopeTimer s("Embed image"); + rstime::RsScopeTimer s("Embed image"); QImage opt; return ImageUtil::optimizeSize(embeddedImage, originalImage, opt, maxPixels, maxBytes); } diff --git a/retroshare-gui/src/util/imageutil.cpp b/retroshare-gui/src/util/imageutil.cpp index ca2716b09..d32da5a97 100644 --- a/retroshare-gui/src/util/imageutil.cpp +++ b/retroshare-gui/src/util/imageutil.cpp @@ -1,6 +1,6 @@ #include "imageutil.h" #include "util/misc.h" -#include "util/rsscopetimer.h" +#include "util/rstime.h" #include #include @@ -113,7 +113,7 @@ bool ImageUtil::optimizeSize(QString &html, const QImage& original, QImage &opti int ImageUtil::checkSize(QString &embeddedImage, const QImage &img, int maxBytes) { - RsScopeTimer st("Check size"); + rstime::RsScopeTimer st("Check size"); QByteArray bytearray; QBuffer buffer(&bytearray); @@ -166,7 +166,7 @@ void ImageUtil::quantization(const QImage &img, QVector &palette) int bits = 4; // bits/pixel int samplesize = 100000; //only take this many color samples - RsScopeTimer st("Quantization"); + rstime::RsScopeTimer st("Quantization"); QSet colors; //collect color information diff --git a/retroshare-nogui/src/retroshare.cc b/retroshare-nogui/src/retroshare.cc index ef02bb623..65d8c6168 100644 --- a/retroshare-nogui/src/retroshare.cc +++ b/retroshare-nogui/src/retroshare.cc @@ -30,7 +30,8 @@ #include "notifytxt.h" #include -#include +#include "util/argstream.h" +#include "util/rstime.h" #include #ifdef WINDOWS_SYS #include @@ -105,7 +106,7 @@ int main(int argc, char **argv) while(ctrl_mod.processShouldExit() == false) { - usleep(1000*1000); + rstime::rs_usleep(1000*1000); if(!tac.isRunning() && !already) { @@ -224,7 +225,7 @@ int main(int argc, char **argv) #endif } - usleep(1000); + rstime::rs_usleep(1000); } return 1;