semaphore: Updates for Switch.

This commit is contained in:
bunnei
2018-01-06 15:55:40 -05:00
parent 6f6d9af408
commit 91f10a1460
2 changed files with 31 additions and 21 deletions

View File

@ -5,6 +5,7 @@
#include "common/assert.h"
#include "core/hle/kernel/errors.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/object_address_table.h"
#include "core/hle/kernel/semaphore.h"
#include "core/hle/kernel/thread.h"
@ -13,20 +14,18 @@ namespace Kernel {
Semaphore::Semaphore() {}
Semaphore::~Semaphore() {}
ResultVal<SharedPtr<Semaphore>> Semaphore::Create(s32 initial_count, s32 max_count, VAddr address,
std::string name) {
if (initial_count > max_count)
return ERR_INVALID_COMBINATION_KERNEL;
ResultVal<SharedPtr<Semaphore>> Semaphore::Create(VAddr guest_addr, VAddr mutex_addr, std::string name) {
SharedPtr<Semaphore> semaphore(new Semaphore);
// When the semaphore is created, some slots are reserved for other threads,
// and the rest is reserved for the caller thread
semaphore->max_count = max_count;
semaphore->available_count = initial_count;
semaphore->address = address;
// and the rest is reserved for the caller thread;
semaphore->available_count = Memory::Read32(guest_addr);
semaphore->name = std::move(name);
semaphore->guest_addr = guest_addr;
semaphore->mutex_addr = mutex_addr;
// Semaphores are referenced by guest address, so track this in the kernel
g_object_address_table.Insert(guest_addr, semaphore);
return MakeResult<SharedPtr<Semaphore>>(std::move(semaphore));
}
@ -39,18 +38,22 @@ void Semaphore::Acquire(Thread* thread) {
if (available_count <= 0)
return;
--available_count;
UpdateGuestState();
}
ResultVal<s32> Semaphore::Release(s32 release_count) {
if (max_count - available_count < release_count)
return ERR_OUT_OF_RANGE_KERNEL;
s32 previous_count = available_count;
available_count += release_count;
UpdateGuestState();
WakeupAllWaitingThreads();
return MakeResult<s32>(previous_count);
}
} // namespace
void Semaphore::UpdateGuestState() {
Memory::Write32(guest_addr, available_count);
}
} // namespace Kernel