kernel: refactored function naming to remove "__" prefix

This commit is contained in:
bunnei 2014-05-22 19:06:12 -04:00
parent ac21e8f2db
commit d26f3d4c1f
6 changed files with 75 additions and 65 deletions

View file

@ -139,15 +139,20 @@ void Shutdown() {
Kernel::ThreadingShutdown(); Kernel::ThreadingShutdown();
} }
} // namespace /**
* Loads executable stored at specified address
bool __KernelLoadExec(u32 entry_point) { * @entry_point Entry point in memory of loaded executable
Kernel::Init(); * @return True on success, otherwise false
*/
bool LoadExec(u32 entry_point) {
Init();
Core::g_app_core->SetPC(entry_point); Core::g_app_core->SetPC(entry_point);
// 0x30 is the typical main thread priority I've seen used so far // 0x30 is the typical main thread priority I've seen used so far
Handle thread_id = Kernel::SetupMainThread(0x30); Handle thread = Kernel::SetupMainThread(0x30);
return true; return true;
} }
} // namespace

View file

@ -144,6 +144,11 @@ private:
extern ObjectPool g_object_pool; extern ObjectPool g_object_pool;
} // namespace /**
* Loads executable stored at specified address
* @entry_point Entry point in memory of loaded executable
* @return True on success, otherwise false
*/
bool LoadExec(u32 entry_point);
bool __KernelLoadExec(u32 entry_point); } // namespace

View file

