Skip to content

Commit

Permalink
Merge 1c51eac into c733d03
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekotekina committed Jan 30, 2017
2 parents c733d03 + 1c51eac commit f4cb525
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 104 deletions.
6 changes: 3 additions & 3 deletions rpcs3/Emu/Cell/PPUThread.h
Expand Up @@ -120,10 +120,10 @@ class ppu_thread : public cpu_thread
u32 prio = 0; // Thread priority (0..3071)
const u32 stack_size; // Stack size
const u32 stack_addr; // Stack address
bool is_joinable = true;
bool is_joining = false;

atomic_t<u32> joiner{~0u}; // Joining thread (-1 if detached)

lf_fifo<atomic_t<cmd64>, 255> cmd_queue; // Command queue for asynchronous operations.
lf_fifo<atomic_t<cmd64>, 127> cmd_queue; // Command queue for asynchronous operations.

void cmd_push(cmd64);
void cmd_list(std::initializer_list<cmd64>);
Expand Down
147 changes: 69 additions & 78 deletions rpcs3/Emu/Cell/lv2/sys_ppu_thread.cpp
Expand Up @@ -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);

// TODO: shall sys_mutex objects be unlocked?

LV2_LOCK;

ppu.state += cpu_flag::exit;

// Delete detached thread
if (!ppu.is_joinable)
if (ppu.joiner == -1)
{
idm::remove<ppu_thread>(ppu.id);
}
Expand All @@ -43,20 +39,19 @@ void sys_ppu_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);

LV2_LOCK;

const auto thread = idm::get<ppu_thread>(thread_id);

if (!thread)
{
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;
}
Expand All @@ -66,70 +61,69 @@ s32 sys_ppu_thread_join(ppu_thread& ppu, u32 thread_id, vm::ptr<u64> vptr)
return CELL_EDEADLK;
}

// mark joining
thread->is_joining = true;

// join thread
while (!test(thread->state & cpu_flag::exit))
if (!thread->joiner.compare_and_swap_test(0, ppu.id))
{
CHECK_EMU_STATUS;

LV2_UNLOCK, thread_ctrl::wait_for(1000);
return CELL_EINVAL;
}

// get exit status from the register
if (vptr) *vptr = thread->gpr[3];
// Actually join
thread->join();

// Get the exit status from the register
if (vptr)
{
*vptr = thread->gpr[3];
}

// cleanup
// Cleanup
idm::remove<ppu_thread>(thread->id);

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);

LV2_LOCK;

const auto thread = idm::get<ppu_thread>(thread_id);

if (!thread)
{
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;
}

if (thread->is_joining)
if (joiner)
{
return CELL_EBUSY;
}

// "detach"
thread->is_joinable = false;

return CELL_OK;
}

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);

LV2_LOCK;
sys_ppu_thread.trace("sys_ppu_thread_get_join_state(isjoinable=*0x%x)", isjoinable);

*isjoinable = ppu.is_joinable;
*isjoinable = ppu.joiner != -1;
}

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);

LV2_LOCK;

const auto thread = idm::get<ppu_thread>(thread_id);

if (!thread)
Expand All @@ -147,12 +141,10 @@ s32 sys_ppu_thread_set_priority(u32 thread_id, s32 prio)
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);

LV2_LOCK;

const auto thread = idm::get<ppu_thread>(thread_id);

if (!thread)
Expand All @@ -165,7 +157,7 @@ s32 sys_ppu_thread_get_priority(u32 thread_id, vm::ptr<s32> priop)
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);

Expand All @@ -175,50 +167,39 @@ s32 sys_ppu_thread_get_stack_information(ppu_thread& ppu, vm::ptr<sys_ppu_thread
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);

LV2_LOCK;

const auto thread = idm::get<ppu_thread>(thread_id);

if (!thread)
{
return CELL_ESRCH;
}

//t->Stop();

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);

LV2_LOCK;

