os: implement LightSemaphore

This commit is contained in:
Michael Scire
2021-09-29 13:24:03 -07:00
parent b8a1ebd11a
commit 1e7a327a25
7 changed files with 346 additions and 5 deletions

View File

@@ -46,5 +46,5 @@
#include <stratosphere/os/os_message_queue.hpp>
#include <stratosphere/os/os_light_event.hpp>
#include <stratosphere/os/os_light_message_queue.hpp>
//#include <stratosphere/os/os_light_semaphore.hpp>
#include <stratosphere/os/os_light_semaphore.hpp>
#include <stratosphere/os/os_waitable.hpp>

View File

@@ -0,0 +1,72 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere/os/os_light_semaphore_types.hpp>
#include <stratosphere/os/os_light_semaphore_api.hpp>
namespace ams::os {
class LightSemaphore {
NON_COPYABLE(LightSemaphore);
NON_MOVEABLE(LightSemaphore);
private:
LightSemaphoreType m_sema;
public:
explicit LightSemaphore(s32 count, s32 max_count) {
InitializeLightSemaphore(std::addressof(m_sema), count, max_count);
}
~LightSemaphore() { FinalizeLightSemaphore(std::addressof(m_sema)); }
void Acquire() {
return os::AcquireLightSemaphore(std::addressof(m_sema));
}
bool TryAcquire() {
return os::TryAcquireLightSemaphore(std::addressof(m_sema));
}
bool TimedAcquire(TimeSpan timeout) {
return os::TimedAcquireLightSemaphore(std::addressof(m_sema), timeout);
}
void Release() {
return os::ReleaseLightSemaphore(std::addressof(m_sema));
}
void Release(s32 count) {
return os::ReleaseLightSemaphore(std::addressof(m_sema), count);
}
s32 GetCurrentCount() const {
return os::GetCurrentLightSemaphoreCount(std::addressof(m_sema));
}
operator LightSemaphoreType &() {
return m_sema;
}
operator const LightSemaphoreType &() const {
return m_sema;
}
LightSemaphoreType *GetBase() {
return std::addressof(m_sema);
}
};
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
namespace ams::os {
struct LightSemaphoreType;
void InitializeLightSemaphore(LightSemaphoreType *sema, s32 count, s32 max_count);
void FinalizeLightSemaphore(LightSemaphoreType *sema);
void AcquireLightSemaphore(LightSemaphoreType *sema);
bool TryAcquireLightSemaphore(LightSemaphoreType *sema);
bool TimedAcquireLightSemaphore(LightSemaphoreType *sema, TimeSpan timeout);
void ReleaseLightSemaphore(LightSemaphoreType *sema);
void ReleaseLightSemaphore(LightSemaphoreType *sema, s32 count);
s32 GetCurrentLightSemaphoreCount(const LightSemaphoreType *sema);
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/impl/os_internal_critical_section.hpp>
#include <stratosphere/os/impl/os_internal_busy_mutex.hpp>
#include <stratosphere/os/impl/os_internal_light_event.hpp>
namespace ams::os {
namespace impl {
using LightSemaphoreMutex = InternalBusyMutex;
using LightSemaphoreMutexStorage = InternalBusyMutexStorage;
}
struct LightSemaphoreType {
enum State {
State_NotInitialized = 0,
State_Initialized = 1,
};
u8 state;
s32 count;
s32 max_count;
mutable impl::LightSemaphoreMutexStorage mutex;
impl::InternalLightEventStorage ev_not_zero;
};
static_assert(std::is_trivial<LightSemaphoreType>::value);
}

View File

@@ -35,8 +35,8 @@ namespace ams::os {
util::TypedStorage<impl::WaitableObjectList, sizeof(util::IntrusiveListNode), alignof(util::IntrusiveListNode)> waitlist;
u8 state;
int count;
int max_count;
s32 count;
s32 max_count;
impl::InternalCriticalSectionStorage cs_sema;
impl::InternalConditionVariableStorage cv_not_zero;