2019-12-09 12:57:37 +01:00
|
|
|
/*
|
2020-01-24 11:10:40 +01:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-12-09 12:57:37 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-05-12 00:02:10 +02:00
|
|
|
#include <stratosphere.hpp>
|
2019-12-09 12:57:37 +01:00
|
|
|
#include "impl/os_waitable_object_list.hpp"
|
2021-09-29 22:24:03 +02:00
|
|
|
#include "impl/os_waitable_holder_impl.hpp"
|
2020-04-08 11:21:35 +02:00
|
|
|
#include "impl/os_timeout_helper.hpp"
|
2019-12-09 12:57:37 +01:00
|
|
|
|
|
|
|
namespace ams::os {
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
void InitializeSemaphore(SemaphoreType *sema, s32 count, s32 max_count) {
|
|
|
|
AMS_ASSERT(max_count >= 1);
|
2021-09-29 22:24:03 +02:00
|
|
|
AMS_ASSERT(0 <= count && count <= max_count);
|
2020-04-08 11:21:35 +02:00
|
|
|
|
|
|
|
/* Setup objects. */
|
2021-03-22 04:30:40 +01:00
|
|
|
util::ConstructAt(sema->cs_sema);
|
|
|
|
util::ConstructAt(sema->cv_not_zero);
|
2020-04-08 11:21:35 +02:00
|
|
|
|
|
|
|
/* Setup wait lists. */
|
2021-03-22 04:30:40 +01:00
|
|
|
util::ConstructAt(sema->waitlist);
|
2020-04-08 11:21:35 +02:00
|
|
|
|
|
|
|
/* Set member variables. */
|
|
|
|
sema->count = count;
|
|
|
|
sema->max_count = max_count;
|
|
|
|
|
|
|
|
/* Mark initialized. */
|
|
|
|
sema->state = SemaphoreType::State_Initialized;
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
void FinalizeSemaphore(SemaphoreType *sema) {
|
|
|
|
AMS_ASSERT(sema->state = SemaphoreType::State_Initialized);
|
|
|
|
|
|
|
|
AMS_ASSERT(GetReference(sema->waitlist).IsEmpty());
|
|
|
|
|
|
|
|
/* Mark uninitialized. */
|
|
|
|
sema->state = SemaphoreType::State_NotInitialized;
|
|
|
|
|
|
|
|
/* Destroy wait lists. */
|
2021-03-22 04:30:40 +01:00
|
|
|
util::DestroyAt(sema->waitlist);
|
2020-04-08 11:21:35 +02:00
|
|
|
|
|
|
|
/* Destroy objects. */
|
2021-03-22 04:30:40 +01:00
|
|
|
util::DestroyAt(sema->cv_not_zero);
|
|
|
|
util::DestroyAt(sema->cs_sema);
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
void AcquireSemaphore(SemaphoreType *sema) {
|
|
|
|
AMS_ASSERT(sema->state == SemaphoreType::State_Initialized);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
{
|
|
|
|
std::scoped_lock lk(GetReference(sema->cs_sema));
|
|
|
|
|
|
|
|
while (sema->count == 0) {
|
|
|
|
GetReference(sema->cv_not_zero).Wait(GetPointer(sema->cs_sema));
|
|
|
|
}
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
--sema->count;
|
|
|
|
}
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
bool TryAcquireSemaphore(SemaphoreType *sema) {
|
|
|
|
AMS_ASSERT(sema->state == SemaphoreType::State_Initialized);
|
|
|
|
|
|
|
|
{
|
|
|
|
std::scoped_lock lk(GetReference(sema->cs_sema));
|
|
|
|
|
|
|
|
if (sema->count == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
--sema->count;
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
bool TimedAcquireSemaphore(SemaphoreType *sema, TimeSpan timeout) {
|
|
|
|
AMS_ASSERT(sema->state == SemaphoreType::State_Initialized);
|
|
|
|
AMS_ASSERT(timeout.GetNanoSeconds() >= 0);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
{
|
|
|
|
impl::TimeoutHelper timeout_helper(timeout);
|
|
|
|
std::scoped_lock lk(GetReference(sema->cs_sema));
|
|
|
|
|
|
|
|
while (sema->count == 0) {
|
|
|
|
if (timeout_helper.TimedOut()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
GetReference(sema->cv_not_zero).TimedWait(GetPointer(sema->cs_sema), timeout_helper);
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
--sema->count;
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
void ReleaseSemaphore(SemaphoreType *sema) {
|
|
|
|
AMS_ASSERT(sema->state == SemaphoreType::State_Initialized);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
{
|
|
|
|
std::scoped_lock lk(GetReference(sema->cs_sema));
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
AMS_ASSERT(sema->count + 1 <= sema->max_count);
|
|
|
|
|
|
|
|
++sema->count;
|
|
|
|
|
|
|
|
GetReference(sema->cv_not_zero).Signal();
|
|
|
|
GetReference(sema->waitlist).SignalAllThreads();
|
|
|
|
}
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
void ReleaseSemaphore(SemaphoreType *sema, s32 count) {
|
|
|
|
AMS_ASSERT(sema->state == SemaphoreType::State_Initialized);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
{
|
|
|
|
std::scoped_lock lk(GetReference(sema->cs_sema));
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
AMS_ASSERT(sema->count + count <= sema->max_count);
|
|
|
|
|
|
|
|
sema->count += count;
|
|
|
|
|
|
|
|
GetReference(sema->cv_not_zero).Signal();
|
|
|
|
GetReference(sema->waitlist).SignalAllThreads();
|
|
|
|
}
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
s32 GetCurrentSemaphoreCount(const SemaphoreType *sema) {
|
|
|
|
AMS_ASSERT(sema->state == SemaphoreType::State_Initialized);
|
|
|
|
|
|
|
|
return sema->count;
|
|
|
|
}
|
|
|
|
|
2021-09-29 22:24:03 +02:00
|
|
|
void InitializeWaitableHolder(WaitableHolderType *waitable_holder, SemaphoreType *sema) {
|
|
|
|
AMS_ASSERT(sema->state == SemaphoreType::State_Initialized);
|
|
|
|
|
|
|
|
util::ConstructAt(GetReference(waitable_holder->impl_storage).holder_of_semaphore_storage, sema);
|
|
|
|
|
|
|
|
waitable_holder->user_data = 0;
|
|
|
|
}
|
2020-04-08 11:21:35 +02:00
|
|
|
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|