os: refactor/rewrite entire namespace.

This commit is contained in:
Michael Scire 2020-04-08 02:21:35 -07:00
parent 6193283f03
commit 065485b971
181 changed files with 5353 additions and 1929 deletions

View file

@ -324,28 +324,20 @@ namespace ams::fatal {
bool is_creport;
CpuContext cpu_ctx;
bool generate_error_report;
Event erpt_event;
Event battery_event;
os::Event *erpt_event;
os::Event *battery_event;
size_t stack_dump_size;
u64 stack_dump_base;
u8 stack_dump[0x100];
u64 tls_address;
u8 tls_dump[0x100];
void ClearState() {
this->result = ResultSuccess();
this->program_id = ncm::ProgramId::Invalid;
std::memset(this->proc_name, 0, sizeof(this->proc_name));
this->is_creport = false;
std::memset(&this->cpu_ctx, 0, sizeof(this->cpu_ctx));
this->generate_error_report = false;
std::memset(&this->erpt_event, 0, sizeof(this->erpt_event));
std::memset(&this->battery_event, 0, sizeof(this->battery_event));
this->stack_dump_size = 0;
this->stack_dump_base = 0;
std::memset(this->stack_dump, 0, sizeof(this->stack_dump));
this->tls_address = 0;
std::memset(this->tls_dump, 0, sizeof(this->tls_dump));
ThrowContext(os::Event *erpt, os::Event *bat)
: result(ResultSuccess()), program_id(), proc_name(), is_creport(), cpu_ctx(), generate_error_report(),
erpt_event(erpt), battery_event(bat),
stack_dump_size(), stack_dump_base(), stack_dump(), tls_address(), tls_dump()
{
/* ... */
}
};

View file

@ -63,7 +63,7 @@ namespace ams::fs {
s64 size;
os::Mutex mutex;
public:
constexpr explicit FileHandleStorage(FileHandle handle, bool close_file) : handle(handle), close_file(close_file), size(InvalidSize), mutex() { /* ... */ }
constexpr explicit FileHandleStorage(FileHandle handle, bool close_file) : handle(handle), close_file(close_file), size(InvalidSize), mutex(false) { /* ... */ }
constexpr explicit FileHandleStorage(FileHandle handle) : FileHandleStorage(handle, false) { /* ... */ }
virtual ~FileHandleStorage() override {

View file

@ -70,7 +70,7 @@ namespace ams::kvdb {
Path GetPath(const void *key, size_t key_size);
Result GetKey(size_t *out_size, void *out_key, size_t max_out_size, const FileName &file_name);
public:
FileKeyValueStore() { /* ... */ }
FileKeyValueStore() : lock(false) { /* ... */ }
/* Basic accessors. */
Result Initialize(const char *dir);

View file

@ -85,7 +85,7 @@ namespace ams::lmem::impl {
void *heap_start;
void *heap_end;
os::Mutex mutex;
os::MutexType mutex;
u8 option;
ImplementationHeapHead impl_head;
};

View file

@ -28,7 +28,7 @@ namespace ams::lr {
std::shared_ptr<IRegisteredLocationResolver> registered_location_resolver = nullptr;
std::shared_ptr<IAddOnContentLocationResolver> add_on_content_location_resolver = nullptr;
os::Mutex mutex;
os::Mutex mutex{false};
public:
/* Actual commands. */
virtual Result OpenLocationResolver(sf::Out<std::shared_ptr<ILocationResolver>> out, ncm::StorageId storage_id) override;

View file

@ -83,7 +83,7 @@ namespace ams::ncm {
ContentMetaDatabaseRoot() { /* ... */ }
};
private:
os::RecursiveMutex mutex;
os::Mutex mutex;
bool initialized;
ContentStorageRoot content_storage_roots[MaxContentStorageRoots];
ContentMetaDatabaseRoot content_meta_database_roots[MaxContentMetaDatabaseRoots];
@ -91,7 +91,7 @@ namespace ams::ncm {
u32 num_content_meta_entries;
RightsIdCache rights_id_cache;
public:
ContentManagerImpl() : initialized(false) { /* ... */ };
ContentManagerImpl() : mutex(true), initialized(false) { /* ... */ };
~ContentManagerImpl();
public:
Result Initialize(const ContentManagerConfig &config);

View file

@ -102,7 +102,7 @@ namespace ams::ncm {
return result;
}
public:
InstallTaskBase() : data(), progress(), cancel_requested() { /* ... */ }
InstallTaskBase() : data(), progress(), progress_mutex(false), cancel_mutex(false), cancel_requested(), throughput_mutex(false) { /* ... */ }
virtual ~InstallTaskBase() { /* ... */ };
public:
virtual void Cancel();

View file

@ -37,7 +37,7 @@ namespace ams::ncm {
u64 counter;
os::Mutex mutex;
public:
RightsIdCache() {
RightsIdCache() : mutex(false) {
this->Invalidate();
}

View file

@ -26,15 +26,13 @@
#include <stratosphere/os/os_process_handle.hpp>
#include <stratosphere/os/os_random.hpp>
#include <stratosphere/os/os_mutex.hpp>
#include <stratosphere/os/os_condvar.hpp>
#include <stratosphere/os/os_condition_variable.hpp>
#include <stratosphere/os/os_rw_lock.hpp>
#include <stratosphere/os/os_semaphore.hpp>
#include <stratosphere/os/os_timeout_helper.hpp>
#include <stratosphere/os/os_event.hpp>
#include <stratosphere/os/os_system_event.hpp>
#include <stratosphere/os/os_interrupt_event.hpp>
#include <stratosphere/os/os_thread_local_storage_api.hpp>
#include <stratosphere/os/os_thread.hpp>
#include <stratosphere/os/os_message_queue.hpp>
#include <stratosphere/os/os_waitable_holder.hpp>
#include <stratosphere/os/os_waitable_manager.hpp>
#include <stratosphere/os/os_waitable.hpp>

View file

@ -0,0 +1,58 @@
/*
* 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/os_condition_variable_common.hpp>
#if defined(ATMOSPHERE_OS_HORIZON)
#include <stratosphere/os/impl/os_internal_condition_variable_impl.os.horizon.hpp>
#else
#error "Unknown OS for ams::os::impl::InternalConditionVariableImpl"
#endif
namespace ams::os::impl {
class InternalConditionVariable {
private:
InternalConditionVariableImpl impl;
public:
constexpr InternalConditionVariable() : impl() { /* ... */ }
constexpr void Initialize() {
this->impl.Initialize();
}
void Signal() {
this->impl.Signal();
}
void Broadcast() {
this->impl.Broadcast();
}
void Wait(InternalCriticalSection *cs) {
this->impl.Wait(cs);
}
ConditionVariableStatus TimedWait(InternalCriticalSection *cs, const TimeoutHelper &timeout_helper) {
return this->impl.TimedWait(cs, timeout_helper);
}
};
using InternalConditionVariableStorage = TYPED_STORAGE(InternalConditionVariable);
}

View file

@ -0,0 +1,43 @@
/*
* 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/os_condition_variable_common.hpp>
#include <stratosphere/os/impl/os_internal_critical_section.hpp>
namespace ams::os::impl {
class TimeoutHelper;
class InternalConditionVariableImpl {
private:
u32 value;
public:
constexpr InternalConditionVariableImpl() : value(0) { /* ... */ }
constexpr void Initialize() {
this->value = 0;
}
void Signal();
void Broadcast();
void Wait(InternalCriticalSection *cs);
ConditionVariableStatus TimedWait(InternalCriticalSection *cs, const TimeoutHelper &timeout_helper);
};
}

View file

@ -0,0 +1,62 @@
/*
* 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>
#if defined(ATMOSPHERE_OS_HORIZON)
#include <stratosphere/os/impl/os_internal_critical_section_impl.os.horizon.hpp>
#else
#error "Unknown OS for ams::os::impl::InternalCriticalSectionImpl"
#endif
namespace ams::os::impl {
class InternalCriticalSection {
private:
InternalCriticalSectionImpl impl;
public:
constexpr InternalCriticalSection() : impl() { /* ... */ }
constexpr void Initialize() { this->impl.Initialize(); }
constexpr void Finalize() { this->impl.Finalize(); }
void Enter() { return this->impl.Enter(); }
bool TryEnter() { return this->impl.TryEnter(); }
void Leave() { return this->impl.Leave(); }
bool IsLockedByCurrentThread() const { return this->impl.IsLockedByCurrentThread(); }
ALWAYS_INLINE void Lock() { return this->Enter(); }
ALWAYS_INLINE bool TryLock() { return this->TryEnter(); }
ALWAYS_INLINE void Unlock() { return this->Leave(); }
ALWAYS_INLINE void lock() { return this->Lock(); }
ALWAYS_INLINE bool try_lock() { return this->TryLock(); }
ALWAYS_INLINE void unlock() { return this->Unlock(); }
InternalCriticalSectionImpl *Get() {
return std::addressof(this->impl);
}
const InternalCriticalSectionImpl *Get() const {
return std::addressof(this->impl);
}
};
using InternalCriticalSectionStorage = TYPED_STORAGE(InternalCriticalSection);
}

