polserver/pol-core/pol/polclock.cpp
turleypol 21577d8e5b
Clang Tidy concat namespaces (#855)
* tidy

* Automated clang-tidy change: modernize-concat-nested-namespaces

* compile test

* fix namespace

---------

Co-authored-by: Clang Tidy <clang-tidy@users.noreply.github.com>
2026-01-17 10:30:21 +01:00

117 lines
2.7 KiB
C++

/** @file
*
* @par History
*/
#include "polclock.h"
#include <atomic>
#include <chrono>
#include <thread>
#include "../clib/logfacility.h"
#include "../clib/spinlock.h"
namespace Pol::Core
{
using namespace std::chrono_literals;
static PolClock::time_point polclock_base = PolClock::time_point( PolClock::duration( 0 ) );
static PolClock::time_point poltime_base = PolClock::time_point( PolClock::duration( 0 ) );
static PolClock::time_point poltime_paused_at = PolClock::time_point( PolClock::duration( 0 ) );
static PolClock::time_point polclock_paused_at = PolClock::time_point( PolClock::duration( 0 ) );
static Clib::SpinLock polclock_lock;
static std::chrono::milliseconds unittest_shift = 0ms;
void pol_sleep_ms( unsigned int millis )
{
std::this_thread::sleep_for( std::chrono::milliseconds( millis ) );
}
void start_polclock()
{
Clib::SpinLockGuard guard( polclock_lock );
polclock_base = PolClock::now();
}
void pause_polclock()
{
Clib::SpinLockGuard guard( polclock_lock );
polclock_paused_at = PolClock::now();
}
void restart_polclock()
{
Clib::SpinLockGuard guard( polclock_lock );
polclock_base += PolClock::now() - polclock_paused_at;
polclock_paused_at = PolClock::time_point( PolClock::duration( 0 ) );
}
polclock_t polclock()
{
Clib::SpinLockGuard guard( polclock_lock );
return std::chrono::duration_cast<polclock_t_unit>( PolClock::now() - polclock_base +
unittest_shift )
.count() /
10;
}
void shift_clock_for_unittest( std::chrono::milliseconds milli )
{
Clib::SpinLockGuard guard( polclock_lock );
unittest_shift += milli;
}
void start_poltime()
{
Clib::SpinLockGuard guard( polclock_lock );
poltime_base = PolClock::now();
}
void pause_poltime()
{
Clib::SpinLockGuard guard( polclock_lock );
poltime_paused_at = PolClock::now();
}
void restart_poltime()
{
Clib::SpinLockGuard guard( polclock_lock );
poltime_base += PolClock::now() - poltime_paused_at;
}
poltime_t poltime()
{
Clib::SpinLockGuard guard( polclock_lock );
return std::chrono::duration_cast<poltime_t_unit>( PolClock::now() - poltime_base +
unittest_shift )
.count();
}
void start_pol_clocks()
{
static bool inited = false;
if ( !inited )
{
inited = true;
start_polclock();
start_poltime();
}
}
void pause_pol_clocks()
{
pause_polclock();
pause_poltime();
}
void restart_pol_clocks()
{
restart_polclock();
restart_poltime();
}
bool is_polclock_paused_at_zero()
{
Clib::SpinLockGuard guard( polclock_lock );
return polclock_paused_at == PolClock::time_point( PolClock::duration( 0 ) );
}
} // namespace Pol::Core