Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve SuperSleepUntil implementation #198

Merged
merged 3 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Src/Network/SimNetBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
** with Supermodel. If not, see <http://www.gnu.org/licenses/>.
**/

#include <chrono>
#include <thread>
#include "Supermodel.h"
#include "SimNetBoard.h"
#include <OSD/Thread.h>

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

void CSimNetBoard::ConnectProc(void)
{
using namespace std::chrono_literals;

if (m_connected)
return;

Expand All @@ -546,7 +544,7 @@ void CSimNetBoard::ConnectProc(void)
{
if (m_quit)
return;
std::this_thread::sleep_for(1ms);
CThread::Sleep(1);
}

printf("Successfully connected.\n");
Expand Down Expand Up @@ -624,4 +622,4 @@ void CSimNetBoard::WriteIORegister(unsigned reg, uint16_t data)
default:
ErrorLog("write to unknown IO register 0x%02x", reg);
}
}
}
12 changes: 4 additions & 8 deletions Src/Network/TCPReceive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@

#include "TCPReceive.h"
#include "OSD/Logger.h"
#include <chrono>

using namespace std::chrono_literals;
#include "OSD/Thread.h"

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

int size = 0;
int result = 0;

result = SDLNet_TCP_Recv(m_receiveSocket, &size, sizeof(int));
int result = SDLNet_TCP_Recv(m_receiveSocket, &size, sizeof(int));
DPRINTF("Received %i bytes\n", result);
if (result <= 0) {
SDLNet_TCP_Close(m_receiveSocket);
Expand Down Expand Up @@ -130,7 +126,7 @@ void TCPReceive::ListenFunc()
{
while (m_running) {

std::this_thread::sleep_for(16ms);
CThread::Sleep(16);
if (m_receiveSocket) continue;

auto socket = SDLNet_TCP_Accept(m_listenSocket);
Expand All @@ -156,4 +152,4 @@ void TCPReceive::ListenFunc()
bool TCPReceive::Connected()
{
return (m_receiveSocket != 0);
}
}
31 changes: 17 additions & 14 deletions Src/OSD/SDL/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -873,33 +873,36 @@ static uint64_t GetDesiredRefreshRateMilliHz()
return refreshRateMilliHz;
}

static void SuperSleepUntil(uint64_t target)
static void SuperSleepUntil(const uint64_t target)
{
uint64_t time = SDL_GetPerformanceCounter();

// If we're ahead of the target, we're done
if (time > target)
if (time >= target)
{
return;
}

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

// Spin until requested time
volatile uint64_t now;
int32_t remain;
int64_t remain;
do
{
now = SDL_GetPerformanceCounter();
remain = int32_t((target - now));
} while (remain>0);
// according to all available processor documentation for x86 and arm,
// spinning should pause the processor for a short while for better
// power efficiency and (surprisingly) overall faster system performance
#ifdef SDL_CPUPauseInstruction
SDL_CPUPauseInstruction();
#endif
remain = target - SDL_GetPerformanceCounter();
} while (remain > 0);
}


Expand Down Expand Up @@ -977,7 +980,7 @@ int Supermodel(const Game &game, ROMSet *rom_set, IEmulator *Model3, CInputs *In
if (gameHasLightguns)
videoInputs = Inputs;
else
videoInputs = NULL;
videoInputs = nullptr;

// Attach the inputs to the emulator
Model3->AttachInputs(Inputs);
Expand Down