View file

@ -0,0 +1,52 @@
/*
* 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::impl {
class ReadWriteLockImpl;
class InternalConditionVariableImpl;
class InternalCriticalSectionImpl {
private:
friend class ReadWriteLockImpl;
friend class InternalConditionVariableImpl;
private:
u32 thread_handle;
public:
constexpr InternalCriticalSectionImpl() : thread_handle(svc::InvalidHandle) { /* ... */ }
constexpr void Initialize() { this->thread_handle = svc::InvalidHandle; }
constexpr void Finalize() { /* ... */}
void Enter();
bool TryEnter();
void Leave();
bool IsLockedByCurrentThread() const;
ALWAYS_INLINE void Lock() { return this->Enter(); }
ALWAYS_INLINE bool TryLock() { return this->TryEnter(); }
ALWAYS_INLINE void Unlock() { return this->Leave(); }
ALWAYS_INLINE void lock() { return this->Lock(); }
ALWAYS_INLINE bool try_lock() { return this->TryLock(); }
ALWAYS_INLINE void unlock() { return this->Unlock(); }
};
}

View file

@ -0,0 +1,65 @@
/*
* 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/os_mutex_types.hpp>
#include <stratosphere/os/os_condition_variable_common.hpp>
#include <stratosphere/os/os_condition_variable_types.hpp>
#include <stratosphere/os/os_condition_variable_api.hpp>
namespace ams::os {
class ConditionVariable {
NON_COPYABLE(ConditionVariable);
NON_MOVEABLE(ConditionVariable);
private:
ConditionVariableType cv;
public:
constexpr ConditionVariable() : cv{::ams::os::ConditionVariableType::State_Initialized, {{0}}} { /* ... */ }
~ConditionVariable() { FinalizeConditionVariable(std::addressof(this->cv)); }
void Signal() {
SignalConditionVariable(std::addressof(this->cv));
}
void Broadcast() {
BroadcastConditionVariable(std::addressof(this->cv));
}
void Wait(ams::os::MutexType &mutex) {
WaitConditionVariable(std::addressof(this->cv), std::addressof(mutex));
}
ConditionVariableStatus TimedWait(ams::os::MutexType &mutex, TimeSpan timeout) {
return TimedWaitConditionVariable(std::addressof(this->cv), std::addressof(mutex), timeout);
}
operator ConditionVariableType &() {
return this->cv;
}
operator const ConditionVariableType &() const {
return this->cv;
}
ConditionVariableType *GetBase() {
return std::addressof(this->cv);
}
};
}

View file

@ -0,0 +1,35 @@
/*
* 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/os_condition_variable_common.hpp>
namespace ams::os {
struct MutexType;
struct ConditionVariableType;
void InitializeConditionVariable(ConditionVariableType *cv);
void FinalizeConditionVariable(ConditionVariableType *cv);
void SignalConditionVariable(ConditionVariableType *cv);
void BroadcastConditionVariable(ConditionVariableType *cv);
void WaitConditionVariable(ConditionVariableType *cv, MutexType *m);
ConditionVariableStatus TimedWaitConditionVariable(ConditionVariableType *cv, MutexType *m, TimeSpan timeout);
}

View file

@ -0,0 +1,26 @@
/*
* 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 {
enum class ConditionVariableStatus {
TimedOut = 0,
Success = 1,
};
}

View file

@ -0,0 +1,37 @@
/*
* 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_condition_variable.hpp>
namespace ams::os {
struct ConditionVariableType {
enum State {
State_NotInitialized = 0,
State_Initialized = 1,
};
u8 state;
union {
s32 _arr[sizeof(impl::InternalConditionVariableStorage) / sizeof(s32)];
impl::InternalConditionVariableStorage _storage;
};
};
static_assert(std::is_trivial<ConditionVariableType>::value);
}

View file

@ -1,68 +0,0 @@
/*
* 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 "os_mutex.hpp"
namespace ams::os {
enum class ConditionVariableStatus {
TimedOut = 0,
Success = 1,
};
class ConditionVariable {
NON_COPYABLE(ConditionVariable);
NON_MOVEABLE(ConditionVariable);
private:
CondVar cv;
public:
constexpr ConditionVariable() : cv() { /* ... */ }
ConditionVariableStatus TimedWait(::Mutex *m, u64 timeout) {
if (timeout > 0) {
/* Abort on any error other than timed out/success. */
R_TRY_CATCH(condvarWaitTimeout(&this->cv, m, timeout)) {
R_CATCH(svc::ResultTimedOut) { return ConditionVariableStatus::TimedOut; }
} R_END_TRY_CATCH_WITH_ABORT_UNLESS;
return ConditionVariableStatus::Success;
}
return ConditionVariableStatus::TimedOut;
}
void Wait(::Mutex *m) {
R_ABORT_UNLESS(condvarWait(&this->cv, m));
}
ConditionVariableStatus TimedWait(os::Mutex *m, u64 timeout) {
return this->TimedWait(m->GetMutex(), timeout);
}
void Wait(os::Mutex *m) {
return this->Wait(m->GetMutex());
}
void Signal() {
condvarWakeOne(&this->cv);
}
void Broadcast() {
condvarWakeAll(&this->cv);
}
};
}

View file

@ -15,39 +15,58 @@
*/
#pragma once
#include "os_mutex.hpp"
#include "os_condvar.hpp"
#include "os_timeout_helper.hpp"
#include <vapours.hpp>
#include <stratosphere/os/os_event_common.hpp>
#include <stratosphere/os/os_event_types.hpp>
#include <stratosphere/os/os_event_api.hpp>
namespace ams::os {
namespace impl {
class WaitableObjectList;
class WaitableHolderOfEvent;
}
class Event {
friend class impl::WaitableHolderOfEvent;
NON_COPYABLE(Event);
NON_MOVEABLE(Event);
private:
util::TypedStorage<impl::WaitableObjectList, sizeof(util::IntrusiveListNode), alignof(util::IntrusiveListNode)> waitable_object_list_storage;
Mutex lock;
ConditionVariable cv;
u64 counter = 0;
bool auto_clear;
bool signaled;
EventType event;
public:
Event(bool a = true, bool s = false);
~Event();
explicit Event(EventClearMode clear_mode) {
InitializeEvent(std::addressof(this->event), false, clear_mode);
}
void Signal();
void Reset();
void Wait();
bool TryWait();
bool TimedWait(u64 ns);
~Event() {
FinalizeEvent(std::addressof(this->event));
}
void Wait() {
return WaitEvent(std::addressof(this->event));
}
bool TryWait() {
return TryWaitEvent(std::addressof(this->event));
}
bool TimedWait(TimeSpan timeout) {
return TimedWaitEvent(std::addressof(this->event), timeout);
}
void Signal() {
return SignalEvent(std::addressof(this->event));
}
void Clear() {
return ClearEvent(std::addressof(this->event));
}
operator EventType &() {
return this->event;
}
operator const EventType &() const {
return this->event;
}
EventType *GetBase() {
return std::addressof(this->event);
}
};
}

View file

