2019-06-26 08:52:34 +02:00
|
|
|
// Copyright 2019 yuzu emulator team
|
2019-06-25 16:45:53 +02:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
2019-12-22 23:49:51 +01:00
|
|
|
#include "common/uuid.h"
|
2020-12-31 08:01:08 +01:00
|
|
|
#include "core/hle/kernel/k_thread.h"
|
2019-06-25 16:45:53 +02:00
|
|
|
#include "core/hle/kernel/shared_memory.h"
|
2019-12-22 23:49:51 +01:00
|
|
|
#include "core/hle/service/time/clock_types.h"
|
2019-06-25 16:45:53 +02:00
|
|
|
|
|
|
|
namespace Service::Time {
|
2019-12-22 23:49:51 +01:00
|
|
|
|
|
|
|
class SharedMemory final {
|
2019-06-25 16:45:53 +02:00
|
|
|
public:
|
2019-06-26 08:52:34 +02:00
|
|
|
explicit SharedMemory(Core::System& system);
|
2019-06-25 16:45:53 +02:00
|
|
|
~SharedMemory();
|
|
|
|
|
|
|
|
// Return the shared memory handle
|
2019-11-25 02:15:51 +01:00
|
|
|
std::shared_ptr<Kernel::SharedMemory> GetSharedMemoryHolder() const;
|
2019-06-25 16:45:53 +02:00
|
|
|
|
|
|
|
// TODO(ogniK): We have to properly simulate memory barriers, how are we going to do this?
|
2019-06-26 08:52:34 +02:00
|
|
|
template <typename T, std::size_t Offset>
|
2019-06-25 16:45:53 +02:00
|
|
|
struct MemoryBarrier {
|
2019-12-22 23:49:51 +01:00
|
|
|
static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable");
|
2019-06-25 16:45:53 +02:00
|
|
|
u32_le read_attempt{};
|
2019-06-26 08:52:34 +02:00
|
|
|
std::array<T, 2> data{};
|
2019-06-25 16:45:53 +02:00
|
|
|
|
|
|
|
// These are not actually memory barriers at the moment as we don't have multicore and all
|
|
|
|
// HLE is mutexed. This will need to properly be implemented when we start updating the time
|
|
|
|
// points on threads. As of right now, we'll be updated both values synchronously and just
|
|
|
|
// incrementing the read_attempt to indicate that we waited.
|
2019-06-26 08:52:34 +02:00
|
|
|
void StoreData(u8* shared_memory, T data_to_store) {
|
|
|
|
std::memcpy(this, shared_memory + Offset, sizeof(*this));
|
2019-06-25 16:45:53 +02:00
|
|
|
read_attempt++;
|
|
|
|
data[read_attempt & 1] = data_to_store;
|
2019-06-26 08:52:34 +02:00
|
|
|
std::memcpy(shared_memory + Offset, this, sizeof(*this));
|
2019-06-25 16:45:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// For reading we're just going to read the last stored value. If there was no value stored
|
|
|
|
// it will just end up reading an empty value as intended.
|
2019-06-26 08:52:34 +02:00
|
|
|
T ReadData(u8* shared_memory) {
|
|
|
|
std::memcpy(this, shared_memory + Offset, sizeof(*this));
|
2019-06-25 16:45:53 +02:00
|
|
|
return data[(read_attempt - 1) & 1];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Shared memory format
|
|
|
|
struct Format {
|
2019-12-22 23:49:51 +01:00
|
|
|
MemoryBarrier<Clock::SteadyClockContext, 0x0> standard_steady_clock_timepoint;
|
|
|
|
MemoryBarrier<Clock::SystemClockContext, 0x38> standard_local_system_clock_context;
|
|
|
|
MemoryBarrier<Clock::SystemClockContext, 0x80> standard_network_system_clock_context;
|
2019-06-26 08:52:34 +02:00
|
|
|
MemoryBarrier<bool, 0xc8> standard_user_system_clock_automatic_correction;
|
2019-06-25 16:45:53 +02:00
|
|
|
u32_le format_version;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(Format) == 0xd8, "Format is an invalid size");
|
|
|
|
|
2019-12-22 23:49:51 +01:00
|
|
|
void SetupStandardSteadyClock(Core::System& system, const Common::UUID& clock_source_id,
|
|
|
|
Clock::TimeSpanType currentTimePoint);
|
|
|
|
void UpdateLocalSystemClockContext(const Clock::SystemClockContext& context);
|
|
|
|
void UpdateNetworkSystemClockContext(const Clock::SystemClockContext& context);
|
|
|
|
void SetAutomaticCorrectionEnabled(bool is_enabled);
|
|
|
|
|
2019-06-25 16:45:53 +02:00
|
|
|
private:
|
2019-12-22 23:49:51 +01:00
|
|
|
std::shared_ptr<Kernel::SharedMemory> shared_memory_holder;
|
2019-06-25 16:45:53 +02:00
|
|
|
Core::System& system;
|
2019-06-26 08:52:34 +02:00
|
|
|
Format shared_memory_format{};
|
2019-06-25 16:45:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Service::Time
|