portable sleep function

This commit is contained in:
badaix 2016-10-28 10:10:58 +02:00
parent 17879618c7
commit 951548f412
12 changed files with 35 additions and 19 deletions

View file

@ -22,7 +22,6 @@
#include <assert.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <iostream>
#include "common/snapException.h"
#include "common/log.h"

View file

@ -184,7 +184,7 @@ void Controller::worker()
if (reply)
{
TimeProvider::getInstance().setDiff(reply->latency, reply->received - reply->sent);
usleep(100);
chronos::usleep(100);
}
}
logO << "diff to server [ms]: " << (float)TimeProvider::getInstance().getDiffToServer<chronos::usec>().count() / 1000.f << "\n";
@ -193,7 +193,7 @@ void Controller::worker()
{
for (size_t n=0; n<10 && active_; ++n)
{
usleep(100*1000);
chronos::sleep(100);
if (asyncException_)
throw AsyncSnapException(exception_);
}
@ -211,7 +211,7 @@ void Controller::worker()
stream_.reset();
decoder_.reset();
for (size_t n=0; (n<10) && active_; ++n)
usleep(100*1000);
chronos::sleep(100);
}
}
logD << "Thread stopped\n";

View file

@ -211,7 +211,7 @@ void AlsaPlayer::worker()
catch (const std::exception& e)
{
logE << "Exception in initAlsa: " << e.what() << endl;
usleep(100*1000);
chronos::sleep(100);
}
}

View file

@ -16,7 +16,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include <unistd.h>
#include "coreAudioPlayer.h"
#define NUM_BUFFERS 2

View file

@ -16,7 +16,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include <unistd.h>
#include <assert.h>
#include <iostream>
@ -122,7 +121,7 @@ void OpenslPlayer::playerCallback(SLAndroidSimpleBufferQueueItf bq)
{
SLresult result = (*bq)->Enqueue(bq, buffer[curBuffer], buff_size);
if (result == SL_RESULT_BUFFER_INSUFFICIENT)
usleep(1000);
chronos::sleep(1);
else
break;
}

View file

@ -191,7 +191,7 @@ int main (int argc, char **argv)
{
logS(kLogErr) << "Exception: " << e.what() << std::endl;
}
usleep(500*1000);
chronos::sleep(500);
}
#endif
}
@ -202,7 +202,7 @@ int main (int argc, char **argv)
logO << "Latency: " << latency << "\n";
controller->start(pcmDevice, host, port, latency);
while(!g_terminated)
usleep(100*1000);
chronos::sleep(100);
controller->stop();
}
}

View file

@ -20,6 +20,7 @@
#define TIME_DEFS_H
#include <chrono>
#include <thread>
#include <sys/time.h>
#ifdef MACOS
#include <mach/clock.h>
@ -78,6 +79,27 @@ namespace chronos
{
return std::chrono::duration_cast<ToDuration>(d).count();
}
/// some sleep functions. Just for convenience.
template< class Rep, class Period >
inline void sleep(const std::chrono::duration<Rep, Period>& sleep_duration)
{
std::this_thread::sleep_for(sleep_duration);
}
inline void sleep(const int32_t& milliseconds)
{
if (milliseconds < 0)
return;
sleep(msec(milliseconds));
}
inline void usleep(const int32_t& microseconds)
{
if (microseconds < 0)
return;
sleep(usec(microseconds));
}
}

View file

@ -164,7 +164,7 @@ int main(int argc, char* argv[])
std::thread t(func, &io_service);
while (!g_terminated)
usleep(100*1000);
chronos::sleep(100);
io_service.stop();
t.join();

View file

@ -19,7 +19,6 @@
#include <memory>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "fileStream.h"
#include "encoder/encoderFactory.h"
@ -92,7 +91,7 @@ void FileStream::worker()
if (nextTick >= currentTick)
{
// logO << "sleep: " << nextTick - currentTick << "\n";
usleep((nextTick - currentTick) * 1000);
chronos::sleep(nextTick - currentTick);
}
else
{

View file

@ -19,7 +19,6 @@
#include <memory>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "encoder/encoderFactory.h"
#include "common/snapException.h"

View file

@ -90,7 +90,7 @@ void PipeStream::worker()
if (count < 0)
{
setState(kIdle);
usleep(100*1000);
chronos::sleep(100);
}
else if (count == 0)
throw SnapException("end of file");
@ -108,7 +108,7 @@ void PipeStream::worker()
{
// logO << "sleep: " << nextTick - currentTick << "\n";
setState(kPlaying);
usleep((nextTick - currentTick) * 1000);
chronos::sleep(nextTick - currentTick);
}
else
{
@ -122,7 +122,7 @@ void PipeStream::worker()
catch(const std::exception& e)
{
logE << "Exception: " << e.what() << std::endl;
usleep(100*1000);
chronos::sleep(100);
}
}
}

View file

@ -16,7 +16,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include <unistd.h>
#include "processStream.h"
#include "common/log.h"
@ -67,7 +66,7 @@ void ProcessStream::worker()
if (nextTick >= currentTick)
{
usleep((nextTick - currentTick) * 1000);
chronos::sleep(nextTick - currentTick);
}
else
{