@ -30,17 +30,17 @@ public:
typedef std::multimap<Handle, Handle> MutexMap; typedef std::multimap<Handle, Handle> MutexMap;
static MutexMap g_mutex_held_locks; static MutexMap g_mutex_held_locks;
void __MutexAcquireLock(Mutex* mutex, Handle thread) { void MutexAcquireLock(Mutex* mutex, Handle thread) {
g_mutex_held_locks.insert(std::make_pair(thread, mutex->GetHandle())); g_mutex_held_locks.insert(std::make_pair(thread, mutex->GetHandle()));
mutex->lock_thread = thread; mutex->lock_thread = thread;
} }
void __MutexAcquireLock(Mutex* mutex) { void MutexAcquireLock(Mutex* mutex) {
Handle thread = GetCurrentThread(); Handle thread = GetCurrentThreadHandle();
__MutexAcquireLock(mutex, thread); MutexAcquireLock(mutex, thread);
} }
void __MutexEraseLock(Mutex* mutex) { void MutexEraseLock(Mutex* mutex) {
Handle handle = mutex->GetHandle(); Handle handle = mutex->GetHandle();
auto locked = g_mutex_held_locks.equal_range(mutex->lock_thread); auto locked = g_mutex_held_locks.equal_range(mutex->lock_thread);
for (MutexMap::iterator iter = locked.first; iter != locked.second; ++iter) { for (MutexMap::iterator iter = locked.first; iter != locked.second; ++iter) {
@ -52,29 +52,29 @@ void __MutexEraseLock(Mutex* mutex) {
mutex->lock_thread = -1; mutex->lock_thread = -1;
} }
bool __LockMutex(Mutex* mutex) { bool LockMutex(Mutex* mutex) {
// Mutex alread locked? // Mutex alread locked?
if (mutex->locked) { if (mutex->locked) {
return false; return false;
} }
__MutexAcquireLock(mutex); MutexAcquireLock(mutex);
return true; return true;
} }
bool __ReleaseMutexForThread(Mutex* mutex, Handle thread) { bool ReleaseMutexForThread(Mutex* mutex, Handle thread) {
__MutexAcquireLock(mutex, thread); MutexAcquireLock(mutex, thread);
Kernel::ResumeThreadFromWait(thread); Kernel::ResumeThreadFromWait(thread);
return true; return true;
} }
bool __ReleaseMutex(Mutex* mutex) { bool ReleaseMutex(Mutex* mutex) {
__MutexEraseLock(mutex); MutexEraseLock(mutex);
bool woke_threads = false; bool woke_threads = false;
auto iter = mutex->waiting_threads.begin(); auto iter = mutex->waiting_threads.begin();
// Find the next waiting thread for the mutex... // Find the next waiting thread for the mutex...
while (!woke_threads && !mutex->waiting_threads.empty()) { while (!woke_threads && !mutex->waiting_threads.empty()) {
woke_threads |= __ReleaseMutexForThread(mutex, *iter); woke_threads |= ReleaseMutexForThread(mutex, *iter);
mutex->waiting_threads.erase(iter); mutex->waiting_threads.erase(iter);
} }
// Reset mutex lock thread handle, nothing is waiting // Reset mutex lock thread handle, nothing is waiting
@ -91,7 +91,7 @@ bool __ReleaseMutex(Mutex* mutex) {
*/ */
Result ReleaseMutex(Handle handle) { Result ReleaseMutex(Handle handle) {
Mutex* mutex = Kernel::g_object_pool.GetFast<Mutex>(handle); Mutex* mutex = Kernel::g_object_pool.GetFast<Mutex>(handle);
if (!__ReleaseMutex(mutex)) { if (!ReleaseMutex(mutex)) {
return -1; return -1;
} }
return 0; return 0;
@ -110,7 +110,7 @@ Mutex* CreateMutex(Handle& handle, bool initial_locked) {
// Acquire mutex with current thread if initialized as locked... // Acquire mutex with current thread if initialized as locked...
if (mutex->locked) { if (mutex->locked) {
__MutexAcquireLock(mutex); MutexAcquireLock(mutex);
// Otherwise, reset lock thread handle // Otherwise, reset lock thread handle
} else { } else {

View file

@ -64,28 +64,33 @@ Thread* g_current_thread;
/// Gets the current thread /// Gets the current thread
inline Thread* __GetCurrentThread() { inline Thread* GetCurrentThread() {
return g_current_thread; return g_current_thread;
} }
/// Gets the current thread handle
Handle GetCurrentThreadHandle() {
return GetCurrentThread()->GetHandle();
}
/// Sets the current thread /// Sets the current thread
inline void __SetCurrentThread(Thread* t) { inline void SetCurrentThread(Thread* t) {
g_current_thread = t; g_current_thread = t;
g_current_thread_handle = t->GetHandle(); g_current_thread_handle = t->GetHandle();
} }
/// Saves the current CPU context /// Saves the current CPU context
void __SaveContext(ThreadContext& ctx) { void SaveContext(ThreadContext& ctx) {
Core::g_app_core->SaveContext(ctx); Core::g_app_core->SaveContext(ctx);
} }
/// Loads a CPU context /// Loads a CPU context
void __LoadContext(ThreadContext& ctx) { void LoadContext(ThreadContext& ctx) {
Core::g_app_core->LoadContext(ctx); Core::g_app_core->LoadContext(ctx);
} }
/// Resets a thread /// Resets a thread
void __ResetThread(Thread* t, u32 arg, s32 lowest_priority) { void ResetThread(Thread* t, u32 arg, s32 lowest_priority) {
memset(&t->context, 0, sizeof(ThreadContext)); memset(&t->context, 0, sizeof(ThreadContext));
t->context.cpu_registers[0] = arg; t->context.cpu_registers[0] = arg;
@ -101,7 +106,7 @@ void __ResetThread(Thread* t, u32 arg, s32 lowest_priority) {
} }
/// Change a thread to "ready" state /// Change a thread to "ready" state
void __ChangeReadyState(Thread* t, bool ready) { void ChangeReadyState(Thread* t, bool ready) {
Handle handle = t->GetHandle(); Handle handle = t->GetHandle();
if (t->IsReady()) { if (t->IsReady()) {
if (!ready) { if (!ready) {
@ -118,11 +123,11 @@ void __ChangeReadyState(Thread* t, bool ready) {
} }
/// Changes a threads state /// Changes a threads state
void __ChangeThreadState(Thread* t, ThreadStatus new_status) { void ChangeThreadState(Thread* t, ThreadStatus new_status) {
if (!t || t->status == new_status) { if (!t || t->status == new_status) {
return; return;
} }
__ChangeReadyState(t, (new_status & THREADSTATUS_READY) != 0); ChangeReadyState(t, (new_status & THREADSTATUS_READY) != 0);
t->status = new_status; t->status = new_status;
if (new_status == THREADSTATUS_WAIT) { if (new_status == THREADSTATUS_WAIT) {
@ -133,42 +138,42 @@ void __ChangeThreadState(Thread* t, ThreadStatus new_status) {
} }
/// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields) /// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields)
void __CallThread(Thread* t) { void CallThread(Thread* t) {
// Stop waiting // Stop waiting
if (t->wait_type != WAITTYPE_NONE) { if (t->wait_type != WAITTYPE_NONE) {
t->wait_type = WAITTYPE_NONE; t->wait_type = WAITTYPE_NONE;
} }
__ChangeThreadState(t, THREADSTATUS_READY); ChangeThreadState(t, THREADSTATUS_READY);
} }
/// Switches CPU context to that of the specified thread /// Switches CPU context to that of the specified thread
void __SwitchContext(Thread* t, const char* reason) { void SwitchContext(Thread* t, const char* reason) {
Thread* cur = __GetCurrentThread(); Thread* cur = GetCurrentThread();
// Save context for current thread // Save context for current thread
if (cur) { if (cur) {
__SaveContext(cur->context); SaveContext(cur->context);
if (cur->IsRunning()) { if (cur->IsRunning()) {
__ChangeReadyState(cur, true); ChangeReadyState(cur, true);
} }
} }
// Load context of new thread // Load context of new thread
if (t) { if (t) {
__SetCurrentThread(t); SetCurrentThread(t);
__ChangeReadyState(t, false); ChangeReadyState(t, false);
t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY; t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY;
t->wait_type = WAITTYPE_NONE; t->wait_type = WAITTYPE_NONE;
__LoadContext(t->context); LoadContext(t->context);
} else { } else {
__SetCurrentThread(NULL); SetCurrentThread(NULL);
} }
} }
/// Gets the next thread that is ready to be run by priority /// Gets the next thread that is ready to be run by priority
Thread* __NextThread() { Thread* NextThread() {
Handle next; Handle next;
Thread* cur = __GetCurrentThread(); Thread* cur = GetCurrentThread();
if (cur && cur->IsRunning()) { if (cur && cur->IsRunning()) {
next = g_thread_ready_queue.pop_first_better(cur->current_priority); next = g_thread_ready_queue.pop_first_better(cur->current_priority);
@ -183,9 +188,9 @@ Thread* __NextThread() {
/// Puts a thread in the wait state for the given type/reason /// Puts a thread in the wait state for the given type/reason
void WaitCurThread(WaitType wait_type, const char* reason) { void WaitCurThread(WaitType wait_type, const char* reason) {
Thread* t = __GetCurrentThread(); Thread* t = GetCurrentThread();
t->wait_type = wait_type; t->wait_type = wait_type;
__ChangeThreadState(t, ThreadStatus(THREADSTATUS_WAIT | (t->status & THREADSTATUS_SUSPEND))); ChangeThreadState(t, ThreadStatus(THREADSTATUS_WAIT | (t->status & THREADSTATUS_SUSPEND)));
} }
/// Resumes a thread from waiting by marking it as "ready" /// Resumes a thread from waiting by marking it as "ready"
@ -195,7 +200,7 @@ void ResumeThreadFromWait(Handle handle) {
if (t) { if (t) {
t->status &= ~THREADSTATUS_WAIT; t->status &= ~THREADSTATUS_WAIT;
if (!(t->status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) { if (!(t->status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
__ChangeReadyState(t, true); ChangeReadyState(t, true);
} }
} }
} }
@ -256,7 +261,7 @@ Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s3
Thread* t = CreateThread(handle, name, entry_point, priority, processor_id, stack_top, Thread* t = CreateThread(handle, name, entry_point, priority, processor_id, stack_top,
stack_size); stack_size);
__ResetThread(t, arg, 0); ResetThread(t, arg, 0);
HLE::EatCycles(32000); HLE::EatCycles(32000);
@ -264,16 +269,11 @@ Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s3
// Technically, this should not eat all at once, and reschedule in the middle, but that's hard. // Technically, this should not eat all at once, and reschedule in the middle, but that's hard.
HLE::ReSchedule("thread created"); HLE::ReSchedule("thread created");
__CallThread(t); CallThread(t);
return handle; return handle;
} }
/// Gets the current thread
Handle GetCurrentThread() {
return __GetCurrentThread()->GetHandle();
}
/// Sets up the primary application thread /// Sets up the primary application thread
Handle SetupMainThread(s32 priority, int stack_size) { Handle SetupMainThread(s32 priority, int stack_size) {
Handle handle; Handle handle;
@ -282,33 +282,33 @@ Handle SetupMainThread(s32 priority, int stack_size) {
Thread* t = CreateThread(handle, "main", Core::g_app_core->GetPC(), priority, Thread* t = CreateThread(handle, "main", Core::g_app_core->GetPC(), priority,
THREADPROCESSORID_0, Memory::SCRATCHPAD_VADDR_END, stack_size); THREADPROCESSORID_0, Memory::SCRATCHPAD_VADDR_END, stack_size);
__ResetThread(t, 0, 0); ResetThread(t, 0, 0);
// If running another thread already, set it to "ready" state // If running another thread already, set it to "ready" state
Thread* cur = __GetCurrentThread(); Thread* cur = GetCurrentThread();
if (cur && cur->IsRunning()) { if (cur && cur->IsRunning()) {
__ChangeReadyState(cur, true); ChangeReadyState(cur, true);
} }
// Run new "main" thread // Run new "main" thread
__SetCurrentThread(t); SetCurrentThread(t);
t->status = THREADSTATUS_RUNNING; t->status = THREADSTATUS_RUNNING;
__LoadContext(t->context); LoadContext(t->context);
return handle; return handle;
} }
/// Reschedules to the next available thread (call after current thread is suspended) /// Reschedules to the next available thread (call after current thread is suspended)
void Reschedule(const char* reason) { void Reschedule(const char* reason) {
Thread* prev = __GetCurrentThread(); Thread* prev = GetCurrentThread();
Thread* next = __NextThread(); Thread* next = NextThread();
if (next > 0) { if (next > 0) {
__SwitchContext(next, reason); SwitchContext(next, reason);
// Hack - automatically change previous thread (which would have been in "wait" state) to // Hack - automatically change previous thread (which would have been in "wait" state) to
// "ready" state, so that we can immediately resume to it when new thread yields. FixMe to // "ready" state, so that we can immediately resume to it when new thread yields. FixMe to
// actually wait for whatever event it is supposed to be waiting on. // actually wait for whatever event it is supposed to be waiting on.
__ChangeReadyState(prev, true); ChangeReadyState(prev, true);
} }
} }

View file

@ -59,8 +59,8 @@ void WaitCurThread(WaitType wait_type, const char* reason);
/// Resumes a thread from waiting by marking it as "ready" /// Resumes a thread from waiting by marking it as "ready"
void ResumeThreadFromWait(Handle handle); void ResumeThreadFromWait(Handle handle);
/// Gets the current thread /// Gets the current thread handle
Handle GetCurrentThread(); Handle GetCurrentThreadHandle();
/// Put current thread in a wait state - on WaitSynchronization /// Put current thread in a wait state - on WaitSynchronization
void WaitThread_Synchronization(); void WaitThread_Synchronization();

View file

@ -56,7 +56,7 @@ bool Load_ELF(std::string &filename) {
elf_reader = new ElfReader(buffer); elf_reader = new ElfReader(buffer);
elf_reader->LoadInto(0x00100000); elf_reader->LoadInto(0x00100000);
__KernelLoadExec(elf_reader->GetEntryPoint()); Kernel::LoadExec(elf_reader->GetEntryPoint());
delete[] buffer; delete[] buffer;
delete elf_reader; delete elf_reader;
@ -102,7 +102,7 @@ bool Load_DAT(std::string &filename) {
*d++ = (*s++); *d++ = (*s++);
} }
__KernelLoadExec(entry_point); Kernel::LoadExec(entry_point);
delete[] buffer; delete[] buffer;
@ -144,7 +144,7 @@ bool Load_BIN(std::string &filename) {
*d++ = (*s++); *d++ = (*s++);
} }
__KernelLoadExec(entry_point); Kernel::LoadExec(entry_point);
delete[] buffer; delete[] buffer;
} }