Replacing the tick count timer with xplat abstraction (also better rate).

Fixes #346.
This commit is contained in:
Ben Vanik 2015-07-19 10:11:54 -07:00
parent 9bcc4e046c
commit edfa3f3fc0
7 changed files with 69 additions and 20 deletions

@ -1 +1 @@
Subproject commit 27df20c41e9525db6f67e57cb4161bdc8e82c117
Subproject commit c21b5b8ab64e69a3aaed0cde0618e75f12778059

View File

@ -36,6 +36,8 @@ uint64_t Clock::QueryHostSystemTime() {
return (uint64_t(t.dwHighDateTime) << 32) | t.dwLowDateTime;
}
uint32_t Clock::QueryHostUptimeMillis() { return ::GetTickCount(); }
uint32_t Clock::QueryHostUptimeMillis() {
return uint32_t(QueryHostTickCount() / (host_tick_frequency() / 1000));
}
} // namespace xe

View File

@ -97,6 +97,20 @@ bool FreeTlsHandle(TlsHandle handle);
uintptr_t GetTlsValue(TlsHandle handle);
bool SetTlsValue(TlsHandle handle, uintptr_t value);
// A high-resolution timer capable of firing at millisecond-precision.
// All timers created in this way are executed in the same thread so
// callbacks must be kept short or else all timers will be impacted.
class HighResolutionTimer {
public:
virtual ~HighResolutionTimer() = default;
// Creates a new repeating timer with the given period.
// The given function will be called back as close to the given period as
// possible.
static std::unique_ptr<HighResolutionTimer> CreateRepeating(
std::chrono::milliseconds period, std::function<void()> callback);
};
// Results for a WaitHandle operation.
enum class WaitResult {
// The state of the specified object is signaled.

View File

@ -108,6 +108,44 @@ bool SetTlsValue(TlsHandle handle, uintptr_t value) {
return TlsSetValue(handle, reinterpret_cast<void*>(value)) ? true : false;
}
class Win32HighResolutionTimer : public HighResolutionTimer {
public:
Win32HighResolutionTimer(std::function<void()> callback)
: callback_(callback) {}
~Win32HighResolutionTimer() override {
if (handle_) {
DeleteTimerQueueTimer(nullptr, handle_, INVALID_HANDLE_VALUE);
handle_ = nullptr;
}
}
bool Initialize(std::chrono::milliseconds period) {
return CreateTimerQueueTimer(
&handle_, nullptr,
[](PVOID param, BOOLEAN timer_or_wait_fired) {
auto timer =
reinterpret_cast<Win32HighResolutionTimer*>(param);
timer->callback_();
},
this, 0, DWORD(period.count()), WT_EXECUTEINTIMERTHREAD)
? true
: false;
}
private:
HANDLE handle_ = nullptr;
std::function<void()> callback_;
};
std::unique_ptr<HighResolutionTimer> HighResolutionTimer::CreateRepeating(
std::chrono::milliseconds period, std::function<void()> callback) {
auto timer = std::make_unique<Win32HighResolutionTimer>(std::move(callback));
if (!timer->Initialize(period)) {
return nullptr;
}
return std::unique_ptr<HighResolutionTimer>(timer.release());
}
template <typename T>
class Win32Handle : public T {
public:

View File

@ -14,7 +14,6 @@
#include "xenia/base/clock.h"
#include "xenia/base/logging.h"
#include "xenia/base/math.h"
#include "xenia/base/platform_win.h"
#include "xenia/emulator.h"
#include "xenia/kernel/kernel_state.h"
#include "xenia/kernel/xboxkrnl_private.h"
@ -131,16 +130,11 @@ XboxkrnlModule::XboxkrnlModule(Emulator* emulator, KernelState* kernel_state)
xe::store_and_swap<uint32_t>(lpKeTimeStampBundle + 16,
Clock::QueryGuestUptimeMillis());
xe::store_and_swap<uint32_t>(lpKeTimeStampBundle + 20, 0);
CreateTimerQueueTimer(
&timestamp_timer_, nullptr,
[](PVOID param, BOOLEAN timer_or_wait_fired) {
auto timestamp_bundle = reinterpret_cast<uint8_t*>(param);
xe::store_and_swap<uint32_t>(timestamp_bundle + 16,
timestamp_timer_ = xe::threading::HighResolutionTimer::CreateRepeating(
std::chrono::milliseconds(1), [lpKeTimeStampBundle]() {
xe::store_and_swap<uint32_t>(lpKeTimeStampBundle + 16,
Clock::QueryGuestUptimeMillis());
},
lpKeTimeStampBundle, 0,
1, // 1ms
WT_EXECUTEINTIMERTHREAD);
});
}
std::vector<xe::cpu::Export*> xboxkrnl_exports(4096);
@ -171,9 +165,7 @@ void XboxkrnlModule::RegisterExportTable(
export_resolver->RegisterTable("xboxkrnl.exe", &xboxkrnl_exports);
}
XboxkrnlModule::~XboxkrnlModule() {
DeleteTimerQueueTimer(nullptr, timestamp_timer_, nullptr);
}
XboxkrnlModule::~XboxkrnlModule() = default;
int XboxkrnlModule::LaunchModule(const char* path) {
// Create and register the module. We keep it local to this function and

View File

@ -10,6 +10,9 @@
#ifndef XENIA_KERNEL_XBOXKRNL_MODULE_H_
#define XENIA_KERNEL_XBOXKRNL_MODULE_H_
#include <memory>
#include "xenia/base/threading.h"
#include "xenia/cpu/export_resolver.h"
#include "xenia/kernel/objects/xkernel_module.h"
#include "xenia/kernel/xboxkrnl_ordinals.h"
@ -17,9 +20,6 @@
// All of the exported functions:
#include "xenia/kernel/xboxkrnl_rtl.h"
// TODO(benvanik): switch timer.
typedef void* HANDLE;
namespace xe {
namespace kernel {
@ -35,7 +35,7 @@ class XboxkrnlModule : public XKernelModule {
int LaunchModule(const char* path);
private:
HANDLE timestamp_timer_;
std::unique_ptr<xe::threading::HighResolutionTimer> timestamp_timer_;
};
} // namespace kernel

5
xb.bat
View File

@ -40,7 +40,10 @@ IF %_RESULT% NEQ 0 (
ECHO this script again.
GOTO :exit_error
)
CALL %VS14_VCVARSALL% amd64
1>NUL 2>NUL CMD /c where devenv
IF %ERRORLEVEL% NEQ 0 (
CALL %VS14_VCVARSALL% amd64
)
SET CLANG_FORMAT=""
SET LLVM_CLANG_FORMAT="C:\Program Files (x86)\LLVM\bin\clang-format.exe"