const auto thread = idm::get<ppu_thread>(thread_id);

if (!thread)
{
return CELL_ESRCH;
}

//t->Stop();
//t->Run();

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)",
thread_id, param, arg, unk, prio, stacksize, flags, threadname);

LV2_LOCK;

if (prio < 0 || prio > 3071)
{
return CELL_EINVAL;
Expand All @@ -229,36 +210,47 @@ s32 _sys_ppu_thread_create(vm::ptr<u64> thread_id, vm::ptr<ppu_thread_param_t> p
return CELL_EPERM;
}

const auto ppu = idm::make_ptr<ppu_thread>(threadname ? threadname.get_ptr() : "", prio, stacksize);

ppu->is_joinable = (flags & SYS_PPU_THREAD_CREATE_JOINABLE) != 0;
ppu->gpr[13] = param->tls.value();

if ((flags & SYS_PPU_THREAD_CREATE_INTERRUPT) == 0)
const u32 tid = idm::import<ppu_thread>([&]()
{
// Initialize thread entry point
ppu->cmd_list
({
{ ppu_cmd::set_args, 2 }, arg, unk, // Actually unknown
{ ppu_cmd::lle_call, param->entry.value() },
});
}
else
auto ppu = std::make_shared<ppu_thread>(threadname ? threadname.get_ptr() : "", prio, stacksize);

if ((flags & SYS_PPU_THREAD_CREATE_JOINABLE) != 0)
{
ppu->joiner = 0;
}

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
ppu->gpr[2] = param->entry.value();
return CELL_EAGAIN;
}

*thread_id = ppu->id;

*thread_id = tid;
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);

LV2_LOCK;
sys_ppu_thread.notice("sys_ppu_thread_start(thread_id=0x%x)", thread_id);

const auto thread = idm::get<ppu_thread>(thread_id);

Expand All @@ -267,17 +259,16 @@ s32 sys_ppu_thread_start(u32 thread_id)
return CELL_ESRCH;
}

// TODO
thread->run();

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);

LV2_LOCK;

const auto thread = idm::get<ppu_thread>(thread_id);

if (!thread)
Expand Down
27 changes: 14 additions & 13 deletions rpcs3/Emu/Cell/lv2/sys_ppu_thread.h
Expand Up @@ -25,8 +25,8 @@ struct sys_ppu_thread_stack_t

struct ppu_thread_param_t
{
be_t<u32> entry; // vm::bptr<void(u64)>
be_t<u32> tls; // vm::bptr<void>
be_t<u32> entry; // vm::ps3::bptr<void(u64)>
be_t<u32> tls; // vm::ps3::bptr<void>
};

enum : u32
Expand All @@ -41,17 +41,18 @@ enum : u32
PPU_THREAD_STATUS_UNKNOWN,
};

// SysCalls
// Syscalls

void _sys_ppu_thread_exit(ppu_thread& ppu, u64 errorcode);
void sys_ppu_thread_yield();
s32 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_join(ppu_thread& ppu, u32 thread_id, vm::ps3::ptr<u64> vptr);
error_code sys_ppu_thread_detach(u32 thread_id);
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);
s32 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);
s32 sys_ppu_thread_stop(u32 thread_id);
s32 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);
s32 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_set_priority(u32 thread_id, s32 prio);
error_code sys_ppu_thread_get_priority(u32 thread_id, vm::ps3::ptr<s32> priop);
error_code sys_ppu_thread_get_stack_information(ppu_thread& ppu, vm::ps3::ptr<sys_ppu_thread_stack_t> sp);
error_code sys_ppu_thread_stop(u32 thread_id);
error_code sys_ppu_thread_restart(u32 thread_id);
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);
error_code sys_ppu_thread_start(u32 thread_id);
error_code sys_ppu_thread_rename(u32 thread_id, vm::ps3::cptr<char> name);

0 comments on commit f4cb525

Please sign in to comment.