Files
eden/src/core/hle/kernel/semaphore.cpp
T

56 lines
1.5 KiB
C++
Raw Normal View History

2014-12-03 18:49:51 -05:00
// Copyright 2014 Citra Emulator Project
2014-12-16 21:38:14 -08:00
// Licensed under GPLv2 or any later version
2014-12-03 18:49:51 -05:00
// Refer to the license.txt file included.
2015-05-06 04:06:12 -03:00
#include "common/assert.h"
#include "core/hle/kernel/errors.h"
2014-12-03 18:49:51 -05:00
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/semaphore.h"
2014-12-03 18:49:51 -05:00
#include "core/hle/kernel/thread.h"
namespace Kernel {
Semaphore::Semaphore() {}
Semaphore::~Semaphore() {}
ResultVal<SharedPtr<Semaphore>> Semaphore::Create(s32 initial_count, s32 max_count,
2016-09-18 09:38:01 +09:00
std::string name) {
2014-12-04 11:40:36 -05:00
2014-12-12 20:46:52 -05:00
if (initial_count > max_count)
return ERR_INVALID_COMBINATION_KERNEL;
2014-12-12 20:46:52 -05:00
SharedPtr<Semaphore> semaphore(new Semaphore);
2014-12-03 18:49:51 -05:00
// When the semaphore is created, some slots are reserved for other threads,
// and the rest is reserved for the caller thread
2014-12-12 20:46:52 -05:00
semaphore->max_count = max_count;
semaphore->available_count = initial_count;
semaphore->name = std::move(name);
2014-12-03 18:49:51 -05:00
return MakeResult<SharedPtr<Semaphore>>(std::move(semaphore));
2014-12-04 11:40:36 -05:00
}
bool Semaphore::ShouldWait(Thread* thread) const {
return available_count <= 0;
}
void Semaphore::Acquire(Thread* thread) {
if (available_count <= 0)
return;
--available_count;
}
2014-12-04 11:40:36 -05:00
ResultVal<s32> Semaphore::Release(s32 release_count) {
if (max_count - available_count < release_count)
return ERR_OUT_OF_RANGE_KERNEL;
2014-12-04 11:40:36 -05:00
s32 previous_count = available_count;
available_count += release_count;
2014-12-04 11:40:36 -05:00
WakeupAllWaitingThreads();
return MakeResult<s32>(previous_count);
2014-12-03 18:49:51 -05:00
}
} // namespace