Skip to content

Commit

Permalink
Update some usleep() to std::chrono
Browse files Browse the repository at this point in the history
Ideally this fixes some issues on windows where usleep
appears to misfunction, but if not there will be another
commit for lower resolution systems.
  • Loading branch information
fundamental committed Jul 23, 2017
1 parent 879fc02 commit 144086d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/Misc/MiddleWare.cpp
Expand Up @@ -48,6 +48,8 @@
#include <string>
#include <future>
#include <atomic>
#include <chrono>
#include <thread>
#include <list>

#define errx(...) {}
Expand Down Expand Up @@ -1554,14 +1556,15 @@ MiddleWareImpl::~MiddleWareImpl(void)

void MiddleWareImpl::doReadOnlyOp(std::function<void()> read_only_fn)
{
using namespace std::chrono;
assert(uToB);
uToB->write("/freeze_state","");

std::list<const char *> fico;
int tries = 0;
while(tries++ < 10000) {
if(!bToU->hasNext()) {
usleep(500);
std::this_thread::sleep_for(microseconds(500));
continue;
}
const char *msg = bToU->read();
Expand Down Expand Up @@ -1667,14 +1670,15 @@ void MiddleWareImpl::doReadOnlyOpPlugin(std::function<void()> read_only_fn)

bool MiddleWareImpl::doReadOnlyOpNormal(std::function<void()> read_only_fn, bool canfail)
{
using namespace std::chrono;
assert(uToB);
uToB->write("/freeze_state","");

std::list<const char *> fico;
int tries = 0;
while(tries++ < 2000) {
if(!bToU->hasNext()) {
usleep(500);
std::this_thread::sleep_for(microseconds(500));
continue;
}
const char *msg = bToU->read();
Expand Down
6 changes: 4 additions & 2 deletions src/Nio/NulEngine.cpp
Expand Up @@ -14,7 +14,8 @@
#include "NulEngine.h"
#include "../globals.h"

#include <unistd.h>
#include <thread>
#include <chrono>
#include <iostream>
using namespace std;

Expand All @@ -35,6 +36,7 @@ void *NulEngine::_AudioThread(void *arg)

void *NulEngine::AudioThread()
{
using namespace std::chrono;
while(pThread) {
getNext();

Expand All @@ -50,7 +52,7 @@ void *NulEngine::AudioThread()
+ (playing_until.tv_sec - now.tv_sec) * 1000000;
if(remaining > 10000) //Don't sleep() less than 10ms.
//This will add latency...
usleep(remaining - 10000);
std::this_thread::sleep_for(std::chrono::microseconds(remaining - 10000));
if(remaining < 0)
cerr << "WARNING - too late" << endl;
}
Expand Down

0 comments on commit 144086d

Please sign in to comment.