ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count) { Semaphore* semaphore = g_handle_table.Get<Semaphore>(handle).get(); if (semaphore == nullptr) return InvalidHandle(ErrorModule::Kernel); if (semaphore->max_count - semaphore->available_count < release_count) return ResultCode(ErrorDescription::OutOfRange, ErrorModule::Kernel, ErrorSummary::InvalidArgument, ErrorLevel::Permanent); *count = semaphore->available_count; semaphore->available_count += 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->ShouldWait() && semaphore->WakeupNextThread() != nullptr) { semaphore->Acquire(); } return RESULT_SUCCESS; }