Kernel: Added WaitObject and changed "waitable" objects inherit from it.

This commit is contained in:
bunnei
2015-01-14 19:22:50 -05:00
parent 0c7498545f
commit c22bac6398
8 changed files with 73 additions and 71 deletions

View File

@ -12,7 +12,7 @@
namespace Kernel {
class Semaphore : public Object {
class Semaphore : public WaitObject {
public:
std::string GetTypeName() const override { return "Semaphore"; }
std::string GetName() const override { return name; }
@ -22,7 +22,6 @@ public:
s32 max_count; ///< Maximum number of simultaneous holders the semaphore can have
s32 available_count; ///< Number of free slots left in the semaphore
std::queue<Handle> waiting_threads; ///< Threads that are waiting for the semaphore
std::string name; ///< Name of semaphore (optional)
/**
@ -38,7 +37,7 @@ public:
if (wait) {
Kernel::WaitCurrentThread(WAITTYPE_SEMA, this);
waiting_threads.push(GetCurrentThread()->GetHandle());
AddWaitingThread(GetCurrentThread());
} else {
--available_count;
}
@ -83,11 +82,7 @@ ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count) {
// Notify some of the threads that the semaphore has been released
// stop once the semaphore is full again or there are no more waiting threads
while (!semaphore->waiting_threads.empty() && semaphore->IsAvailable()) {
Thread* thread = Kernel::g_handle_table.Get<Thread>(semaphore->waiting_threads.front()).get();
if (thread != nullptr)
thread->ResumeFromWait();
semaphore->waiting_threads.pop();
while (semaphore->IsAvailable() && semaphore->ResumeNextThread() != nullptr) {
--semaphore->available_count;
}