rpcsx/rpcs3/Emu/Cell/lv2/sys_lwmutex.h

118 lines
2.3 KiB
C
Raw Normal View History

2014-01-19 22:19:37 +01:00
#pragma once
2016-04-14 00:23:53 +02:00
#include "sys_sync.h"
#include "Emu/Memory/vm_ptr.h"
struct sys_lwmutex_attribute_t
{
2014-09-19 02:19:22 +02:00
be_t<u32> protocol;
be_t<u32> recursive;
2017-02-03 00:16:09 +01:00
union
{
nse_t<u64, 1> name_u64;
char name[sizeof(u64)];
2017-02-03 00:16:09 +01:00
};
};
2015-03-08 22:56:45 +01:00
enum : u32
{
lwmutex_free = 0xffffffffu,
lwmutex_dead = 0xfffffffeu,
lwmutex_reserved = 0xfffffffdu,
2015-03-08 22:56:45 +01:00
};
2014-01-19 22:19:37 +01:00
struct sys_lwmutex_t
{
2016-04-14 00:23:53 +02:00
struct alignas(8) sync_var_t
2015-03-08 22:56:45 +01:00
{
be_t<u32> owner;
be_t<u32> waiter;
};
union
{
atomic_t<sync_var_t> lock_var;
2015-03-08 22:56:45 +01:00
struct
{
atomic_be_t<u32> owner;
atomic_be_t<u32> waiter;
}
vars;
atomic_be_t<u64> all_info;
2015-03-08 22:56:45 +01:00
};
2018-02-09 15:49:37 +01:00
2014-01-19 22:19:37 +01:00
be_t<u32> attribute;
2015-03-08 22:56:45 +01:00
be_t<u32> recursive_count;
be_t<u32> sleep_queue; // lwmutex pseudo-id
2014-01-19 22:19:37 +01:00
be_t<u32> pad;
2015-03-08 22:56:45 +01:00
};
struct lv2_lwmutex final : lv2_obj
2015-03-08 22:56:45 +01:00
{
2017-01-25 18:50:30 +01:00
static const u32 id_base = 0x95000000;
2020-06-09 17:35:14 +02:00
const lv2_protocol protocol;
2018-02-09 15:49:37 +01:00
const vm::ptr<sys_lwmutex_t> control;
const be_t<u64> name;
2014-09-19 02:19:22 +02:00
shared_mutex mutex;
atomic_t<s32> signaled{0};
2017-02-03 00:16:09 +01:00
std::deque<cpu_thread*> sq;
atomic_t<s32> lwcond_waiters{0};
2018-02-09 15:49:37 +01:00
lv2_lwmutex(u32 protocol, vm::ptr<sys_lwmutex_t> control, u64 name)
2020-06-09 17:35:14 +02:00
: protocol{protocol}
2017-02-03 00:16:09 +01:00
, control(control)
, name(std::bit_cast<be_t<u64>>(name))
2015-03-08 22:56:45 +01:00
{
2015-03-10 22:47:13 +01:00
}
// Try to add a waiter
bool add_waiter(cpu_thread* cpu)
{
if (const auto old = lwcond_waiters.fetch_op([](s32& val)
{
2020-03-23 12:37:02 +01:00
if (val + 0u <= 1u << 31)
{
// Value was either positive or INT32_MIN
return false;
}
// lwmutex was set to be destroyed, but there are lwcond waiters
// Turn off the "destroying" bit as we are adding an lwmutex waiter
val &= 0x7fff'ffff;
return true;
}).first; old != INT32_MIN)
{
sq.emplace_back(cpu);
if (old < 0)
{
// Notify lwmutex destroyer (may cause EBUSY to be returned for it)
lwcond_waiters.notify_all();
}
return true;
}
// Failed - lwmutex was set to be destroyed and all lwcond waiters quit
return false;
}
};
// Aux
class ppu_thread;
2017-02-03 00:16:09 +01:00
// Syscalls
2019-06-20 13:42:06 +02:00
error_code _sys_lwmutex_create(ppu_thread& ppu, vm::ptr<u32> lwmutex_id, u32 protocol, vm::ptr<sys_lwmutex_t> control, s32 has_name, u64 name);
error_code _sys_lwmutex_destroy(ppu_thread& ppu, u32 lwmutex_id);
2017-02-03 00:16:09 +01:00
error_code _sys_lwmutex_lock(ppu_thread& ppu, u32 lwmutex_id, u64 timeout);
2019-06-20 13:42:06 +02:00
error_code _sys_lwmutex_trylock(ppu_thread& ppu, u32 lwmutex_id);
2017-02-06 19:36:46 +01:00
error_code _sys_lwmutex_unlock(ppu_thread& ppu, u32 lwmutex_id);
2019-06-20 13:42:06 +02:00
error_code _sys_lwmutex_unlock2(ppu_thread& ppu, u32 lwmutex_id);