Atmosphere/stratosphere/pm/source/impl/pm_process_info.hpp
Michael Scire 609a302e16 os: implement waitable management.
This implements waitable management for Events (and
implements Events). It also refactors PM to use new
Event/Waitable semantics, and also adds STS_ASSERT
as a macro for asserting a boolean expression. The
rest of stratosphere has been refactored to use
STS_ASSERT whenever possible.
2019-12-07 12:41:28 -08:00

228 lines
6.8 KiB
C++

/*
* Copyright (c) 2018-2019 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 <switch.h>
#include <stratosphere.hpp>
#include <stratosphere/ldr.hpp>
#include <stratosphere/pm.hpp>
#include "pm_process_manager.hpp"
namespace sts::pm::impl {
class ProcessList;
class ProcessInfo {
friend class ProcessList;
NON_COPYABLE(ProcessInfo);
NON_MOVEABLE(ProcessInfo);
private:
enum Flag : u32 {
Flag_SignalOnExit = (1 << 0),
Flag_ExceptionOccurred = (1 << 1),
Flag_ExceptionWaitingAttach = (1 << 2),
Flag_SignalOnDebugEvent = (1 << 3),
Flag_SuspendedStateChanged = (1 << 4),
Flag_Suspended = (1 << 5),
Flag_Application = (1 << 6),
Flag_SignalOnStart = (1 << 7),
Flag_StartedStateChanged = (1 << 8),
};
private:
util::IntrusiveListNode list_node;
const u64 process_id;
const ldr::PinId pin_id;
const ncm::TitleLocation loc;
Handle handle;
ProcessState state;
u32 flags;
os::WaitableHolder waitable_holder;
private:
void SetFlag(Flag flag) {
this->flags |= flag;
}
void ClearFlag(Flag flag) {
this->flags &= ~flag;
}
bool HasFlag(Flag flag) const {
return (this->flags & flag);
}
public:
ProcessInfo(Handle h, u64 pid, ldr::PinId pin, const ncm::TitleLocation &l);
~ProcessInfo();
void Cleanup();
void LinkToWaitableManager(os::WaitableManager &manager) {
manager.LinkWaitableHolder(&this->waitable_holder);
}
Handle GetHandle() const {
return this->handle;
}
u64 GetProcessId() const {
return this->process_id;
}
ldr::PinId GetPinId() const {
return this->pin_id;
}
const ncm::TitleLocation &GetTitleLocation() {
return this->loc;
}
ProcessState GetState() const {
return this->state;
}
void SetState(ProcessState state) {
this->state = state;
}
bool HasStarted() const {
return this->state != ProcessState_Created && this->state != ProcessState_CreatedAttached;
}
bool HasExited() const {
return this->state == ProcessState_Exited;
}
#define DEFINE_FLAG_SET(flag) \
void Set##flag() { \
this->SetFlag(Flag_##flag); \
}
#define DEFINE_FLAG_GET(get, flag) \
bool get##flag() const { \
return this->HasFlag(Flag_##flag); \
}
#define DEFINE_FLAG_CLEAR(flag) \
void Clear##flag() { \
this->ClearFlag(Flag_##flag); \
}
DEFINE_FLAG_SET(SignalOnExit)
DEFINE_FLAG_GET(Should, SignalOnExit)
/* This needs a manual setter, because it sets two flags. */
void SetExceptionOccurred() {
this->SetFlag(Flag_ExceptionOccurred);
this->SetFlag(Flag_ExceptionWaitingAttach);
}
DEFINE_FLAG_GET(Has, ExceptionOccurred)
DEFINE_FLAG_GET(Has, ExceptionWaitingAttach)
DEFINE_FLAG_CLEAR(ExceptionOccurred)
DEFINE_FLAG_CLEAR(ExceptionWaitingAttach)
DEFINE_FLAG_SET(SignalOnDebugEvent)
DEFINE_FLAG_GET(Should, SignalOnDebugEvent)
DEFINE_FLAG_SET(SuspendedStateChanged)
DEFINE_FLAG_GET(Has, SuspendedStateChanged)
DEFINE_FLAG_CLEAR(SuspendedStateChanged)
DEFINE_FLAG_SET(Suspended)
DEFINE_FLAG_GET(Is, Suspended)
DEFINE_FLAG_CLEAR(Suspended)
DEFINE_FLAG_SET(Application)
DEFINE_FLAG_GET(Is, Application)
DEFINE_FLAG_SET(SignalOnStart)
DEFINE_FLAG_GET(Should, SignalOnStart)
DEFINE_FLAG_CLEAR(SignalOnStart)
DEFINE_FLAG_SET(StartedStateChanged)
DEFINE_FLAG_GET(Has, StartedStateChanged)
DEFINE_FLAG_CLEAR(StartedStateChanged)
#undef DEFINE_FLAG_SET
#undef DEFINE_FLAG_GET
#undef DEFINE_FLAG_CLEAR
};
class ProcessList final : public util::IntrusiveListMemberTraits<&ProcessInfo::list_node>::ListType {
private:
os::Mutex lock;
public:
void Lock() {
this->lock.Lock();
}
void Unlock() {
this->lock.Unlock();
}
void Remove(ProcessInfo *process_info) {
this->erase(this->iterator_to(*process_info));
}
ProcessInfo *Find(u64 process_id) {
for (auto it = this->begin(); it != this->end(); it++) {
if ((*it).GetProcessId() == process_id) {
return &*it;
}
}
return nullptr;
}
ProcessInfo *Find(ncm::TitleId title_id) {
for (auto it = this->begin(); it != this->end(); it++) {
if ((*it).GetTitleLocation().title_id == title_id) {
return &*it;
}
}
return nullptr;
}
};
class ProcessListAccessor final {
private:
ProcessList &list;
public:
explicit ProcessListAccessor(ProcessList &l) : list(l) {
this->list.Lock();
}
~ProcessListAccessor() {
this->list.Unlock();
}
ProcessList *operator->() {
return &this->list;
}
const ProcessList *operator->() const {
return &this->list;
}
ProcessList &operator*() {
return this->list;
}
const ProcessList &operator*() const {
return this->list;
}
};
}