LV2_LOCK removal
error_code introduction
This commit is contained in:
Nekotekina 2017-01-30 16:20:09 +03:00
commit dc7ac22f84
3 changed files with 86 additions and 94 deletions

View file

@ -120,10 +120,10 @@ public:
u32 prio = 0; // Thread priority (0..3071) u32 prio = 0; // Thread priority (0..3071)
const u32 stack_size; // Stack size const u32 stack_size; // Stack size
const u32 stack_addr; // Stack address const u32 stack_addr; // Stack address
bool is_joinable = true;
bool is_joining = false;
lf_fifo<atomic_t<cmd64>, 255> cmd_queue; // Command queue for asynchronous operations. atomic_t<u32> joiner{~0u}; // Joining thread (-1 if detached)
lf_fifo<atomic_t<cmd64>, 127> cmd_queue; // Command queue for asynchronous operations.
void cmd_push(cmd64); void cmd_push(cmd64);
void cmd_list(std::initializer_list<cmd64>); void cmd_list(std::initializer_list<cmd64>);

View file

@ -17,14 +17,10 @@ void _sys_ppu_thread_exit(ppu_thread& ppu, u64 errorcode)
{ {
sys_ppu_thread.warning("_sys_ppu_thread_exit(errorcode=0x%llx)", errorcode); sys_ppu_thread.warning("_sys_ppu_thread_exit(errorcode=0x%llx)", errorcode);
// TODO: shall sys_mutex objects be unlocked?
LV2_LOCK;
ppu.state += cpu_flag::exit; ppu.state += cpu_flag::exit;
// Delete detached thread // Delete detached thread
if (!ppu.is_joinable) if (ppu.joiner == -1)
{ {
idm::remove<ppu_thread>(ppu.id); idm::remove<ppu_thread>(ppu.id);
} }
@ -43,12 +39,10 @@ void sys_ppu_thread_yield()
std::this_thread::yield(); std::this_thread::yield();
} }
s32 sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ptr<u64> vptr) error_code sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ptr<u64> vptr)
{ {
sys_ppu_thread.warning("sys_ppu_thread_join(thread_id=0x%x, vptr=*0x%x)", thread_id, vptr); sys_ppu_thread.warning("sys_ppu_thread_join(thread_id=0x%x, vptr=*0x%x)", thread_id, vptr);
LV2_LOCK;
const auto thread = idm::get<ppu_thread>(thread_id); const auto thread = idm::get<ppu_thread>(thread_id);
if (!thread) if (!thread)
@ -56,7 +50,8 @@ s32 sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ptr<u64> vptr)
return CELL_ESRCH; return CELL_ESRCH;
} }
if (!thread->is_joinable || thread->is_joining) // TODO: this is race condition if EDEADLK check doesn't actually take the precedence
if (thread->joiner)
{ {
return CELL_EINVAL; return CELL_EINVAL;
} }
@ -66,32 +61,30 @@ s32 sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ptr<u64> vptr)
return CELL_EDEADLK; return CELL_EDEADLK;
} }
// mark joining if (!thread->joiner.compare_and_swap_test(0, ppu.id))
thread->is_joining = true;
// join thread
while (!test(thread->state & cpu_flag::exit))
{ {
CHECK_EMU_STATUS; return CELL_EINVAL;
LV2_UNLOCK, thread_ctrl::wait_for(1000);
} }
// get exit status from the register // Actually join
if (vptr) *vptr = thread->gpr[3]; thread->join();
// cleanup // Get the exit status from the register
if (vptr)
{
*vptr = thread->gpr[3];
}
// Cleanup
idm::remove<ppu_thread>(thread->id); idm::remove<ppu_thread>(thread->id);
return CELL_OK; return CELL_OK;
} }
s32 sys_ppu_thread_detach(u32 thread_id) error_code sys_ppu_thread_detach(u32 thread_id)
{ {
sys_ppu_thread.warning("sys_ppu_thread_detach(thread_id=0x%x)", thread_id); sys_ppu_thread.warning("sys_ppu_thread_detach(thread_id=0x%x)", thread_id);
LV2_LOCK;
const auto thread = idm::get<ppu_thread>(thread_id); const auto thread = idm::get<ppu_thread>(thread_id);
if (!thread) if (!thread)
@ -99,37 +92,38 @@ s32 sys_ppu_thread_detach(u32 thread_id)
return CELL_ESRCH; return CELL_ESRCH;
} }
if (!thread->is_joinable) u32 joiner = thread->joiner;
if (!joiner)
{
// Detach thread by assigning -1
joiner = thread->joiner.compare_and_swap(0, -1);
}
if (joiner == -1)
{ {
return CELL_EINVAL; return CELL_EINVAL;
} }
if (thread->is_joining) if (joiner)
{ {
return CELL_EBUSY; return CELL_EBUSY;
} }
// "detach"
thread->is_joinable = false;
return CELL_OK; return CELL_OK;
} }
void sys_ppu_thread_get_join_state(ppu_thread& ppu, vm::ptr<s32> isjoinable) void sys_ppu_thread_get_join_state(ppu_thread& ppu, vm::ptr<s32> isjoinable)
{ {
sys_ppu_thread.warning("sys_ppu_thread_get_join_state(isjoinable=*0x%x)", isjoinable); sys_ppu_thread.trace("sys_ppu_thread_get_join_state(isjoinable=*0x%x)", isjoinable);
LV2_LOCK; *isjoinable = ppu.joiner != -1;
*isjoinable = ppu.is_joinable;
} }
s32 sys_ppu_thread_set_priority(u32 thread_id, s32 prio) error_code sys_ppu_thread_set_priority(u32 thread_id, s32 prio)
{ {
sys_ppu_thread.trace("sys_ppu_thread_set_priority(thread_id=0x%x, prio=%d)", thread_id, prio); sys_ppu_thread.trace("sys_ppu_thread_set_priority(thread_id=0x%x, prio=%d)", thread_id, prio);
LV2_LOCK;
const auto thread = idm::get<ppu_thread>(thread_id); const auto thread = idm::get<ppu_thread>(thread_id);
if (!thread) if (!thread)
@ -147,12 +141,10 @@ s32 sys_ppu_thread_set_priority(u32 thread_id, s32 prio)
return CELL_OK; return CELL_OK;
} }
s32 sys_ppu_thread_get_priority(u32 thread_id, vm::ptr<s32> priop) error_code sys_ppu_thread_get_priority(u32 thread_id, vm::ptr<s32> priop)
{ {
sys_ppu_thread.trace("sys_ppu_thread_get_priority(thread_id=0x%x, priop=*0x%x)", thread_id, priop); sys_ppu_thread.trace("sys_ppu_thread_get_priority(thread_id=0x%x, priop=*0x%x)", thread_id, priop);
LV2_LOCK;
const auto thread = idm::get<ppu_thread>(thread_id); const auto thread = idm::get<ppu_thread>(thread_id);
if (!thread) if (!thread)
@ -165,7 +157,7 @@ s32 sys_ppu_thread_get_priority(u32 thread_id, vm::ptr<s32> priop)
return CELL_OK; return CELL_OK;
} }
s32 sys_ppu_thread_get_stack_information(ppu_thread& ppu, vm::ptr<sys_ppu_thread_stack_t> sp) error_code sys_ppu_thread_get_stack_information(ppu_thread& ppu, vm::ptr<sys_ppu_thread_stack_t> sp)
{ {
sys_ppu_thread.trace("sys_ppu_thread_get_stack_information(sp=*0x%x)", sp); sys_ppu_thread.trace("sys_ppu_thread_get_stack_information(sp=*0x%x)", sp);
@ -175,12 +167,10 @@ s32 sys_ppu_thread_get_stack_information(ppu_thread& ppu, vm::ptr<sys_ppu_thread
return CELL_OK; return CELL_OK;
} }
s32 sys_ppu_thread_stop(u32 thread_id) error_code sys_ppu_thread_stop(u32 thread_id)
{ {
sys_ppu_thread.todo("sys_ppu_thread_stop(thread_id=0x%x)", thread_id); sys_ppu_thread.todo("sys_ppu_thread_stop(thread_id=0x%x)", thread_id);
LV2_LOCK;
const auto thread = idm::get<ppu_thread>(thread_id); const auto thread = idm::get<ppu_thread>(thread_id);
if (!thread) if (!thread)
@ -188,17 +178,13 @@ s32 sys_ppu_thread_stop(u32 thread_id)
return CELL_ESRCH; return CELL_ESRCH;
} }
//t->Stop();
return CELL_OK; return CELL_OK;
} }
s32 sys_ppu_thread_restart(u32 thread_id) error_code sys_ppu_thread_restart(u32 thread_id)
{ {
sys_ppu_thread.todo("sys_ppu_thread_restart(thread_id=0x%x)", thread_id); sys_ppu_thread.todo("sys_ppu_thread_restart(thread_id=0x%x)", thread_id);
LV2_LOCK;
const auto thread = idm::get<ppu_thread>(thread_id); const auto thread = idm::get<ppu_thread>(thread_id);
if (!thread) if (!thread)
@ -206,19 +192,14 @@ s32 sys_ppu_thread_restart(u32 thread_id)
return CELL_ESRCH; return CELL_ESRCH;
} }
//t->Stop();
//t->Run();
return CELL_OK; return CELL_OK;
} }
s32 _sys_ppu_thread_create(vm::ptr<u64> thread_id, vm::ptr<ppu_thread_param_t> param, u64 arg, u64 unk, s32 prio, u32 stacksize, u64 flags, vm::cptr<char> threadname) error_code _sys_ppu_thread_create(vm::ptr<u64> thread_id, vm::ptr<ppu_thread_param_t> param, u64 arg, u64 unk, s32 prio, u32 stacksize, u64 flags, vm::cptr<char> threadname)
{ {
sys_ppu_thread.warning("_sys_ppu_thread_create(thread_id=*0x%x, param=*0x%x, arg=0x%llx, unk=0x%llx, prio=%d, stacksize=0x%x, flags=0x%llx, threadname=%s)", sys_ppu_thread.warning("_sys_ppu_thread_create(thread_id=*0x%x, param=*0x%x, arg=0x%llx, unk=0x%llx, prio=%d, stacksize=0x%x, flags=0x%llx, threadname=%s)",
thread_id, param, arg, unk, prio, stacksize, flags, threadname); thread_id, param, arg, unk, prio, stacksize, flags, threadname);
LV2_LOCK;
if (prio < 0 || prio > 3071) if (prio < 0 || prio > 3071)
{ {
return CELL_EINVAL; return CELL_EINVAL;
@ -229,36 +210,47 @@ s32 _sys_ppu_thread_create(vm::ptr<u64> thread_id, vm::ptr<ppu_thread_param_t> p
return CELL_EPERM; return CELL_EPERM;
} }
const auto ppu = idm::make_ptr<ppu_thread>(threadname ? threadname.get_ptr() : "", prio, stacksize); const u32 tid = idm::import<ppu_thread>([&]()
ppu->is_joinable = (flags & SYS_PPU_THREAD_CREATE_JOINABLE) != 0;
ppu->gpr[13] = param->tls.value();
if ((flags & SYS_PPU_THREAD_CREATE_INTERRUPT) == 0)
{ {
// Initialize thread entry point auto ppu = std::make_shared<ppu_thread>(threadname ? threadname.get_ptr() : "", prio, stacksize);
ppu->cmd_list
({ if ((flags & SYS_PPU_THREAD_CREATE_JOINABLE) != 0)
{ ppu_cmd::set_args, 2 }, arg, unk, // Actually unknown {
{ ppu_cmd::lle_call, param->entry.value() }, ppu->joiner = 0;
}); }
}
else ppu->gpr[13] = param->tls.value();
if ((flags & SYS_PPU_THREAD_CREATE_INTERRUPT) == 0)
{
// Initialize thread entry point
ppu->cmd_list
({
{ ppu_cmd::set_args, 2 }, arg, unk, // Actually unknown
{ ppu_cmd::lle_call, param->entry.value() },
});
}
else
{
// Save entry for further use (workaround)
ppu->gpr[2] = param->entry.value();
}
return ppu;
});
if (!tid)
{ {
// Save entry for further use return CELL_EAGAIN;
ppu->gpr[2] = param->entry.value();
} }
*thread_id = ppu->id; *thread_id = tid;
return CELL_OK; return CELL_OK;
} }
s32 sys_ppu_thread_start(u32 thread_id) error_code sys_ppu_thread_start(u32 thread_id)
{ {
sys_ppu_thread.warning("sys_ppu_thread_start(thread_id=0x%x)", thread_id); sys_ppu_thread.notice("sys_ppu_thread_start(thread_id=0x%x)", thread_id);
LV2_LOCK;
const auto thread = idm::get<ppu_thread>(thread_id); const auto thread = idm::get<ppu_thread>(thread_id);
@ -267,17 +259,16 @@ s32 sys_ppu_thread_start(u32 thread_id)
return CELL_ESRCH; return CELL_ESRCH;
} }
// TODO
thread->run(); thread->run();
return CELL_OK; return CELL_OK;
} }
s32 sys_ppu_thread_rename(u32 thread_id, vm::cptr<char> name) error_code sys_ppu_thread_rename(u32 thread_id, vm::cptr<char> name)
{ {
sys_ppu_thread.todo("sys_ppu_thread_rename(thread_id=0x%x, name=%s)", thread_id, name); sys_ppu_thread.todo("sys_ppu_thread_rename(thread_id=0x%x, name=%s)", thread_id, name);
LV2_LOCK;
const auto thread = idm::get<ppu_thread>(thread_id); const auto thread = idm::get<ppu_thread>(thread_id);
if (!thread) if (!thread)

View file

@ -25,8 +25,8 @@ struct sys_ppu_thread_stack_t
struct ppu_thread_param_t struct ppu_thread_param_t
{ {
be_t<u32> entry; // vm::bptr<void(u64)> be_t<u32> entry; // vm::ps3::bptr<void(u64)>
be_t<u32> tls; // vm::bptr<void> be_t<u32> tls; // vm::ps3::bptr<void>
}; };
enum : u32 enum : u32
@ -41,17 +41,18 @@ enum : u32
PPU_THREAD_STATUS_UNKNOWN, PPU_THREAD_STATUS_UNKNOWN,
}; };
// SysCalls // Syscalls
void _sys_ppu_thread_exit(ppu_thread& ppu, u64 errorcode); void _sys_ppu_thread_exit(ppu_thread& ppu, u64 errorcode);
void sys_ppu_thread_yield(); void sys_ppu_thread_yield();
s32 sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ps3::ptr<u64> vptr); error_code sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ps3::ptr<u64> vptr);
s32 sys_ppu_thread_detach(u32 thread_id); error_code sys_ppu_thread_detach(u32 thread_id);
void sys_ppu_thread_get_join_state(ppu_thread& ppu, vm::ps3::ptr<s32> isjoinable); void sys_ppu_thread_get_join_state(ppu_thread& ppu, vm::ps3::ptr<s32> isjoinable);
s32 sys_ppu_thread_set_priority(u32 thread_id, s32 prio); error_code sys_ppu_thread_set_priority(u32 thread_id, s32 prio);
s32 sys_ppu_thread_get_priority(u32 thread_id, vm::ps3::ptr<s32> priop); error_code sys_ppu_thread_get_priority(u32 thread_id, vm::ps3::ptr<s32> priop);
s32 sys_ppu_thread_get_stack_information(ppu_thread& ppu, vm::ps3::ptr<sys_ppu_thread_stack_t> sp); error_code sys_ppu_thread_get_stack_information(ppu_thread& ppu, vm::ps3::ptr<sys_ppu_thread_stack_t> sp);
s32 sys_ppu_thread_stop(u32 thread_id); error_code sys_ppu_thread_stop(u32 thread_id);
s32 sys_ppu_thread_restart(u32 thread_id); error_code sys_ppu_thread_restart(u32 thread_id);
s32 _sys_ppu_thread_create(vm::ps3::ptr<u64> thread_id, vm::ps3::ptr<ppu_thread_param_t> param, u64 arg, u64 arg4, s32 prio, u32 stacksize, u64 flags, vm::ps3::cptr<char> threadname); error_code _sys_ppu_thread_create(vm::ps3::ptr<u64> thread_id, vm::ps3::ptr<ppu_thread_param_t> param, u64 arg, u64 arg4, s32 prio, u32 stacksize, u64 flags, vm::ps3::cptr<char> threadname);
s32 sys_ppu_thread_start(u32 thread_id); error_code sys_ppu_thread_start(u32 thread_id);
s32 sys_ppu_thread_rename(u32 thread_id, vm::ps3::cptr<char> name); error_code sys_ppu_thread_rename(u32 thread_id, vm::ps3::cptr<char> name);