mirror of
https://git.suyu.dev/suyu/suyu
synced 2024-12-25 10:52:48 -06:00
Kernel/Semaphores: Invert the available count checking.
Same semantics, idea by @yuriks
This commit is contained in:
parent
5e25986235
commit
effb181888
1 changed files with 9 additions and 11 deletions
|
@ -20,9 +20,8 @@ public:
|
||||||
static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Semaphore; }
|
static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Semaphore; }
|
||||||
Kernel::HandleType GetHandleType() const override { return Kernel::HandleType::Semaphore; }
|
Kernel::HandleType GetHandleType() const override { return Kernel::HandleType::Semaphore; }
|
||||||
|
|
||||||
u32 initial_count; ///< Number of entries reserved for other threads
|
|
||||||
u32 max_count; ///< Maximum number of simultaneous holders the semaphore can have
|
u32 max_count; ///< Maximum number of simultaneous holders the semaphore can have
|
||||||
u32 current_usage; ///< Number of currently used entries in the semaphore
|
u32 available_count; ///< Number of free slots left in the semaphore
|
||||||
std::queue<Handle> waiting_threads; ///< Threads that are waiting for the semaphore
|
std::queue<Handle> waiting_threads; ///< Threads that are waiting for the semaphore
|
||||||
std::string name; ///< Name of semaphore (optional)
|
std::string name; ///< Name of semaphore (optional)
|
||||||
|
|
||||||
|
@ -31,17 +30,17 @@ public:
|
||||||
* @return Whether the semaphore is available
|
* @return Whether the semaphore is available
|
||||||
*/
|
*/
|
||||||
bool IsAvailable() const {
|
bool IsAvailable() const {
|
||||||
return current_usage < max_count;
|
return available_count > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<bool> WaitSynchronization() override {
|
ResultVal<bool> WaitSynchronization() override {
|
||||||
bool wait = current_usage == max_count;
|
bool wait = available_count == 0;
|
||||||
|
|
||||||
if (wait) {
|
if (wait) {
|
||||||
Kernel::WaitCurrentThread(WAITTYPE_SEMA, GetHandle());
|
Kernel::WaitCurrentThread(WAITTYPE_SEMA, GetHandle());
|
||||||
waiting_threads.push(GetCurrentThreadHandle());
|
waiting_threads.push(GetCurrentThreadHandle());
|
||||||
} else {
|
} else {
|
||||||
++current_usage;
|
--available_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeResult<bool>(wait);
|
return MakeResult<bool>(wait);
|
||||||
|
@ -60,11 +59,10 @@ ResultCode CreateSemaphore(Handle* handle, u32 initial_count,
|
||||||
Semaphore* semaphore = new Semaphore;
|
Semaphore* semaphore = new Semaphore;
|
||||||
*handle = g_object_pool.Create(semaphore);
|
*handle = g_object_pool.Create(semaphore);
|
||||||
|
|
||||||
semaphore->initial_count = initial_count;
|
|
||||||
// When the semaphore is created, some slots are reserved for other threads,
|
// When the semaphore is created, some slots are reserved for other threads,
|
||||||
// and the rest is reserved for the caller thread
|
// and the rest is reserved for the caller thread
|
||||||
semaphore->max_count = max_count;
|
semaphore->max_count = max_count;
|
||||||
semaphore->current_usage = max_count - initial_count;
|
semaphore->available_count = initial_count;
|
||||||
semaphore->name = name;
|
semaphore->name = name;
|
||||||
|
|
||||||
return RESULT_SUCCESS;
|
return RESULT_SUCCESS;
|
||||||
|
@ -75,19 +73,19 @@ ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count) {
|
||||||
if (semaphore == nullptr)
|
if (semaphore == nullptr)
|
||||||
return InvalidHandle(ErrorModule::Kernel);
|
return InvalidHandle(ErrorModule::Kernel);
|
||||||
|
|
||||||
if (semaphore->current_usage < release_count)
|
if (semaphore->max_count - semaphore->available_count < release_count)
|
||||||
return ResultCode(ErrorDescription::OutOfRange, ErrorModule::Kernel,
|
return ResultCode(ErrorDescription::OutOfRange, ErrorModule::Kernel,
|
||||||
ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
|
ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
|
||||||
|
|
||||||
*count = semaphore->max_count - semaphore->current_usage;
|
*count = semaphore->available_count;
|
||||||
semaphore->current_usage = semaphore->current_usage - release_count;
|
semaphore->available_count += release_count;
|
||||||
|
|
||||||
// Notify some of the threads that the semaphore has been released
|
// 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
|
// stop once the semaphore is full again or there are no more waiting threads
|
||||||
while (!semaphore->waiting_threads.empty() && semaphore->IsAvailable()) {
|
while (!semaphore->waiting_threads.empty() && semaphore->IsAvailable()) {
|
||||||
Kernel::ResumeThreadFromWait(semaphore->waiting_threads.front());
|
Kernel::ResumeThreadFromWait(semaphore->waiting_threads.front());
|
||||||
semaphore->waiting_threads.pop();
|
semaphore->waiting_threads.pop();
|
||||||
semaphore->current_usage++;
|
--semaphore->available_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
return RESULT_SUCCESS;
|
return RESULT_SUCCESS;
|
||||||
|
|
Loading…
Reference in a new issue