@ -0,0 +1,37 @@
/*
* 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/os_event_common.hpp>
namespace ams::os {
struct EventType;
struct WaitableHolderType;
void InitializeEvent(EventType *event, bool signaled, EventClearMode clear_mode);
void FinalizeEvent(EventType *event);
void SignalEvent(EventType *event);
void WaitEvent(EventType *event);
bool TryWaitEvent(EventType *event);
bool TimedWaitEvent(EventType *event, TimeSpan timeout);
void ClearEvent(EventType *event);
void InitializeWaitableHolder(WaitableHolderType *waitable_holder, EventType *event);
}

View file

@ -0,0 +1,26 @@
/*
* 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 {
enum EventClearMode {
EventClearMode_ManualClear = 0,
EventClearMode_AutoClear = 1,
};
}

View file

@ -0,0 +1,49 @@
/*
* 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_condition_variable.hpp>
namespace ams::os {
namespace impl {
class WaitableObjectList;
}
struct EventType {
enum State {
State_NotInitialized = 0,
State_Initialized = 1,
};
util::TypedStorage<impl::WaitableObjectList, sizeof(util::IntrusiveListNode), alignof(util::IntrusiveListNode)> waitable_object_list_storage;
bool signaled;
bool initially_signaled;
u8 clear_mode;
u8 state;
u32 broadcast_counter_low;
u32 broadcast_counter_high;
impl::InternalCriticalSectionStorage cs_event;
impl::InternalConditionVariableStorage cv_signaled;
};
static_assert(std::is_trivial<EventType>::value);
}

View file

@ -15,35 +15,56 @@
*/
#pragma once
#include "os_managed_handle.hpp"
#include <vapours.hpp>
#include <stratosphere/os/os_event_common.hpp>
#include <stratosphere/os/os_interrupt_event_common.hpp>
#include <stratosphere/os/os_interrupt_event_types.hpp>
#include <stratosphere/os/os_interrupt_event_api.hpp>
namespace ams::os {
namespace impl {
class WaitableHolderOfInterruptEvent;
}
class InterruptEvent {
friend class impl::WaitableHolderOfInterruptEvent;
NON_COPYABLE(InterruptEvent);
NON_MOVEABLE(InterruptEvent);
private:
ManagedHandle handle;
bool auto_clear;
bool is_initialized;
InterruptEventType event;
public:
InterruptEvent() : auto_clear(true), is_initialized(false) { }
InterruptEvent(u32 interrupt_id, bool autoclear = true);
explicit InterruptEvent(InterruptName name, EventClearMode clear_mode) {
InitializeInterruptEvent(std::addressof(this->event), name, clear_mode);
}
Result Initialize(u32 interrupt_id, bool autoclear = true);
void Finalize();
~InterruptEvent() {
FinalizeInterruptEvent(std::addressof(this->event));
}
void Reset();
void Wait();
bool TryWait();
bool TimedWait(u64 ns);
void Wait() {
return WaitInterruptEvent(std::addressof(this->event));
}
bool TryWait() {
return TryWaitInterruptEvent(std::addressof(this->event));
}
bool TimedWait(TimeSpan timeout) {
return TimedWaitInterruptEvent(std::addressof(this->event), timeout);
}
void Clear() {
return ClearInterruptEvent(std::addressof(this->event));
}
operator InterruptEventType &() {
return this->event;
}
operator const InterruptEventType &() const {
return this->event;
}
InterruptEventType *GetBase() {
return std::addressof(this->event);
}
};
}

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>
#include <stratosphere/os/os_interrupt_event_common.hpp>
namespace ams::os {
struct InterruptEventType;
struct WaitableHolderType;
void InitializeInterruptEvent(InterruptEventType *event, InterruptName name, EventClearMode clear_mode);
void FinalizeInterruptEvent(InterruptEventType *event);
void WaitInterruptEvent(InterruptEventType *event);
bool TryWaitInterruptEvent(InterruptEventType *event);
bool TimedWaitInterruptEvent(InterruptEventType *event, TimeSpan timeout);
void ClearInterruptEvent(InterruptEventType *event);
void InitializeWaitableHolder(WaitableHolderType *waitable_holder, InterruptEventType *event);
}

View file

@ -0,0 +1,23 @@
/*
* 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 {
using InterruptName = s32;
}

View file

@ -0,0 +1,46 @@
/*
* 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_condition_variable.hpp>
namespace ams::os {
namespace impl {
class WaitableObjectList;
class InterruptEventImpl;
}
struct InterruptEventType {
enum State {
State_NotInitialized = 0,
State_Initialized = 1,
};
util::TypedStorage<impl::WaitableObjectList, sizeof(util::IntrusiveListNode), alignof(util::IntrusiveListNode)> waitable_object_list_storage;
u8 clear_mode;
u8 state;
util::TypedStorage<impl::InterruptEventImpl, sizeof(svc::Handle) * 2, alignof(svc::Handle)> impl;
};
static_assert(std::is_trivial<InterruptEventType>::value);
}

View file

@ -15,75 +15,87 @@
*/
#pragma once
#include "os_mutex.hpp"
#include "os_condvar.hpp"
#include <stratosphere/os/os_message_queue_common.hpp>
#include <stratosphere/os/os_message_queue_types.hpp>
#include <stratosphere/os/os_message_queue_api.hpp>
namespace ams::os {
namespace impl {
class WaitableObjectList;
template<MessageQueueWaitKind WaitKind>
class WaitableHolderOfMessageQueue;
}
class MessageQueue {
template<MessageQueueWaitKind WaitKind>
friend class impl::WaitableHolderOfMessageQueue;
NON_COPYABLE(MessageQueue);
NON_MOVEABLE(MessageQueue);
private:
util::TypedStorage<impl::WaitableObjectList, sizeof(util::IntrusiveListNode), alignof(util::IntrusiveListNode)> waitlist_not_empty;
util::TypedStorage<impl::WaitableObjectList, sizeof(util::IntrusiveListNode), alignof(util::IntrusiveListNode)> waitlist_not_full;
Mutex queue_lock;
ConditionVariable cv_not_full;
ConditionVariable cv_not_empty;
std::unique_ptr<uintptr_t[]> buffer;
size_t capacity;
size_t count;
size_t offset;
private:
constexpr inline bool IsFull() const {
return this->count >= this->capacity;
}
constexpr inline bool IsEmpty() const {
return this->count == 0;
}
void SendInternal(uintptr_t data);
void SendNextInternal(uintptr_t data);
uintptr_t ReceiveInternal();
uintptr_t PeekInternal();
MessageQueueType mq;
public:
MessageQueue(std::unique_ptr<uintptr_t[]> buf, size_t c);
~MessageQueue();
explicit MessageQueue(uintptr_t *buf, size_t count) {
InitializeMessageQueue(std::addressof(this->mq), buf, count);
}
/* For convenience. */
MessageQueue(size_t c) : MessageQueue(std::make_unique<uintptr_t[]>(c), c) { /* ... */ }
~MessageQueue() { FinalizeMessageQueue(std::addressof(this->mq)); }
/* Sending (FIFO functionality) */
void Send(uintptr_t data);
bool TrySend(uintptr_t data);
bool TimedSend(uintptr_t data, u64 timeout);
void Send(uintptr_t data) {
return SendMessageQueue(std::addressof(this->mq), data);
}
bool TrySend(uintptr_t data) {
return TrySendMessageQueue(std::addressof(this->mq), data);
}
bool TimedSend(uintptr_t data, TimeSpan timeout) {
return TimedSendMessageQueue(std::addressof(this->mq), data, timeout);
}
/* Sending (LIFO functionality) */
void SendNext(uintptr_t data);
bool TrySendNext(uintptr_t data);
bool TimedSendNext(uintptr_t data, u64 timeout);
void SendNext(uintptr_t data) {
return SendNextMessageQueue(std::addressof(this->mq), data);
}
bool TrySendNext(uintptr_t data) {
return TrySendNextMessageQueue(std::addressof(this->mq), data);
}
bool TimedSendNext(uintptr_t data, TimeSpan timeout) {
return TimedSendNextMessageQueue(std::addressof(this->mq), data, timeout);
}
/* Receive functionality */
void Receive(uintptr_t *out);
bool TryReceive(uintptr_t *out);
bool TimedReceive(uintptr_t *out, u64 timeout);
void Receive(uintptr_t *out) {
return ReceiveMessageQueue(out, std::addressof(this->mq));
}
bool TryReceive(uintptr_t *out) {
return TryReceiveMessageQueue(out, std::addressof(this->mq));
}
bool TimedReceive(uintptr_t *out, TimeSpan timeout) {
return TimedReceiveMessageQueue(out, std::addressof(this->mq), timeout);
}
/* Peek functionality */
void Peek(uintptr_t *out);
bool TryPeek(uintptr_t *out);
bool TimedPeek(uintptr_t *out, u64 timeout);
void Peek(uintptr_t *out) const {
return PeekMessageQueue(out, std::addressof(this->mq));
}
bool TryPeek(uintptr_t *out) const {
return TryPeekMessageQueue(out, std::addressof(this->mq));
}
bool TimedPeek(uintptr_t *out, TimeSpan timeout) const {
return TimedPeekMessageQueue(out, std::addressof(this->mq), timeout);
}
operator MessageQueueType &() {
return this->mq;
}
operator const MessageQueueType &() const {
return this->mq;
}
MessageQueueType *GetBase() {
return std::addressof(this->mq);
}
};
}

