Atmosphere/libraries/libstratosphere/include/stratosphere/ddsf/ddsf_i_device.hpp

140 lines
4.9 KiB
C++
Raw Normal View History

2020-10-30 22:36:11 +00:00
/*
* Copyright (c) Atmosphère-NX
2020-10-30 22:36:11 +00: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/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_sdk_mutex.hpp>
#include <stratosphere/ddsf/ddsf_types.hpp>
#include <stratosphere/ddsf/impl/ddsf_for_each.hpp>
#include <stratosphere/ddsf/ddsf_i_castable.hpp>
#include <stratosphere/ddsf/ddsf_i_session.hpp>
namespace ams::ddsf {
class IDriver;
class IDevice : public ICastable {
friend Result OpenSession(IDevice *device, ISession *session, AccessMode mode);
friend void CloseSession(ISession *session);
friend class IDriver;
public:
AMS_DDSF_CASTABLE_ROOT_TRAITS(ams::ddsf::IDevice);
private:
2021-10-10 07:14:06 +00:00
util::IntrusiveListNode m_list_node;
IDriver *m_driver;
ISession::List m_session_list;
mutable os::SdkMutex m_session_list_lock;
bool m_is_exclusive_write;
2020-10-30 22:36:11 +00:00
public:
using ListTraits = util::IntrusiveListMemberTraits<&IDevice::m_list_node>;
2020-10-30 22:36:11 +00:00
using List = typename ListTraits::ListType;
friend class util::IntrusiveList<IDevice, util::IntrusiveListMemberTraits<&IDevice::m_list_node>>;
2020-10-30 22:36:11 +00:00
private:
Result AttachSession(ISession *session) {
AMS_ASSERT(session != nullptr);
2021-10-10 07:14:06 +00:00
std::scoped_lock lk(m_session_list_lock);
2020-10-30 22:36:11 +00:00
/* Check if we're allowed to attach the session. */
2021-10-10 07:14:06 +00:00
if (m_is_exclusive_write && session->CheckExclusiveWrite()) {
for (const auto &attached : m_session_list) {
2020-10-30 22:36:11 +00:00
R_UNLESS(!attached.CheckAccess(AccessMode_Write), ddsf::ResultAccessModeDenied());
}
}
/* Attach the session. */
2021-10-10 07:14:06 +00:00
m_session_list.push_back(*session);
2022-03-26 07:14:36 +00:00
R_SUCCEED();
2020-10-30 22:36:11 +00:00
}
void DetachSession(ISession *session) {
AMS_ASSERT(session != nullptr);
2021-10-10 07:14:06 +00:00
std::scoped_lock lk(m_session_list_lock);
m_session_list.erase(m_session_list.iterator_to(*session));
2020-10-30 22:36:11 +00:00
}
void AttachDriver(IDriver *drv) {
AMS_ASSERT(drv != nullptr);
AMS_ASSERT(!this->IsDriverAttached());
2021-10-10 07:14:06 +00:00
m_driver = drv;
2020-10-30 22:36:11 +00:00
AMS_ASSERT(this->IsDriverAttached());
}
void DetachDriver() {
AMS_ASSERT(this->IsDriverAttached());
2021-10-10 07:14:06 +00:00
m_driver = nullptr;
2020-10-30 22:36:11 +00:00
AMS_ASSERT(!this->IsDriverAttached());
}
public:
2021-10-10 07:14:06 +00:00
IDevice(bool exclusive_write) : m_list_node(), m_driver(nullptr), m_session_list(), m_session_list_lock(), m_is_exclusive_write(exclusive_write) {
m_session_list.clear();
2020-10-30 22:36:11 +00:00
}
protected:
virtual ~IDevice() {
2021-10-10 07:14:06 +00:00
m_session_list.clear();
2020-10-30 22:36:11 +00:00
}
public:
void AddTo(List &list) {
list.push_back(*this);
}
void RemoveFrom(List list) {
list.erase(list.iterator_to(*this));
}
bool IsLinkedToList() const {
2021-10-10 07:14:06 +00:00
return m_list_node.IsLinked();
2020-10-30 22:36:11 +00:00
}
IDriver &GetDriver() {
AMS_ASSERT(this->IsDriverAttached());
2021-10-10 07:14:06 +00:00
return *m_driver;
2020-10-30 22:36:11 +00:00
}
const IDriver &GetDriver() const {
AMS_ASSERT(this->IsDriverAttached());
2021-10-10 07:14:06 +00:00
return *m_driver;
2020-10-30 22:36:11 +00:00
}
bool IsDriverAttached() const {
2021-10-10 07:14:06 +00:00
return m_driver != nullptr;
2020-10-30 22:36:11 +00:00
}
template<typename F>
Result ForEachSession(F f, bool return_on_fail) {
2021-10-10 07:14:06 +00:00
return impl::ForEach(m_session_list_lock, m_session_list, f, return_on_fail);
2020-10-30 22:36:11 +00:00
}
template<typename F>
Result ForEachSession(F f, bool return_on_fail) const {
2021-10-10 07:14:06 +00:00
return impl::ForEach(m_session_list_lock, m_session_list, f, return_on_fail);
2020-10-30 22:36:11 +00:00
}
template<typename F>
int ForEachSession(F f) {
2021-10-10 07:14:06 +00:00
return impl::ForEach(m_session_list_lock, m_session_list, f);
2020-10-30 22:36:11 +00:00
}
template<typename F>
int ForEachSession(F f) const {
2021-10-10 07:14:06 +00:00
return impl::ForEach(m_session_list_lock, m_session_list, f);
2020-10-30 22:36:11 +00:00
}
bool HasAnyOpenSession() const {
2021-10-10 07:14:06 +00:00
return !m_session_list.empty();
2020-10-30 22:36:11 +00:00
}
};
}