Skip to content

Commit 161f1e3

Browse files
authored
Merge pull request #198 from toxieainc/sleep_tweak
improve SuperSleepUntil implementation
2 parents 6da55ab + 518e3df commit 161f1e3

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

Src/Network/SimNetBoard.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
** with Supermodel. If not, see <http://www.gnu.org/licenses/>.
2121
**/
2222

23-
#include <chrono>
2423
#include <thread>
2524
#include "Supermodel.h"
2625
#include "SimNetBoard.h"
26+
#include <OSD/Thread.h>
2727

2828
// these make 16-bit read/writes much neater
2929
#define RAM16 *(uint16_t*)&RAM
@@ -527,8 +527,6 @@ void CSimNetBoard::GetGame(const Game& gameInfo)
527527

528528
void CSimNetBoard::ConnectProc(void)
529529
{
530-
using namespace std::chrono_literals;
531-
532530
if (m_connected)
533531
return;
534532

@@ -546,7 +544,7 @@ void CSimNetBoard::ConnectProc(void)
546544
{
547545
if (m_quit)
548546
return;
549-
std::this_thread::sleep_for(1ms);
547+
CThread::Sleep(1);
550548
}
551549

552550
printf("Successfully connected.\n");
@@ -624,4 +622,4 @@ void CSimNetBoard::WriteIORegister(unsigned reg, uint16_t data)
624622
default:
625623
ErrorLog("write to unknown IO register 0x%02x", reg);
626624
}
627-
}
625+
}

Src/Network/TCPReceive.cpp

+4-8
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222

2323
#include "TCPReceive.h"
2424
#include "OSD/Logger.h"
25-
#include <chrono>
26-
27-
using namespace std::chrono_literals;
25+
#include "OSD/Thread.h"
2826

2927
#if defined(_DEBUG)
3028
#include <stdio.h>
@@ -98,9 +96,7 @@ std::vector<char>& TCPReceive::Receive()
9896
}
9997

10098
int size = 0;
101-
int result = 0;
102-
103-
result = SDLNet_TCP_Recv(m_receiveSocket, &size, sizeof(int));
99+
int result = SDLNet_TCP_Recv(m_receiveSocket, &size, sizeof(int));
104100
DPRINTF("Received %i bytes\n", result);
105101
if (result <= 0) {
106102
SDLNet_TCP_Close(m_receiveSocket);
@@ -130,7 +126,7 @@ void TCPReceive::ListenFunc()
130126
{
131127
while (m_running) {
132128

133-
std::this_thread::sleep_for(16ms);
129+
CThread::Sleep(16);
134130
if (m_receiveSocket) continue;
135131

136132
auto socket = SDLNet_TCP_Accept(m_listenSocket);
@@ -156,4 +152,4 @@ void TCPReceive::ListenFunc()
156152
bool TCPReceive::Connected()
157153
{
158154
return (m_receiveSocket != 0);
159-
}
155+
}

Src/OSD/SDL/Main.cpp

+17-14
Original file line numberDiff line numberDiff line change
@@ -873,33 +873,36 @@ static uint64_t GetDesiredRefreshRateMilliHz()
873873
return refreshRateMilliHz;
874874
}
875875

876-
static void SuperSleepUntil(uint64_t target)
876+
static void SuperSleepUntil(const uint64_t target)
877877
{
878878
uint64_t time = SDL_GetPerformanceCounter();
879879

880880
// If we're ahead of the target, we're done
881-
if (time > target)
881+
if (time >= target)
882882
{
883883
return;
884884
}
885885

886-
// Compute the whole number of millis to sleep. Because OS sleep is not accurate,
887-
// we actually sleep for one less and will spin-wait for the final millisecond.
888-
int32_t numWholeMillisToSleep = int32_t((target - time) * 1000 / s_perfCounterFrequency);
889-
numWholeMillisToSleep -= 1;
890-
if (numWholeMillisToSleep > 0)
886+
// Because OS sleep is not accurate,
887+
// we actually sleep until a maximum of 2 milliseconds are left.
888+
while (int64_t(target - time) * 1000 > 2 * int64_t(s_perfCounterFrequency))
891889
{
892-
SDL_Delay(numWholeMillisToSleep);
890+
SDL_Delay(1);
891+
time = SDL_GetPerformanceCounter();
893892
}
894893

895894
// Spin until requested time
896-
volatile uint64_t now;
897-
int32_t remain;
895+
int64_t remain;
898896
do
899897
{
900-
now = SDL_GetPerformanceCounter();
901-
remain = int32_t((target - now));
902-
} while (remain>0);
898+
// according to all available processor documentation for x86 and arm,
899+
// spinning should pause the processor for a short while for better
900+
// power efficiency and (surprisingly) overall faster system performance
901+
#ifdef SDL_CPUPauseInstruction
902+
SDL_CPUPauseInstruction();
903+
#endif
904+
remain = target - SDL_GetPerformanceCounter();
905+
} while (remain > 0);
903906
}
904907

905908

@@ -977,7 +980,7 @@ int Supermodel(const Game &game, ROMSet *rom_set, IEmulator *Model3, CInputs *In
977980
if (gameHasLightguns)
978981
videoInputs = Inputs;
979982
else
980-
videoInputs = NULL;
983+
videoInputs = nullptr;
981984

982985
// Attach the inputs to the emulator
983986
Model3->AttachInputs(Inputs);

0 commit comments

Comments
 (0)