View file

@ -0,0 +1,51 @@
/*
* 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/os_message_queue_common.hpp>
namespace ams::os {
struct MessageQueueType;
struct WaitableHolderType;
void InitializeMessageQueue(MessageQueueType *mq, uintptr_t *buffer, size_t count);
void FinalizeMessageQueue(MessageQueueType *mq);
/* Sending (FIFO functionality) */
void SendMessageQueue(MessageQueueType *mq, uintptr_t data);
bool TrySendMessageQueue(MessageQueueType *mq, uintptr_t data);
bool TimedSendMessageQueue(MessageQueueType *mq, uintptr_t data, TimeSpan timeout);
/* Sending (LIFO functionality) */
void SendNextMessageQueue(MessageQueueType *mq, uintptr_t data);
bool TrySendNextMessageQueue(MessageQueueType *mq, uintptr_t data);
bool TimedSendNextMessageQueue(MessageQueueType *mq, uintptr_t data, TimeSpan timeout);
/* Receive functionality */
void ReceiveMessageQueue(uintptr_t *out, MessageQueueType *mq);
bool TryReceiveMessageQueue(uintptr_t *out, MessageQueueType *mq);
bool TimedReceiveMessageQueue(uintptr_t *out, MessageQueueType *mq, TimeSpan timeout);
/* Peek functionality */
void PeekMessageQueue(uintptr_t *out, const MessageQueueType *mq);
bool TryPeekMessageQueue(uintptr_t *out, const MessageQueueType *mq);
bool TimedPeekMessageQueue(uintptr_t *out, const MessageQueueType *mq, TimeSpan timeout);
void InitializeWaitableHolder(WaitableHolderType *waitable_holder, MessageQueueType *event, MessageQueueWaitType wait_type);
}

View file

