rpcs3/rpcs3/Emu/Cell/lv2/sys_timer.h
Elad 575a245f8d
IDM: Implement lock-free smart pointers (#16403)
Replaces `std::shared_pointer` with `stx::atomic_ptr` and `stx::shared_ptr`.

Notes to programmers:

* This pr kills the use of `dynamic_cast`, `std::dynamic_pointer_cast` and `std::weak_ptr` on IDM objects, possible replacement is to save the object ID on the base object, then use idm::check/get_unlocked to the destination type via the saved ID which may be null. Null pointer check is how you can tell type mismatch (as dynamic cast) or object destruction (as weak_ptr locking).
* Double-inheritance on IDM objects should be used with care, `stx::shared_ptr` does not support constant-evaluated pointer offsetting to parent/child type.
* `idm::check/get_unlocked` can now be used anywhere.

Misc fixes:
* Fixes some segfaults with RPCN with interaction with IDM.
* Fix deadlocks in access violation handler due locking recursion.
* Fixes race condition in process exit-spawn on memory containers read.
* Fix bug that theoretically can prevent RPCS3 from booting - fix `id_manager::typeinfo` comparison to compare members instead of `memcmp` which can fail spuriously on padding bytes.
* Ensure all IDM inherited types of base, either has `id_base` or `id_type` defined locally, this allows to make getters such as `idm::get_unlocked<lv2_socket, lv2_socket_raw>()` which were broken before. (requires save-states invalidation)
* Removes broken operator[] overload of `stx::shared_ptr` and `stx::single_ptr` for non-array types.
2024-12-22 20:59:48 +02:00

79 lines
1.8 KiB
C++

#pragma once
#include "sys_event.h"
#include "Utilities/Thread.h"
#include "Emu/Memory/vm_ptr.h"
// Timer State
enum : u32
{
SYS_TIMER_STATE_STOP = 0,
SYS_TIMER_STATE_RUN = 1,
};
struct sys_timer_information_t
{
be_t<u64> next_expire; // system_time_t
be_t<u64> period;
be_t<u32> timer_state;
be_t<u32> pad;
};
struct lv2_timer : lv2_obj
{
static const u32 id_base = 0x11000000;
shared_mutex mutex;
atomic_t<u32> state{SYS_TIMER_STATE_STOP};
shared_ptr<lv2_event_queue> port;
u64 source;
u64 data1;
u64 data2;
atomic_t<u64> expire{0}; // Next expiration time
atomic_t<u64> period{0}; // Period (oneshot if 0)
u64 check(u64 _now) noexcept;
u64 check_unlocked(u64 _now) noexcept;
lv2_timer() noexcept
: lv2_obj(1)
{
}
void get_information(sys_timer_information_t& info) const
{
if (state == SYS_TIMER_STATE_RUN)
{
info.timer_state = state;
info.next_expire = expire;
info.period = period;
}
else
{
info.timer_state = SYS_TIMER_STATE_STOP;
info.next_expire = 0;
info.period = 0;
}
}
lv2_timer(utils::serial& ar);
void save(utils::serial& ar);
};
class ppu_thread;
// Syscalls
error_code sys_timer_create(ppu_thread&, vm::ptr<u32> timer_id);
error_code sys_timer_destroy(ppu_thread&, u32 timer_id);
error_code sys_timer_get_information(ppu_thread&, u32 timer_id, vm::ptr<sys_timer_information_t> info);
error_code _sys_timer_start(ppu_thread&, u32 timer_id, u64 basetime, u64 period); // basetime type changed from s64
error_code sys_timer_stop(ppu_thread&, u32 timer_id);
error_code sys_timer_connect_event_queue(ppu_thread&, u32 timer_id, u32 queue_id, u64 name, u64 data1, u64 data2);
error_code sys_timer_disconnect_event_queue(ppu_thread&, u32 timer_id);
error_code sys_timer_sleep(ppu_thread&, u32 sleep_time);
error_code sys_timer_usleep(ppu_thread&, u64 sleep_time);