mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-07-04 16:42:14 +02:00
Stratosphere: Replace more raw loops with algorithms
This commit is contained in:
parent
c66b30a31d
commit
05f908da1f
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <switch.h>
|
#include <switch.h>
|
||||||
|
#include <algorithm>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
@ -11,18 +12,13 @@ class IServiceObject;
|
|||||||
|
|
||||||
class DomainOwner {
|
class DomainOwner {
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<IServiceObject> domain_objects[DOMAIN_ID_MAX];
|
std::array<std::shared_ptr<IServiceObject>, DOMAIN_ID_MAX> domain_objects;
|
||||||
public:
|
public:
|
||||||
DomainOwner() {
|
DomainOwner() = default;
|
||||||
for (unsigned int i = 0; i < DOMAIN_ID_MAX; i++) {
|
|
||||||
domain_objects[i].reset();
|
/* Shared ptrs should auto delete here. */
|
||||||
}
|
virtual ~DomainOwner() = default;
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~DomainOwner() {
|
|
||||||
/* Shared ptrs should auto delete here. */
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<IServiceObject> get_domain_object(unsigned int i) {
|
std::shared_ptr<IServiceObject> get_domain_object(unsigned int i) {
|
||||||
if (i < DOMAIN_ID_MAX) {
|
if (i < DOMAIN_ID_MAX) {
|
||||||
return domain_objects[i];
|
return domain_objects[i];
|
||||||
@ -31,15 +27,15 @@ class DomainOwner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Result reserve_object(std::shared_ptr<IServiceObject> object, unsigned int *out_i) {
|
Result reserve_object(std::shared_ptr<IServiceObject> object, unsigned int *out_i) {
|
||||||
for (unsigned int i = 4; i < DOMAIN_ID_MAX; i++) {
|
auto object_it = std::find(domain_objects.begin() + 4, domain_objects.end(), nullptr);
|
||||||
if (domain_objects[i] == NULL) {
|
if (object_it == domain_objects.end()) {
|
||||||
domain_objects[i] = object;
|
return 0x1900B;
|
||||||
object->set_owner(this);
|
|
||||||
*out_i = i;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 0x1900B;
|
|
||||||
|
*out_i = std::distance(domain_objects.begin(), object_it);
|
||||||
|
*object_it = object;
|
||||||
|
(*object_it)->set_owner(this);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result set_object(std::shared_ptr<IServiceObject> object, unsigned int i) {
|
Result set_object(std::shared_ptr<IServiceObject> object, unsigned int i) {
|
||||||
@ -52,26 +48,18 @@ class DomainOwner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsigned int get_object_id(std::shared_ptr<IServiceObject> object) {
|
unsigned int get_object_id(std::shared_ptr<IServiceObject> object) {
|
||||||
for (unsigned int i = 0; i < DOMAIN_ID_MAX; i++) {
|
auto object_it = std::find(domain_objects.begin(), domain_objects.end(), object);
|
||||||
if (domain_objects[i] == object) {
|
return std::distance(domain_objects.begin(), object_it);
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return DOMAIN_ID_MAX;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void delete_object(unsigned int i) {
|
void delete_object(unsigned int i) {
|
||||||
if (domain_objects[i]) {
|
domain_objects[i].reset();
|
||||||
domain_objects[i].reset();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void delete_object(std::shared_ptr<IServiceObject> object) {
|
void delete_object(std::shared_ptr<IServiceObject> object) {
|
||||||
for (unsigned int i = 0; i < DOMAIN_ID_MAX; i++) {
|
auto object_it = std::find(domain_objects.begin(), domain_objects.end(), object);
|
||||||
if (domain_objects[i] == object) {
|
if (object_it != domain_objects.end()) {
|
||||||
domain_objects[i].reset();
|
object_it->reset();
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include <switch.h>
|
#include <switch.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include <stratosphere/multithreadedwaitablemanager.hpp>
|
#include <stratosphere/multithreadedwaitablemanager.hpp>
|
||||||
|
|
||||||
@ -44,21 +45,15 @@ IWaitable *MultiThreadedWaitableManager::get_waitable() {
|
|||||||
rc = svcWaitSynchronization(&handle_index, handles.data(), this->waitables.size(), this->timeout);
|
rc = svcWaitSynchronization(&handle_index, handles.data(), this->waitables.size(), this->timeout);
|
||||||
IWaitable *w = this->waitables[handle_index];
|
IWaitable *w = this->waitables[handle_index];
|
||||||
if (R_SUCCEEDED(rc)) {
|
if (R_SUCCEEDED(rc)) {
|
||||||
for (int i = 0; i < handle_index; i++) {
|
std::for_each(waitables.begin(), waitables.begin() + handle_index, std::mem_fn(&IWaitable::update_priority));
|
||||||
this->waitables[i]->update_priority();
|
|
||||||
}
|
|
||||||
this->waitables.erase(this->waitables.begin() + handle_index);
|
this->waitables.erase(this->waitables.begin() + handle_index);
|
||||||
} else if (rc == 0xEA01) {
|
} else if (rc == 0xEA01) {
|
||||||
/* Timeout. */
|
/* Timeout. */
|
||||||
for (auto & waitable : this->waitables) {
|
std::for_each(waitables.begin(), waitables.end(), std::mem_fn(&IWaitable::update_priority));
|
||||||
waitable->update_priority();
|
|
||||||
}
|
|
||||||
} else if (rc != 0xF601 && rc != 0xE401) {
|
} else if (rc != 0xF601 && rc != 0xE401) {
|
||||||
/* TODO: Panic. When can this happen? */
|
/* TODO: Panic. When can this happen? */
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < handle_index; i++) {
|
std::for_each(waitables.begin(), waitables.begin() + handle_index, std::mem_fn(&IWaitable::update_priority));
|
||||||
this->waitables[i]->update_priority();
|
|
||||||
}
|
|
||||||
this->waitables.erase(this->waitables.begin() + handle_index);
|
this->waitables.erase(this->waitables.begin() + handle_index);
|
||||||
delete w;
|
delete w;
|
||||||
}
|
}
|
||||||
@ -107,4 +102,4 @@ void MultiThreadedWaitableManager::thread_func(void *t) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user