Thread: Move Get/SetThreadPriority to be Thread members

This commit is contained in:
Yuri Kunde Schlesner 2014-12-22 11:17:34 -02:00
parent ff992edecf
commit d1b7587647
3 changed files with 24 additions and 42 deletions

View File

@ -350,27 +350,8 @@ ResultVal<Handle> Thread::Create(const char* name, u32 entry_point, s32 priority
return MakeResult<Handle>(*handle);
}
/// Get the priority of the thread specified by handle
ResultVal<u32> GetThreadPriority(const Handle handle) {
Thread* thread = g_handle_table.Get<Thread>(handle);
if (thread == nullptr) return InvalidHandle(ErrorModule::Kernel);
return MakeResult<u32>(thread->current_priority);
}
/// Set the priority of the thread specified by handle
ResultCode SetThreadPriority(Handle handle, s32 priority) {
Thread* thread = nullptr;
if (!handle) {
thread = GetCurrentThread(); // TODO(bunnei): Is this correct behavior?
} else {
thread = g_handle_table.Get<Thread>(handle);
if (thread == nullptr) {
return InvalidHandle(ErrorModule::Kernel);
}
}
_assert_msg_(KERNEL, (thread != nullptr), "called, but thread is nullptr!");
void Thread::SetPriority(s32 priority) {
// If priority is invalid, clamp to valid range
if (priority < THREADPRIO_HIGHEST || priority > THREADPRIO_LOWEST) {
s32 new_priority = CLAMP(priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
@ -381,20 +362,18 @@ ResultCode SetThreadPriority(Handle handle, s32 priority) {
}
// Change thread priority
s32 old = thread->current_priority;
thread_ready_queue.remove(old, handle);
thread->current_priority = priority;
thread_ready_queue.prepare(thread->current_priority);
s32 old = current_priority;
thread_ready_queue.remove(old, GetHandle());
current_priority = priority;
thread_ready_queue.prepare(current_priority);
// Change thread status to "ready" and push to ready queue
if (thread->IsRunning()) {
thread->status = (thread->status & ~THREADSTATUS_RUNNING) | THREADSTATUS_READY;
if (IsRunning()) {
status = (status & ~THREADSTATUS_RUNNING) | THREADSTATUS_READY;
}
if (thread->IsReady()) {
thread_ready_queue.push_back(thread->current_priority, handle);
if (IsReady()) {
thread_ready_queue.push_back(current_priority, GetHandle());
}
return RESULT_SUCCESS;
}
/// Sets up the primary application thread

View File

@ -70,6 +70,9 @@ public:
ResultVal<bool> WaitSynchronization() override;
s32 GetPriority() const { return current_priority; }
void SetPriority(s32 priority);
Core::ThreadContext context;
u32 thread_id;
@ -146,12 +149,6 @@ void WaitCurrentThread(WaitType wait_type, Handle wait_handle, VAddr wait_addres
/// Put current thread in a wait state - on WaitSynchronization
void WaitThread_Synchronization();
/// Get the priority of the thread specified by handle
ResultVal<u32> GetThreadPriority(const Handle handle);
/// Set the priority of the thread specified by handle
ResultCode SetThreadPriority(Handle handle, s32 priority);
/// Initialize threading
void ThreadingInit();

View File

@ -256,16 +256,22 @@ static u32 ExitThread() {
/// Gets the priority for the specified thread
static Result GetThreadPriority(s32* priority, Handle handle) {
ResultVal<u32> priority_result = Kernel::GetThreadPriority(handle);
if (priority_result.Succeeded()) {
*priority = *priority_result;
}
return priority_result.Code().raw;
const Kernel::Thread* thread = Kernel::g_handle_table.Get<Kernel::Thread>(handle);
if (thread == nullptr)
return InvalidHandle(ErrorModule::Kernel).raw;
*priority = thread->GetPriority();
return RESULT_SUCCESS.raw;
}
/// Sets the priority for the specified thread
static Result SetThreadPriority(Handle handle, s32 priority) {
return Kernel::SetThreadPriority(handle, priority).raw;
Kernel::Thread* thread = Kernel::g_handle_table.Get<Kernel::Thread>(handle);
if (thread == nullptr)
return InvalidHandle(ErrorModule::Kernel).raw;
thread->SetPriority(priority);
return RESULT_SUCCESS.raw;
}
/// Create a mutex