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

125 lines
2.7 KiB
C
Raw Normal View History

2020-12-05 13:08:24 +01:00
#pragma once
2014-08-08 17:55:12 +02:00
2016-04-14 00:23:53 +02:00
#include "sys_sync.h"
#include "Emu/Memory/vm_ptr.h"
2014-08-08 17:55:12 +02:00
enum
{
SYS_SYNC_WAITER_SINGLE = 0x10000,
SYS_SYNC_WAITER_MULTIPLE = 0x20000,
SYS_EVENT_FLAG_WAIT_AND = 0x01,
SYS_EVENT_FLAG_WAIT_OR = 0x02,
SYS_EVENT_FLAG_WAIT_CLEAR = 0x10,
SYS_EVENT_FLAG_WAIT_CLEAR_ALL = 0x20,
};
2015-07-09 17:30:37 +02:00
struct sys_event_flag_attribute_t
2014-08-08 17:55:12 +02:00
{
be_t<u32> protocol;
be_t<u32> pshared;
be_t<u64> ipc_key;
2014-12-24 17:09:32 +01:00
be_t<s32> flags;
be_t<s32> type;
2015-03-05 22:29:05 +01:00
2014-12-24 17:09:32 +01:00
union
{
nse_t<u64, 1> name_u64;
char name[sizeof(u64)];
2014-12-24 17:09:32 +01:00
};
2014-08-08 17:55:12 +02:00
};
struct lv2_event_flag final : lv2_obj
2014-08-08 17:55:12 +02:00
{
2017-01-25 18:50:30 +01:00
static const u32 id_base = 0x98000000;
2020-06-09 17:35:14 +02:00
const lv2_protocol protocol;
2017-02-03 00:16:09 +01:00
const u32 shared;
const u64 key;
const s32 flags;
const s32 type;
2014-12-24 17:09:32 +01:00
const u64 name;
2014-08-08 17:55:12 +02:00
shared_mutex mutex;
2017-02-03 17:27:03 +01:00
atomic_t<u32> waiters{0};
2016-04-14 00:23:53 +02:00
atomic_t<u64> pattern;
2017-02-03 17:27:03 +01:00
std::deque<cpu_thread*> sq;
2015-03-05 22:29:05 +01:00
2017-07-24 17:59:48 +02:00
lv2_event_flag(u32 protocol, u32 shared, u64 key, s32 flags, s32 type, u64 name, u64 pattern)
2020-06-09 17:35:14 +02:00
: protocol{protocol}
2017-07-24 17:59:48 +02:00
, shared(shared)
, key(key)
, flags(flags)
2014-12-23 00:31:11 +01:00
, type(type)
2014-12-24 17:09:32 +01:00
, name(name)
2017-02-03 00:16:09 +01:00
, pattern(pattern)
2014-08-08 17:55:12 +02:00
{
}
2015-07-19 23:29:40 +02:00
2017-02-03 17:27:03 +01:00
// Check mode arg
static bool check_mode(u32 mode)
2015-07-19 23:29:40 +02:00
{
switch (mode & 0xf)
{
case SYS_EVENT_FLAG_WAIT_AND: break;
case SYS_EVENT_FLAG_WAIT_OR: break;
default: return false;
}
switch (mode & ~0xf)
{
case 0: break;
case SYS_EVENT_FLAG_WAIT_CLEAR: break;
case SYS_EVENT_FLAG_WAIT_CLEAR_ALL: break;
default: return false;
}
return true;
}
2017-02-03 17:27:03 +01:00
// Check and clear pattern (must be atomic op)
static bool check_pattern(u64& pattern, u64 bitptn, u64 mode, u64* result)
2015-07-19 23:29:40 +02:00
{
2017-02-03 17:27:03 +01:00
// Write pattern
if (result)
2015-07-19 23:29:40 +02:00
{
2017-02-03 17:27:03 +01:00
*result = pattern;
2015-07-19 23:29:40 +02:00
}
2017-02-03 17:27:03 +01:00
// Check pattern
2017-11-02 07:28:40 +01:00
if (((mode & 0xf) == SYS_EVENT_FLAG_WAIT_AND && (pattern & bitptn) != bitptn) ||
((mode & 0xf) == SYS_EVENT_FLAG_WAIT_OR && (pattern & bitptn) == 0))
2015-07-19 23:29:40 +02:00
{
2017-02-03 17:27:03 +01:00
return false;
2015-07-19 23:29:40 +02:00
}
2017-02-03 17:27:03 +01:00
// Clear pattern if necessary
2015-07-19 23:29:40 +02:00
if ((mode & ~0xf) == SYS_EVENT_FLAG_WAIT_CLEAR)
{
2017-02-03 17:27:03 +01:00
pattern &= ~bitptn;
2015-07-19 23:29:40 +02:00
}
else if ((mode & ~0xf) == SYS_EVENT_FLAG_WAIT_CLEAR_ALL)
{
2017-02-03 17:27:03 +01:00
pattern = 0;
2015-07-19 23:29:40 +02:00
}
2017-02-03 17:27:03 +01:00
return true;
}
2014-08-08 17:55:12 +02:00
};
2015-07-19 23:29:40 +02:00
// Aux
class ppu_thread;
2015-07-19 23:29:40 +02:00
2017-02-03 17:27:03 +01:00
// Syscalls
error_code sys_event_flag_create(ppu_thread& ppu, vm::ptr<u32> id, vm::ptr<sys_event_flag_attribute_t> attr, u64 init);
error_code sys_event_flag_destroy(ppu_thread& ppu, u32 id);
2018-02-09 15:49:37 +01:00
error_code sys_event_flag_wait(ppu_thread& ppu, u32 id, u64 bitptn, u32 mode, vm::ptr<u64> result, u64 timeout);
error_code sys_event_flag_trywait(ppu_thread& ppu, u32 id, u64 bitptn, u32 mode, vm::ptr<u64> result);
error_code sys_event_flag_set(cpu_thread& cpu, u32 id, u64 bitptn);
error_code sys_event_flag_clear(ppu_thread& ppu, u32 id, u64 bitptn);
2018-02-09 15:49:37 +01:00
error_code sys_event_flag_cancel(ppu_thread& ppu, u32 id, vm::ptr<u32> num);
error_code sys_event_flag_get(ppu_thread& ppu, u32 id, vm::ptr<u64> flags);