@ -0,0 +1,26 @@
/*
* 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 {
enum class MessageQueueWaitType {
ForNotFull = 1,
ForNotEmpty = 2,
};
}

View file

@ -0,0 +1,50 @@
/*
* 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_condition_variable.hpp>
namespace ams::os {
namespace impl {
class WaitableObjectList;
}
struct MessageQueueType {
enum State {
State_NotInitialized = 0,
State_Initialized = 1,
};
util::TypedStorage<impl::WaitableObjectList, sizeof(util::IntrusiveListNode), alignof(util::IntrusiveListNode)> waitlist_not_full;
util::TypedStorage<impl::WaitableObjectList, sizeof(util::IntrusiveListNode), alignof(util::IntrusiveListNode)> waitlist_not_empty;
uintptr_t *buffer;
s32 capacity;
s32 count;
s32 offset;
u8 state;
mutable impl::InternalCriticalSectionStorage cs_queue;
mutable impl::InternalConditionVariableStorage cv_not_full;
mutable impl::InternalConditionVariableStorage cv_not_empty;
};
static_assert(std::is_trivial<MessageQueueType>::value);
}

View file

@ -15,82 +15,60 @@
*/
#pragma once
#include "os_common_types.hpp"
#include <stratosphere/os/os_mutex_common.hpp>
#include <stratosphere/os/os_mutex_types.hpp>
#include <stratosphere/os/os_mutex_api.hpp>
namespace ams::os {
class ConditionVariable;
class Mutex {
NON_COPYABLE(Mutex);
NON_MOVEABLE(Mutex);
friend class ams::os::ConditionVariable;
private:
::Mutex m;
private:
constexpr ::Mutex *GetMutex() {
return &this->m;
}
MutexType mutex;
public:
constexpr Mutex() : m() { /* ... */ }
constexpr explicit Mutex(bool recursive) : mutex{::ams::os::MutexType::State_Initialized, recursive, 0, 0, nullptr, {{0}}} { /* ... */ }
~Mutex() { FinalizeMutex(std::addressof(this->mutex)); }
void lock() {
mutexLock(GetMutex());
return LockMutex(std::addressof(this->mutex));
}
void unlock() {
mutexUnlock(GetMutex());
return UnlockMutex(std::addressof(this->mutex));
}
bool try_lock() {
return mutexTryLock(GetMutex());
return TryLockMutex(std::addressof(this->mutex));
}
void Lock() {
lock();
bool IsLockedByCurrentThread() const {
return IsMutexLockedByCurrentThread(std::addressof(this->mutex));
}
void Unlock() {
unlock();
ALWAYS_INLINE void Lock() {
return this->lock();
}
bool TryLock() {
return try_lock();
}
};
class RecursiveMutex {
private:
::RMutex m;
private:
constexpr ::RMutex *GetMutex() {
return &this->m;
}
public:
constexpr RecursiveMutex() : m() { /* ... */ }
void lock() {
rmutexLock(GetMutex());
ALWAYS_INLINE void Unlock() {
return this->unlock();
}
void unlock() {
rmutexUnlock(GetMutex());
ALWAYS_INLINE bool TryLock() {
return this->try_lock();
}
bool try_lock() {
return rmutexTryLock(GetMutex());
operator MutexType &() {
return this->mutex;
}
void Lock() {
lock();
operator const MutexType &() const {
return this->mutex;
}
void Unlock() {
unlock();
}
bool TryLock() {
return try_lock();
MutexType *GetBase() {
return std::addressof(this->mutex);
}
};

View file

@ -0,0 +1,34 @@
/*
* 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/os_mutex_common.hpp>
namespace ams::os {
struct MutexType;
void InitializeMutex(MutexType *mutex, bool recursive, int lock_level);
void FinalizeMutex(MutexType *mutex);
void LockMutex(MutexType *mutex);
bool TryLockMutex(MutexType *mutex);
void UnlockMutex(MutexType *mutex);
bool IsMutexLockedByCurrentThread(const MutexType *mutex);
}

View file

@ -0,0 +1,27 @@
/*
* 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 {
constexpr inline s32 MutexLockLevelMin = 1;
constexpr inline s32 MutexLockLevelMax = BITSIZEOF(s32) - 1;
constexpr inline s32 MutexLockLevelInitial = 0;
constexpr inline s32 MutexRecursiveLockCountMax = (1 << BITSIZEOF(u16)) - 1;
}

View file

@ -0,0 +1,43 @@
/*
* 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>
namespace ams::os {
struct ThreadType;
struct MutexType {
enum State {
State_NotInitialized = 0,
State_Initialized = 1,
};
u8 state;
bool is_recursive;
s32 lock_level;
s32 nest_count;
ThreadType *owner_thread;
union {
s32 _arr[sizeof(impl::InternalCriticalSectionStorage) / sizeof(s32)];
impl::InternalCriticalSectionStorage _storage;
};
};
static_assert(std::is_trivial<MutexType>::value);
}

View file

@ -13,43 +13,58 @@
* 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 "os_mutex.hpp"
#include "os_condvar.hpp"
#include <stratosphere/os/os_semaphore_types.hpp>
#include <stratosphere/os/os_semaphore_api.hpp>
namespace ams::os {
namespace impl {
class WaitableObjectList;
class WaitableHolderOfSemaphore;
}
class Semaphore {
friend class impl::WaitableHolderOfSemaphore;
NON_COPYABLE(Semaphore);
NON_MOVEABLE(Semaphore);
private:
util::TypedStorage<impl::WaitableObjectList, sizeof(util::IntrusiveListNode), alignof(util::IntrusiveListNode)> waitlist;
os::Mutex mutex;
os::ConditionVariable condvar;
int count;
int max_count;
SemaphoreType sema;
public:
explicit Semaphore(int c, int mc);
~Semaphore();
explicit Semaphore(s32 count, s32 max_count) {
InitializeSemaphore(std::addressof(this->sema), count, max_count);
}
void Acquire();
bool TryAcquire();
bool TimedAcquire(u64 timeout);
~Semaphore() { FinalizeSemaphore(std::addressof(this->sema)); }
void Release();
void Release(int count);
void Acquire() {
return os::AcquireSemaphore(std::addressof(this->sema));
}
constexpr inline int GetCurrentCount() const {
return this->count;
bool TryAcquire() {
return os::TryAcquireSemaphore(std::addressof(this->sema));
}
bool TimedAcquire(TimeSpan timeout) {
return os::TimedAcquireSemaphore(std::addressof(this->sema), timeout);
}
void Release() {
return os::ReleaseSemaphore(std::addressof(this->sema));
}
void Release(s32 count) {
return os::ReleaseSemaphore(std::addressof(this->sema), count);
}
s32 GetCurrentCount() const {
return os::GetCurrentSemaphoreCount(std::addressof(this->sema));
}
operator SemaphoreType &() {
return this->sema;
}
operator const SemaphoreType &() const {
return this->sema;
}
SemaphoreType *GetBase() {
return std::addressof(this->sema);
}
};

View file

@ -0,0 +1,39 @@
/*
* 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 SemaphoreType;
struct WaitableHolderType;
void InitializeSemaphore(SemaphoreType *sema, s32 count, s32 max_count);
void FinalizeSemaphore(SemaphoreType *sema);
void AcquireSemaphore(SemaphoreType *sema);
bool TryAcquireSemaphore(SemaphoreType *sema);
bool TimedAcquireSemaphore(SemaphoreType *sema, TimeSpan timeout);
void ReleaseSemaphore(SemaphoreType *sema);
void ReleaseSemaphore(SemaphoreType *sema, s32 count);
s32 GetCurrentSemaphoreCount(const SemaphoreType *sema);
void InitializeWaitableHolder(WaitableHolderType *waitable_holder, SemaphoreType *sema);
}

View file

@ -0,0 +1,46 @@
/*
* 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_condition_variable.hpp>
namespace ams::os {
namespace impl {
class WaitableObjectList;
}
struct SemaphoreType {
enum State {
State_NotInitialized = 0,
State_Initialized = 1,
};
util::TypedStorage<impl::WaitableObjectList, sizeof(util::IntrusiveListNode), alignof(util::IntrusiveListNode)> waitlist;
u8 state;
int count;
int max_count;
impl::InternalCriticalSectionStorage cs_sema;
impl::InternalConditionVariableStorage cv_not_zero;
};
static_assert(std::is_trivial<SemaphoreType>::value);
}

View file

@ -15,66 +15,99 @@
*/
#pragma once
#include "os_event.hpp"
#include <stratosphere/os/os_event_common.hpp>
#include <stratosphere/os/os_system_event_types.hpp>
#include <stratosphere/os/os_system_event_api.hpp>
namespace ams::os {
class WaitableHolder;
namespace impl {
class InterProcessEvent;
}
enum class SystemEventState {
Uninitialized,
Event,
InterProcessEvent,
};
class SystemEvent {
friend class WaitableHolder;
NON_COPYABLE(SystemEvent);
NON_MOVEABLE(SystemEvent);
private:
union {
util::TypedStorage<Event, sizeof(Event), alignof(Event)> storage_for_event;
util::TypedStorage<impl::InterProcessEvent, 3 * sizeof(Handle), alignof(Handle)> storage_for_inter_process_event;
};
SystemEventState state;
private:
Event &GetEvent();
const Event &GetEvent() const;
impl::InterProcessEvent &GetInterProcessEvent();
const impl::InterProcessEvent &GetInterProcessEvent() const;
SystemEventType system_event;
public:
SystemEvent() : state(SystemEventState::Uninitialized) { /* ... */ }
SystemEvent(bool inter_process, bool autoclear = true);
SystemEvent(Handle read_handle, bool manage_read_handle, Handle write_handle, bool manage_write_handle, bool autoclear = true);
SystemEvent(Handle read_handle, bool manage_read_handle, bool autoclear = true) : SystemEvent(read_handle, manage_read_handle, INVALID_HANDLE, false, autoclear) { /* ... */ }
~SystemEvent();
Result InitializeAsEvent(bool autoclear = true);
Result InitializeAsInterProcessEvent(bool autoclear = true);
void AttachHandles(Handle read_handle, bool manage_read_handle, Handle write_handle, bool manage_write_handle, bool autoclear = true);
void AttachReadableHandle(Handle read_handle, bool manage_read_handle, bool autoclear = true);
void AttachWritableHandle(Handle write_handle, bool manage_write_handle, bool autoclear = true);
Handle DetachReadableHandle();
Handle DetachWritableHandle();
Handle GetReadableHandle() const;
Handle GetWritableHandle() const;
void Finalize();
SystemEventState GetState() const {
return this->state;
SystemEvent() {
this->system_event.state = SystemEventType::State_NotInitialized;
}
void Signal();
void Reset();
void Wait();
bool TryWait();
bool TimedWait(u64 ns);
explicit SystemEvent(EventClearMode clear_mode, bool inter_process) {
R_ABORT_UNLESS(CreateSystemEvent(std::addressof(this->system_event), clear_mode, inter_process));
}
explicit SystemEvent(Handle read_handle, bool manage_read_handle, Handle write_handle, bool manage_write_handle, EventClearMode clear_mode) {
AttachSystemEvent(std::addressof(this->system_event), read_handle, manage_read_handle, write_handle, manage_write_handle, clear_mode);
}
~SystemEvent() {
if (this->system_event.state == SystemEventType::State_NotInitialized) {
return;
}
DestroySystemEvent(std::addressof(this->system_event));
}
void Attach(Handle read_handle, bool manage_read_handle, Handle write_handle, bool manage_write_handle, EventClearMode clear_mode) {
AMS_ABORT_UNLESS(this->system_event.state == SystemEventType::State_NotInitialized);
return AttachSystemEvent(std::addressof(this->system_event), read_handle, manage_read_handle, write_handle, manage_write_handle, clear_mode);
}
void AttachReadableHandle(Handle read_handle, bool manage_read_handle, EventClearMode clear_mode) {
AMS_ABORT_UNLESS(this->system_event.state == SystemEventType::State_NotInitialized);
return AttachReadableHandleToSystemEvent(std::addressof(this->system_event), read_handle, manage_read_handle, clear_mode);
}
void AttachWritableHandle(Handle write_handle, bool manage_write_handle, EventClearMode clear_mode) {
AMS_ABORT_UNLESS(this->system_event.state == SystemEventType::State_NotInitialized);
return AttachWritableHandleToSystemEvent(std::addressof(this->system_event), write_handle, manage_write_handle, clear_mode);
}
Handle DetachReadableHandle() {
return DetachReadableHandleOfSystemEvent(std::addressof(this->system_event));
}
Handle DetachWritableHandle() {
return DetachWritableHandleOfSystemEvent(std::addressof(this->system_event));
}
void Wait() {
return WaitSystemEvent(std::addressof(this->system_event));
}
bool TryWait() {
return TryWaitSystemEvent(std::addressof(this->system_event));
}
bool TimedWait(TimeSpan timeout) {
return TimedWaitSystemEvent(std::addressof(this->system_event), timeout);
}
void Signal() {
return SignalSystemEvent(std::addressof(this->system_event));
}
void Clear() {
return ClearSystemEvent(std::addressof(this->system_event));
}
Handle GetReadableHandle() const {
return GetReadableHandleOfSystemEvent(std::addressof(this->system_event));
}
Handle GetWritableHandle() const {
return GetWritableHandleOfSystemEvent(std::addressof(this->system_event));
}
operator SystemEventType &() {
return this->system_event;
}
operator const SystemEventType &() const {
return this->system_event;
}
SystemEventType *GetBase() {
return std::addressof(this->system_event);
}
};
}

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/os_event_common.hpp>
namespace ams::os {
struct SystemEventType;
struct WaitableHolderType;
Result CreateSystemEvent(SystemEventType *event, EventClearMode clear_mode, bool inter_process);
void DestroySystemEvent(SystemEventType *event);
void AttachSystemEvent(SystemEventType *event, Handle read_handle, bool read_handle_managed, Handle write_handle, bool write_handle_managed, EventClearMode clear_mode);
void AttachReadableHandleToSystemEvent(SystemEventType *event, Handle read_handle, bool manage_read_handle, EventClearMode clear_mode);
void AttachWritableHandleToSystemEvent(SystemEventType *event, Handle write_handle, bool manage_write_handle, EventClearMode clear_mode);
Handle DetachReadableHandleOfSystemEvent(SystemEventType *event);
Handle DetachWritableHandleOfSystemEvent(SystemEventType *event);
Handle GetReadableHandleOfSystemEvent(const SystemEventType *event);
Handle GetWritableHandleOfSystemEvent(const SystemEventType *event);
void SignalSystemEvent(SystemEventType *event);
void WaitSystemEvent(SystemEventType *event);
bool TryWaitSystemEvent(SystemEventType *event);
bool TimedWaitSystemEvent(SystemEventType *event, TimeSpan timeout);
void ClearSystemEvent(SystemEventType *event);
void InitializeWaitableHolder(WaitableHolderType *waitable_holder, SystemEventType *event);
}

View file

@ -0,0 +1,60 @@
/*
* 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/os_event_types.hpp>
namespace ams::os {
namespace impl {
struct InterProcessEventType {
enum State {
State_NotInitialized = 0,
State_Initialized = 1,
};
util::TypedStorage<impl::WaitableObjectList, sizeof(util::IntrusiveListNode), alignof(util::IntrusiveListNode)> waitable_object_list_storage;
bool auto_clear;
u8 state;
bool is_readable_handle_managed;
bool is_writable_handle_managed;
Handle readable_handle;
Handle writable_handle;
};
static_assert(std::is_trivial<InterProcessEventType>::value);
}
struct SystemEventType {
enum State {
State_NotInitialized = 0,
State_InitializedAsEvent = 1,
State_InitializedAsInterProcessEvent = 2,
};
union {
EventType event;
impl::InterProcessEventType inter_process_event;
};
u8 state;
};
static_assert(std::is_trivial<SystemEventType>::value);
}

View file

@ -13,107 +13,9 @@
* 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 "os_common_types.hpp"
#include "os_memory_common.hpp"
#include <vapours.hpp>
#include <stratosphere/os/os_thread_common.hpp>
#include <stratosphere/os/os_thread_types.hpp>
#include <stratosphere/os/os_thread_api.hpp>
namespace ams::os {
class Thread {
NON_COPYABLE(Thread);
NON_MOVEABLE(Thread);
private:
::Thread thr;
public:
constexpr Thread() : thr{} { /* ... */ }
Result Initialize(ThreadFunc entry, void *arg, void *stack_mem, size_t stack_sz, int prio, int cpuid = -2) {
return threadCreate(&this->thr, entry, arg, stack_mem, stack_sz, prio, cpuid);
}
Result Initialize(ThreadFunc entry, void *arg, size_t stack_sz, int prio, int cpuid = -2) {
return threadCreate(&this->thr, entry, arg, nullptr, stack_sz, prio, cpuid);
}
Handle GetHandle() const {
return this->thr.handle;
}
Result Start() {
return threadStart(&this->thr);
}
Result Wait() {
return threadWaitForExit(&this->thr);
}
Result Join() {
R_TRY(threadWaitForExit(&this->thr));
R_TRY(threadClose(&this->thr));
return ResultSuccess();
}
Result CancelSynchronization() {
return svcCancelSynchronization(this->thr.handle);
}
};
template<size_t StackSize>
class StaticThread {
NON_COPYABLE(StaticThread);
NON_MOVEABLE(StaticThread);
static_assert(util::IsAligned(StackSize, os::MemoryPageSize), "StaticThread must have aligned resource size");
private:
alignas(os::MemoryPageSize) u8 stack_mem[StackSize];
::Thread thr;
public:
constexpr StaticThread() : stack_mem{}, thr{} { /* ... */ }
constexpr StaticThread(ThreadFunc entry, void *arg, int prio, int cpuid = -2) : StaticThread() {
R_ABORT_UNLESS(this->Initialize(entry, arg, prio, cpuid));
}
Result Initialize(ThreadFunc entry, void *arg, int prio, int cpuid = -2) {
return threadCreate(&this->thr, entry, arg, this->stack_mem, StackSize, prio, cpuid);
}
Handle GetHandle() const {
return this->thr.handle;
}
Result Start() {
return threadStart(&this->thr);
}
Result Wait() {
return threadWaitForExit(&this->thr);
}
Result Join() {
R_TRY(threadWaitForExit(&this->thr));
R_TRY(threadClose(&this->thr));
return ResultSuccess();
}
Result CancelSynchronization() {
return svcCancelSynchronization(this->thr.handle);
}
};
ALWAYS_INLINE s32 GetCurrentThreadPriority() {
s32 prio;
R_ABORT_UNLESS(svcGetThreadPriority(&prio, CUR_THREAD_HANDLE));
return prio;
}
/* TODO: ThreadManager? */
ALWAYS_INLINE s32 GetCurrentProcessorNumber() {
return svcGetCurrentProcessorNumber();
}
ALWAYS_INLINE s32 GetCurrentCoreNumber() {
return GetCurrentProcessorNumber();
}
}

View file

@ -0,0 +1,67 @@
/*
* 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/os_thread_common.hpp>
namespace ams::os {
struct ThreadType;
struct WaitableHolderType;
Result CreateThread(ThreadType *thread, ThreadFunction function, void *argument, void *stack, size_t stack_size, s32 priority, s32 ideal_core);
Result CreateThread(ThreadType *thread, ThreadFunction function, void *argument, void *stack, size_t stack_size, s32 priority);
void DestroyThread(ThreadType *thread);
void StartThread(ThreadType *thread);
ThreadType *GetCurrentThread();
void WaitThread(ThreadType *thread);
bool TryWaitThread(ThreadType *thread);
void YieldThread();
void SleepThread(TimeSpan time);
s32 SuspendThread(ThreadType *thread);
s32 ResumeThread(ThreadType *thread);
s32 GetThreadSuspendCount(const ThreadType *thread);
void CancelThreadSynchronization(ThreadType *Thread);
/* TODO: void GetThreadContext(ThreadContextInfo *out_context, const ThreadType *thread); */
s32 ChangeThreadPriority(ThreadType *thread, s32 priority);
s32 GetThreadPriority(const ThreadType *thread);
s32 GetThreadCurrentPriority(const ThreadType *thread);
void SetThreadName(ThreadType *thread, const char *name);
void SetThreadNamePointer(ThreadType *thread, const char *name);
const char *GetThreadNamePointer(const ThreadType *thread);
s32 GetCurrentProcessorNumber();
s32 GetCurrentCoreNumber();
void SetThreadCoreMask(ThreadType *thread, s32 ideal_core, u64 affinity_mask);
void GetThreadCoreMask(s32 *out_ideal_core, u64 *out_affinity_mask, const ThreadType *thread);
u64 GetThreadAvailableCoreMask();
ThreadId GetThreadId(const ThreadType *thread);
void InitializeWaitableHolder(WaitableHolderType *holder, ThreadType *thread);
}

View file

@ -0,0 +1,38 @@
/*
* 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 {
constexpr inline s32 ThreadSuspendCountMax = 127;
constexpr inline s32 ThreadNameLengthMax = 0x20;
constexpr inline s32 ThreadPriorityRangeSize = 32;
constexpr inline s32 HighestThreadPriority = 0;
constexpr inline s32 DefaultThreadPriority = ThreadPriorityRangeSize / 2;
constexpr inline s32 LowestThreadPriority = ThreadPriorityRangeSize - 1;
constexpr inline s32 LowestSystemThreadPriority = 35;
constexpr inline s32 HighestSystemThreadPriority = -12;
constexpr inline size_t StackGuardAlignment = 4_KB;
constexpr inline size_t ThreadStackAlignment = 4_KB;
using ThreadFunction = void (*)(void *);
}

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 <vapours.hpp>
#include <stratosphere/os/os_thread_common.hpp>
#include <stratosphere/os/os_thread_local_storage_common.hpp>
#include <stratosphere/os/os_thread_local_storage_api.hpp>
#include <stratosphere/os/impl/os_internal_critical_section.hpp>
#include <stratosphere/os/impl/os_internal_condition_variable.hpp>
namespace ams::os {
namespace impl {
class WaitableObjectList;
}
using ThreadId = u64;
/* TODO */
using ThreadImpl = ::Thread;
struct ThreadType {
enum State {
State_NotInitialized = 0,
State_Initialized = 1,
State_DestroyedBeforeStarted = 2,
State_Started = 3,
State_Terminated = 4,
};
TYPED_STORAGE(util::IntrusiveListNode) all_threads_node;
util::TypedStorage<impl::WaitableObjectList, sizeof(util::IntrusiveListNode), alignof(util::IntrusiveListNode)> waitlist;
uintptr_t reserved[4];
u8 state;
u8 suspend_count;
s32 base_priority;
char name_buffer[ThreadNameLengthMax];
const char *name_pointer;
ThreadId thread_id;
void *stack;
size_t stack_size;
ThreadFunction function;
void *argument;
mutable impl::InternalCriticalSectionStorage cs_thread;
mutable impl::InternalConditionVariableStorage cv_thread;
ThreadImpl *thread_impl;
ThreadImpl thread_impl_storage;
};
static_assert(std::is_trivial<ThreadType>::value);
constexpr inline s32 IdealCoreDontCare = -1;
constexpr inline s32 IdealCoreUseDefault = -2;
constexpr inline s32 IdealCoreNoUpdate = -3;
}

View file

@ -1,63 +0,0 @@
/*
* 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 "os_common_types.hpp"
namespace ams::os {
class TimeoutHelper {
private:
u64 end_tick;
public:
TimeoutHelper(u64 ns) {
/* Special case zero-time timeouts. */
if (ns == 0) {
end_tick = 0;
return;
}
u64 cur_tick = armGetSystemTick();
this->end_tick = cur_tick + NsToTick(ns) + 1;
}
static constexpr inline u64 NsToTick(u64 ns) {
return (ns * 12) / 625;
}
static constexpr inline u64 TickToNs(u64 tick) {
return (tick * 625) / 12;
}
inline bool TimedOut() const {
if (this->end_tick == 0) {
return true;
}
return armGetSystemTick() >= this->end_tick;
}
inline u64 NsUntilTimeout() const {
u64 diff = TickToNs(this->end_tick - armGetSystemTick());
if (this->TimedOut()) {
return 0;
}
return diff;
}
};
}

View file

@ -0,0 +1,19 @@
/*
* 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_waitable_types.hpp>
#include <stratosphere/os/os_waitable_api.hpp>
#include <stratosphere/os/os_waitable_utils.hpp>

View file

@ -0,0 +1,45 @@
/*
* 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/os_message_queue_common.hpp>
namespace ams::os {
struct WaitableHolderType;
struct WaitableManagerType;
void InitializeWaitableManager(WaitableManagerType *manager);
void FinalizeWaitableManager(WaitableManagerType *manager);
WaitableHolderType *WaitAny(WaitableManagerType *manager);
WaitableHolderType *TryWaitAny(WaitableManagerType *manager);
WaitableHolderType *TimedWaitAny(WaitableManagerType *manager, TimeSpan timeout);
void FinalizeWaitableHolder(WaitableHolderType *holder);
void LinkWaitableHolder(WaitableManagerType *manager, WaitableHolderType *holder);
void UnlinkWaitableHolder(WaitableHolderType *holder);
void UnlinkAllWaitableHolder(WaitableManagerType *manager);
void MoveAllWaitableHolder(WaitableManagerType *dst, WaitableManagerType *src);
void SetWaitableHolderUserData(WaitableHolderType *holder, uintptr_t user_data);
uintptr_t GetWaitableHolderUserData(const WaitableHolderType *holder);
void InitializeWaitableHolder(WaitableHolderType *holder, Handle handle);
}

View file

@ -1,67 +0,0 @@
/*
* 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 "os_common_types.hpp"
namespace ams::os {
class WaitableManager;
class Event;
class SystemEvent;
class InterruptEvent;
class Thread;
class MessageQueue;
class Semaphore;
namespace impl {
class WaitableHolderImpl;
}
class WaitableHolder {
friend class WaitableManager;
NON_COPYABLE(WaitableHolder);
NON_MOVEABLE(WaitableHolder);
private:
util::TypedStorage<impl::WaitableHolderImpl, 2 * sizeof(util::IntrusiveListNode) + 3 * sizeof(void *), alignof(void *)> impl_storage;
uintptr_t user_data;
public:
static constexpr size_t ImplStorageSize = sizeof(impl_storage);
public:
WaitableHolder(Handle handle);
WaitableHolder(Event *event);
WaitableHolder(SystemEvent *event);
WaitableHolder(InterruptEvent *event);
WaitableHolder(Thread *thread);
WaitableHolder(Semaphore *semaphore);
WaitableHolder(MessageQueue *message_queue, MessageQueueWaitKind wait_kind);
~WaitableHolder();
void SetUserData(uintptr_t data) {
this->user_data = data;
}
uintptr_t GetUserData() const {
return this->user_data;
}
void UnlinkFromWaitableManager();
};
}

View file

@ -1,51 +0,0 @@
/*
* 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 "os_mutex.hpp"
namespace ams::os {
class WaitableHolder;
namespace impl {
class WaitableManagerImpl;
}
class WaitableManager {
NON_COPYABLE(WaitableManager);
NON_MOVEABLE(WaitableManager);
private:
util::TypedStorage<impl::WaitableManagerImpl, sizeof(util::IntrusiveListNode) + sizeof(Mutex) + 2 * sizeof(void *) + sizeof(Handle), alignof(void *)> impl_storage;
public:
static constexpr size_t ImplStorageSize = sizeof(impl_storage);
public:
WaitableManager();
~WaitableManager();
/* Wait. */
WaitableHolder *WaitAny();
WaitableHolder *TryWaitAny();
WaitableHolder *TimedWaitAny(u64 timeout);
/* Link. */
void LinkWaitableHolder(WaitableHolder *holder);
void UnlinkAll();
void MoveAllFrom(WaitableManager *other);
};
}

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>
namespace ams::os {
namespace impl {
class WaitableManagerImpl;
struct WaitableHolderImpl;
}
struct WaitableManagerType {
enum State {
State_NotInitialized,
State_Initialized,
};
u8 state;
bool is_waiting;
util::TypedStorage<impl::WaitableManagerImpl, sizeof(util::IntrusiveListNode) + sizeof(impl::InternalCriticalSection) + 2 * sizeof(void *) + sizeof(Handle), alignof(void *)> impl_storage;
};
static_assert(std::is_trivial<WaitableManagerType>::value);
struct WaitableHolderType {
util::TypedStorage<impl::WaitableHolderImpl, 2 * sizeof(util::IntrusiveListNode) + 3 * sizeof(void *), alignof(void *)> impl_storage;
uintptr_t user_data;
};
static_assert(std::is_trivial<WaitableHolderType>::value);
}

View file

@ -0,0 +1,22 @@
/*
* 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/os_message_queue_common.hpp>
namespace ams::os {
}

View file

@ -109,7 +109,7 @@ namespace ams::sf::cmif {
virtual void *AllocateDomain() = 0;
virtual void FreeDomain(void *) = 0;
protected:
ServerDomainManager(DomainEntryStorage *entry_storage, size_t entry_count) : entry_manager(entry_storage, entry_count) { /* ... */ }
ServerDomainManager(DomainEntryStorage *entry_storage, size_t entry_count) : entry_owner_lock(false), entry_manager(entry_storage, entry_count) { /* ... */ }
inline DomainServiceObject *AllocateDomainServiceObject() {
void *storage = this->AllocateDomain();

View file

@ -28,6 +28,9 @@ namespace ams::sf::hipc {
NeedsRetry,
};
void AttachWaitableHolderForAccept(os::WaitableHolderType *holder, Handle port);
void AttachWaitableHolderForReply(os::WaitableHolderType *holder, Handle request);
Result Receive(ReceiveResult *out_recv_result, Handle session_handle, const cmif::PointerAndSize &message_buffer);
Result Receive(bool *out_closed, Handle session_handle, const cmif::PointerAndSize &message_buffer);
Result Reply(Handle session_handle, const cmif::PointerAndSize &message_buffer);

View file

@ -47,7 +47,7 @@ namespace ams::sf::hipc {
using ServerDomainSessionManager::DomainEntryStorage;
using ServerDomainSessionManager::DomainStorage;
private:
class ServerBase : public os::WaitableHolder {
class ServerBase : public os::WaitableHolderType {
friend class ServerManagerBase;
template<size_t, typename, size_t>
friend class ServerManager;
@ -60,9 +60,9 @@ namespace ams::sf::hipc {
bool service_managed;
public:
ServerBase(Handle ph, sm::ServiceName sn, bool m, cmif::ServiceObjectHolder &&sh) :
os::WaitableHolder(ph), static_object(std::move(sh)), port_handle(ph), service_name(sn), service_managed(m)
static_object(std::move(sh)), port_handle(ph), service_name(sn), service_managed(m)
{
/* ... */
hipc::AttachWaitableHolderForAccept(this, ph);
}
virtual ~ServerBase() = 0;
@ -87,7 +87,7 @@ namespace ams::sf::hipc {
} else {
R_ABORT_UNLESS(sm::UnregisterService(this->service_name));
}
R_ABORT_UNLESS(svcCloseHandle(this->port_handle));
R_ABORT_UNLESS(svc::CloseHandle(this->port_handle));
}
}
@ -118,30 +118,30 @@ namespace ams::sf::hipc {
};
private:
/* Management of waitables. */
os::WaitableManager waitable_manager;
os::WaitableManagerType waitable_manager;
os::Event request_stop_event;
os::WaitableHolder request_stop_event_holder;
os::WaitableHolderType request_stop_event_holder;
os::Event notify_event;
os::WaitableHolder notify_event_holder;
os::WaitableHolderType notify_event_holder;
os::Mutex waitable_selection_mutex;
os::Mutex waitlist_mutex;
os::WaitableManager waitlist;
os::WaitableManagerType waitlist;
os::Mutex deferred_session_mutex;
using DeferredSessionList = typename util::IntrusiveListMemberTraits<&ServerSession::deferred_list_node>::ListType;
DeferredSessionList deferred_session_list;
private:
virtual void RegisterSessionToWaitList(ServerSession *session) override final;
void RegisterToWaitList(os::WaitableHolder *holder);
void RegisterToWaitList(os::WaitableHolderType *holder);
void ProcessWaitList();
bool WaitAndProcessImpl();
Result ProcessForServer(os::WaitableHolder *holder);
Result ProcessForMitmServer(os::WaitableHolder *holder);
Result ProcessForSession(os::WaitableHolder *holder);
Result ProcessForServer(os::WaitableHolderType *holder);
Result ProcessForMitmServer(os::WaitableHolderType *holder);
Result ProcessForSession(os::WaitableHolderType *holder);
void ProcessDeferredSessions();
@ -154,13 +154,13 @@ namespace ams::sf::hipc {
if constexpr (!ServiceObjectTraits<ServiceImpl>::IsMitmServiceObject) {
/* Non-mitm server. */
server->SetUserData(static_cast<uintptr_t>(UserDataTag::Server));
os::SetWaitableHolderUserData(server, static_cast<uintptr_t>(UserDataTag::Server));
} else {
/* Mitm server. */
server->SetUserData(static_cast<uintptr_t>(UserDataTag::MitmServer));
os::SetWaitableHolderUserData(server, static_cast<uintptr_t>(UserDataTag::MitmServer));
}
this->waitable_manager.LinkWaitableHolder(server);
os::LinkWaitableHolder(std::addressof(this->waitable_manager), server);
}
template<typename ServiceImpl>
@ -175,12 +175,16 @@ namespace ams::sf::hipc {
public:
ServerManagerBase(DomainEntryStorage *entry_storage, size_t entry_count) :
ServerDomainSessionManager(entry_storage, entry_count),
request_stop_event(false), request_stop_event_holder(&request_stop_event),
notify_event(false), notify_event_holder(&notify_event)
request_stop_event(os::EventClearMode_ManualClear), notify_event(os::EventClearMode_ManualClear),
waitable_selection_mutex(false), waitlist_mutex(false), deferred_session_mutex(false)
{
/* Link waitables. */
this->waitable_manager.LinkWaitableHolder(&this->request_stop_event_holder);
this->waitable_manager.LinkWaitableHolder(&this->notify_event_holder);
os::InitializeWaitableManager(std::addressof(this->waitable_manager));
os::InitializeWaitableHolder(std::addressof(this->request_stop_event_holder), this->request_stop_event.GetBase());
os::LinkWaitableHolder(std::addressof(this->waitable_manager), std::addressof(this->request_stop_event_holder));
os::InitializeWaitableHolder(std::addressof(this->notify_event_holder), this->notify_event.GetBase());
os::LinkWaitableHolder(std::addressof(this->waitable_manager), std::addressof(this->notify_event_holder));
os::InitializeWaitableManager(std::addressof(this->waitlist));
}
template<typename ServiceImpl, auto MakeShared = std::make_shared<ServiceImpl>>
@ -224,13 +228,13 @@ namespace ams::sf::hipc {
}
/* Processing. */
os::WaitableHolder *WaitSignaled();
os::WaitableHolderType *WaitSignaled();
void ResumeProcessing();
void RequestStopProcessing();
void AddUserWaitableHolder(os::WaitableHolder *waitable);
void AddUserWaitableHolder(os::WaitableHolderType *waitable);
Result Process(os::WaitableHolder *waitable);
Result Process(os::WaitableHolderType *waitable);
void WaitAndProcess();
void LoopProcess();
};
@ -356,7 +360,7 @@ namespace ams::sf::hipc {
return this->GetObjectBySessionIndex(session, this->saved_messages_start, hipc::TlsMessageBufferSize);
}
public:
ServerManager() : ServerManagerBase(this->domain_entry_storages, ManagerOptions::MaxDomainObjects) {
ServerManager() : ServerManagerBase(this->domain_entry_storages, ManagerOptions::MaxDomainObjects), resource_mutex(false) {
/* Clear storages. */
#define SF_SM_MEMCLEAR(obj) if constexpr (sizeof(obj) > 0) { std::memset(obj, 0, sizeof(obj)); }
SF_SM_MEMCLEAR(this->server_storages);

View file

@ -38,7 +38,7 @@ namespace ams::sf::hipc {
}
class ServerSession : public os::WaitableHolder {
class ServerSession : public os::WaitableHolderType {
friend class ServerSessionManager;
friend class ServerManagerBase;
friend class impl::HipcManager;
@ -54,14 +54,16 @@ namespace ams::sf::hipc {
bool is_closed;
bool has_received;
public:
ServerSession(Handle h, cmif::ServiceObjectHolder &&obj) : WaitableHolder(h), srv_obj_holder(std::move(obj)), session_handle(h) {
ServerSession(Handle h, cmif::ServiceObjectHolder &&obj) : srv_obj_holder(std::move(obj)), session_handle(h) {
hipc::AttachWaitableHolderForReply(this, h);
this->is_closed = false;
this->has_received = false;
this->forward_service = nullptr;
AMS_ABORT_UNLESS(!this->IsMitmSession());
}
ServerSession(Handle h, cmif::ServiceObjectHolder &&obj, std::shared_ptr<::Service> &&fsrv) : WaitableHolder(h), srv_obj_holder(std::move(obj)), session_handle(h) {
ServerSession(Handle h, cmif::ServiceObjectHolder &&obj, std::shared_ptr<::Service> &&fsrv) : srv_obj_holder(std::move(obj)), session_handle(h) {
hipc::AttachWaitableHolderForReply(this, h);
this->is_closed = false;
this->has_received = false;
this->forward_service = std::move(fsrv);